26 changes: 19 additions & 7 deletions packages/renderer/MdToHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { ItemIdToUrlHandler } from './utils';
import { RenderResult, RenderResultPluginAsset } from './MarkupToHtml';
import { Options as NoteStyleOptions } from './noteStyle';
import hljs from './highlight';
import * as MarkdownIt from 'markdown-it';

const Entities = require('html-entities').AllHtmlEntities;
const htmlentities = new Entities().encode;
const MarkdownIt = require('markdown-it');
const md5 = require('md5');

export interface RenderOptions {
Expand All @@ -32,6 +32,7 @@ export interface RenderOptions {
useCustomPdfViewer?: boolean;
noteId?: string;
vendorDir?: string;
settingValue?: (pluginId: string, key: string)=> any;
}

interface RendererRule {
Expand All @@ -40,6 +41,7 @@ interface RendererRule {
plugin?: any;
assetPath?: string;
assetPathIsAbsolute?: boolean;
pluginId?: string;
}

interface RendererRules {
Expand Down Expand Up @@ -102,6 +104,7 @@ export interface ExtraRendererRule {
id: string;
module: any;
assetPath: string;
pluginId: string;
}

export interface Options {
Expand Down Expand Up @@ -233,7 +236,7 @@ export default class MdToHtml {

if (options.extraRendererRules) {
for (const rule of options.extraRendererRules) {
this.loadExtraRendererRule(rule.id, rule.assetPath, rule.module);
this.loadExtraRendererRule(rule.id, rule.assetPath, rule.module, rule.pluginId);
}
}

Expand Down Expand Up @@ -268,11 +271,13 @@ export default class MdToHtml {
}

// `module` is a file that has already been `required()`
public loadExtraRendererRule(id: string, assetPath: string, module: any) {
public loadExtraRendererRule(id: string, assetPath: string, module: any, pluginId: string) {
if (this.extraRendererRules_[id]) throw new Error(`A renderer rule with this ID has already been loaded: ${id}`);

this.extraRendererRules_[id] = {
...module,
assetPath,
pluginId: pluginId,
assetPathIsAbsolute: true,
};
}
Expand Down Expand Up @@ -451,6 +456,7 @@ export default class MdToHtml {
pdfViewerEnabled: this.pluginEnabled('pdfViewer'),

contentMaxWidth: 0,
settingValue: (_pluginId: string, _key: string) => { throw new Error('settingValue is not implemented'); },
...options,
};

Expand All @@ -467,8 +473,11 @@ export default class MdToHtml {
const cachedOutput = this.cachedOutputs_[cacheKey];
if (cachedOutput) return cachedOutput;

const ruleOptions = { ...options, resourceBaseUrl: this.resourceBaseUrl_,
ResourceModel: this.ResourceModel_ };
const ruleOptions = {
...options,
resourceBaseUrl: this.resourceBaseUrl_,
ResourceModel: this.ResourceModel_,
};

const context: PluginContext = {
css: {},
Expand All @@ -478,12 +487,12 @@ export default class MdToHtml {
currentLinks: [],
};

const markdownIt = new MarkdownIt({
const markdownIt: MarkdownIt = new MarkdownIt({
breaks: !this.pluginEnabled('softbreaks'),
typographer: this.pluginEnabled('typographer'),
linkify: this.pluginEnabled('linkify'),
html: true,
highlight: (str: string, lang: string) => {
highlight: (str: string, lang: string, _attrs: any): any => {
let outputCodeHtml = '';

// The strings includes the last \n that is part of the fence,
Expand Down Expand Up @@ -567,6 +576,9 @@ export default class MdToHtml {
context: context,
...ruleOptions,
...(ruleOptions.plugins[key] ? ruleOptions.plugins[key] : {}),
settingValue: (key: string) => {
return options.settingValue(rule.pluginId, key);
},
});
}

Expand Down
1 change: 1 addition & 0 deletions packages/renderer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"license": "AGPL-3.0-or-later",
"devDependencies": {
"@types/jest": "29.5.5",
"@types/markdown-it": "13.0.2",
"@types/node": "18.17.19",
"jest": "29.7.0",
"jest-environment-jsdom": "29.7.0",
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6668,6 +6668,7 @@ __metadata:
"@joplin/fork-uslug": ^1.0.11
"@joplin/utils": ~2.13
"@types/jest": 29.5.5
"@types/markdown-it": 13.0.2
"@types/node": 18.17.19
font-awesome-filetypes: 2.1.0
fs-extra: 11.1.1
Expand Down