2020-11-05 18:58:23 +02:00
|
|
|
import { RuleOptions } from '../../MdToHtml';
|
2020-11-20 18:04:47 +02:00
|
|
|
import linkReplacement from '../linkReplacement';
|
2024-01-18 13:20:10 +02:00
|
|
|
import * as utils from '../../utils';
|
2020-10-21 01:23:55 +02:00
|
|
|
|
2020-01-30 23:05:23 +02:00
|
|
|
const urlUtils = require('../../urlUtils.js');
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
function plugin(markdownIt: any, ruleOptions: RuleOptions) {
|
|
|
|
markdownIt.renderer.rules.link_open = function(tokens: any[], idx: number) {
|
2020-01-30 23:05:23 +02:00
|
|
|
const token = tokens[idx];
|
2020-11-20 18:04:47 +02:00
|
|
|
const href = utils.getAttr(token.attrs, 'href');
|
2020-01-30 23:05:23 +02:00
|
|
|
const resourceHrefInfo = urlUtils.parseResourceUrl(href);
|
2020-07-15 00:27:12 +02:00
|
|
|
const isResourceUrl = ruleOptions.resources && !!resourceHrefInfo;
|
2020-11-20 18:04:47 +02:00
|
|
|
const title = utils.getAttr(token.attrs, 'title', isResourceUrl ? '' : href);
|
|
|
|
|
2020-12-09 23:30:51 +02:00
|
|
|
const replacement = linkReplacement(href, {
|
2020-11-20 18:04:47 +02:00
|
|
|
title,
|
|
|
|
resources: ruleOptions.resources,
|
|
|
|
ResourceModel: ruleOptions.ResourceModel,
|
|
|
|
linkRenderingType: ruleOptions.linkRenderingType,
|
|
|
|
plainResourceRendering: ruleOptions.plainResourceRendering,
|
|
|
|
postMessageSyntax: ruleOptions.postMessageSyntax,
|
|
|
|
enableLongPress: ruleOptions.enableLongPress,
|
2021-01-29 20:45:11 +02:00
|
|
|
itemIdToUrl: ruleOptions.itemIdToUrl,
|
2020-11-20 18:04:47 +02:00
|
|
|
});
|
2020-12-09 23:30:51 +02:00
|
|
|
|
|
|
|
ruleOptions.context.currentLinks.push({
|
|
|
|
href: href,
|
|
|
|
resource: replacement.resource,
|
|
|
|
resourceReady: replacement.resourceReady,
|
|
|
|
resourceFullPath: replacement.resourceFullPath,
|
|
|
|
});
|
|
|
|
|
|
|
|
return replacement.html;
|
2020-01-30 23:05:23 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-21 01:23:55 +02:00
|
|
|
export default { plugin };
|