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';
|
2020-01-30 23:05:23 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
function renderImageHtml(before: string, src: string, after: string, ruleOptions: RuleOptions) {
|
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;
|
2023-08-21 19:37:33 +02:00
|
|
|
if (r) return `<img ${before} ${attributesHtml(r)} ${after}/>`;
|
2020-01-30 23:05:23 +02:00
|
|
|
return `[Image: ${src}]`;
|
|
|
|
}
|
|
|
|
|
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 Resource = ruleOptions.ResourceModel;
|
|
|
|
|
|
|
|
const htmlBlockDefaultRender =
|
|
|
|
markdownIt.renderer.rules.html_block ||
|
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(tokens: any[], idx: number, options: any, _env: any, self: any) {
|
2020-01-30 23:05:23 +02:00
|
|
|
return self.renderToken(tokens, idx, options);
|
|
|
|
};
|
|
|
|
|
|
|
|
const htmlInlineDefaultRender =
|
|
|
|
markdownIt.renderer.rules.html_inline ||
|
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(tokens: any[], idx: number, options: any, _env: any, self: any) {
|
2020-01-30 23:05:23 +02:00
|
|
|
return self.renderToken(tokens, idx, options);
|
|
|
|
};
|
|
|
|
|
|
|
|
const imageRegex = /<img(.*?)src=["'](.*?)["'](.*?)>/gi;
|
|
|
|
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
const handleImageTags = function(defaultRender: Function) {
|
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
|
|
|
return function(tokens: any[], idx: number, options: any, env: any, self: any) {
|
2020-01-30 23:05:23 +02:00
|
|
|
const token = tokens[idx];
|
|
|
|
const content = token.content;
|
|
|
|
|
|
|
|
if (!content.match(imageRegex)) return defaultRender(tokens, idx, options, env, self);
|
|
|
|
|
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
|
|
|
return content.replace(imageRegex, (_v: any, before: string, src: string, after: string) => {
|
2020-03-06 02:54:21 +02:00
|
|
|
if (!Resource.isResourceUrl(src)) return `<img${before}src="${src}"${after}>`;
|
2020-01-30 23:05:23 +02:00
|
|
|
return renderImageHtml(before, src, after, ruleOptions);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// It seems images sometimes are inline, sometimes a block
|
|
|
|
// to make sure they both render correctly.
|
|
|
|
markdownIt.renderer.rules.html_block = handleImageTags(htmlBlockDefaultRender);
|
|
|
|
markdownIt.renderer.rules.html_inline = handleImageTags(htmlInlineDefaultRender);
|
|
|
|
}
|
|
|
|
|
2020-10-24 01:14:30 +02:00
|
|
|
export default { plugin };
|