2020-11-05 18:58:23 +02:00
|
|
|
import { RuleOptions } from '../../MdToHtml';
|
2020-11-20 18:04:47 +02:00
|
|
|
import htmlUtils from '../../htmlUtils';
|
|
|
|
import utils from '../../utils';
|
2023-02-17 15:13:28 +02:00
|
|
|
import createEventHandlingAttrs from '../createEventHandlingAttrs';
|
2020-01-30 23:05:23 +02:00
|
|
|
|
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;
|
|
|
|
|
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);
|
|
|
|
|
2021-01-29 20:45:11 +02:00
|
|
|
const r = utils.imageReplacement(ruleOptions.ResourceModel, src, 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'];
|
|
|
|
|
|
|
|
const js = createEventHandlingAttrs(id, {
|
|
|
|
enableLongPress: ruleOptions.enableLongPress ?? false,
|
|
|
|
postMessageSyntax: ruleOptions.postMessageSyntax ?? 'void',
|
|
|
|
}, null);
|
|
|
|
|
2023-06-01 13:02:36 +02:00
|
|
|
return `<img data-from-md ${htmlUtils.attributesHtml({ ...r, title: title, alt: token.content })} ${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 };
|