2020-11-05 18:58:23 +02:00
|
|
|
import { RuleOptions } from '../../MdToHtml';
|
2023-08-21 19:37:33 +02:00
|
|
|
import { attributesHtml } from '../../htmlUtils';
|
2024-01-18 13:20:10 +02:00
|
|
|
import * as utils from '../../utils';
|
2023-02-17 15:13:28 +02:00
|
|
|
import createEventHandlingAttrs from '../createEventHandlingAttrs';
|
2020-01-30 23:05:23 +02:00
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
function plugin(markdownIt: any, ruleOptions: RuleOptions) {
|
2020-01-30 23:05:23 +02:00
|
|
|
const defaultRender = markdownIt.renderer.rules.image;
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
markdownIt.renderer.rules.image = (tokens: any[], idx: number, options: any, env: any, self: any) => {
|
2020-01-30 23:05:23 +02:00
|
|
|
const Resource = ruleOptions.ResourceModel;
|
|
|
|
|
|
|
|
const token = tokens[idx];
|
|
|
|
const src = utils.getAttr(token.attrs, 'src');
|
|
|
|
const title = utils.getAttr(token.attrs, 'title');
|
|
|
|
|
|
|
|
if (!Resource.isResourceUrl(src) || ruleOptions.plainResourceRendering) return defaultRender(tokens, idx, options, env, self);
|
|
|
|
|
2024-07-16 20:25:23 +02:00
|
|
|
const alt = token.content;
|
|
|
|
const r = utils.imageReplacement(ruleOptions.ResourceModel, { src, alt, title }, ruleOptions.resources, ruleOptions.resourceBaseUrl, ruleOptions.itemIdToUrl);
|
2020-01-30 23:05:23 +02:00
|
|
|
if (typeof r === 'string') return r;
|
2020-10-12 11:25:59 +02:00
|
|
|
if (r) {
|
2023-02-17 15:13:28 +02:00
|
|
|
const id = r['data-resource-id'];
|
|
|
|
|
2023-10-02 16:15:51 +02:00
|
|
|
// Show the edit popup if any MIME type matches that in editPopupFiletypes
|
|
|
|
const mimeType = ruleOptions.resources[id]?.item?.mime?.toLowerCase();
|
|
|
|
const enableEditPopup = ruleOptions.editPopupFiletypes?.some(showForMime => mimeType === showForMime);
|
|
|
|
|
2023-02-17 15:13:28 +02:00
|
|
|
const js = createEventHandlingAttrs(id, {
|
|
|
|
enableLongPress: ruleOptions.enableLongPress ?? false,
|
|
|
|
postMessageSyntax: ruleOptions.postMessageSyntax ?? 'void',
|
2023-10-02 16:15:51 +02:00
|
|
|
|
|
|
|
enableEditPopup,
|
|
|
|
createEditPopupSyntax: ruleOptions.createEditPopupSyntax,
|
|
|
|
destroyEditPopupSyntax: ruleOptions.destroyEditPopupSyntax,
|
2023-02-17 15:13:28 +02:00
|
|
|
}, null);
|
|
|
|
|
2024-07-16 20:25:23 +02:00
|
|
|
return `<img data-from-md ${attributesHtml({ ...r, title: title, alt })} ${js}/>`;
|
2020-10-12 11:25:59 +02:00
|
|
|
}
|
2020-01-30 23:05:23 +02:00
|
|
|
return defaultRender(tokens, idx, options, env, self);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-21 01:23:55 +02:00
|
|
|
export default { plugin };
|