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';
|
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);
|
|
|
|
|
2020-08-18 22:52:00 +02:00
|
|
|
const r = utils.imageReplacement(ruleOptions.ResourceModel, src, ruleOptions.resources, ruleOptions.resourceBaseUrl);
|
2020-01-30 23:05:23 +02:00
|
|
|
if (typeof r === 'string') return r;
|
2020-10-12 11:25:59 +02:00
|
|
|
if (r) {
|
|
|
|
let js = '';
|
|
|
|
if (ruleOptions.enableLongPress) {
|
|
|
|
const id = r['data-resource-id'];
|
2020-01-30 23:05:23 +02:00
|
|
|
|
2020-10-12 11:25:59 +02:00
|
|
|
const longPressHandler = `${ruleOptions.postMessageSyntax}('longclick:${id}')`;
|
2020-11-20 18:04:47 +02:00
|
|
|
const touchStart = `t=setTimeout(()=>{t=null; ${longPressHandler};}, ${utils.longPressDelay});`;
|
2020-10-20 12:48:10 +02:00
|
|
|
const cancel = 'if (!!t) clearTimeout(t); t=null';
|
2020-10-12 11:25:59 +02:00
|
|
|
|
2020-10-20 12:48:10 +02:00
|
|
|
js = ` ontouchstart="${touchStart}" ontouchend="${cancel}" ontouchcancel="${cancel}" ontouchmove="${cancel}"`;
|
2020-10-12 11:25:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return `<img data-from-md ${htmlUtils.attributesHtml(Object.assign({}, r, { title: title }))}${js}/>`;
|
|
|
|
}
|
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 };
|