2020-12-09 23:30:51 +02:00
|
|
|
// This rule is used to add a media player for certain resource types below
|
|
|
|
// the link.
|
|
|
|
|
|
|
|
import { RuleOptions } from '../../MdToHtml';
|
|
|
|
import renderMedia, { Options as RenderMediaOptions } from '../renderMedia';
|
|
|
|
|
2022-08-27 14:32:20 +02:00
|
|
|
|
|
|
|
export interface LinkIndexes {
|
|
|
|
[key: string]: number;
|
|
|
|
}
|
|
|
|
|
2020-12-09 23:30:51 +02:00
|
|
|
function plugin(markdownIt: any, ruleOptions: RuleOptions) {
|
|
|
|
const defaultRender = markdownIt.renderer.rules.link_close || function(tokens: any, idx: any, options: any, _env: any, self: any) {
|
|
|
|
return self.renderToken(tokens, idx, options);
|
|
|
|
};
|
|
|
|
|
2022-08-27 14:32:20 +02:00
|
|
|
const linkIndexes: LinkIndexes = {};
|
|
|
|
|
2020-12-09 23:30:51 +02:00
|
|
|
markdownIt.renderer.rules.link_close = function(tokens: any[], idx: number, options: any, env: any, self: any) {
|
|
|
|
const defaultOutput = defaultRender(tokens, idx, options, env, self);
|
|
|
|
const link = ruleOptions.context.currentLinks.pop();
|
|
|
|
|
|
|
|
if (!link || ruleOptions.linkRenderingType === 2 || ruleOptions.plainResourceRendering) return defaultOutput;
|
|
|
|
|
2022-08-27 14:32:20 +02:00
|
|
|
return [defaultOutput, renderMedia(link, ruleOptions as RenderMediaOptions, linkIndexes)].join('');
|
2020-12-09 23:30:51 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default { plugin };
|