1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-15 09:04:04 +02:00
joplin/packages/renderer/MdToHtml/rules/source_map.ts

37 lines
1.1 KiB
TypeScript

export default {
plugin: (markdownIt: any, params: any) => {
if (!params.mapsToLine) return;
const allowedLevels = {
paragraph_open: 0,
heading_open: 0,
// fence: 0, // fence uses custom rendering that doesn't propogate attr so it can't be used for now
blockquote_open: 0,
table_open: 0,
code_block: 0,
hr: 0,
html_block: 0,
list_item_open: 99, // this will stop matching if a list goes more than 99 indents deep
math_block: 0,
};
for (const [key, allowedLevel] of Object.entries(allowedLevels)) {
const precedentRule = markdownIt.renderer.rules[key];
markdownIt.renderer.rules[key] = (tokens: any[], idx: number, options: any, env: any, self: any) => {
if (!!tokens[idx].map && tokens[idx].level <= allowedLevel) {
const line = tokens[idx].map[0];
tokens[idx].attrJoin('class', 'maps-to-line');
tokens[idx].attrSet('source-line', `${line}`);
}
if (precedentRule) {
return precedentRule(tokens, idx, options, env, self);
} else {
return self.renderToken(tokens, idx, options);
}
};
}
},
};