2021-11-03 14:10:46 +02:00
|
|
|
export default {
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2021-11-03 14:10:46 +02:00
|
|
|
plugin: (markdownIt: any, params: any) => {
|
|
|
|
|
|
|
|
if (!params.mapsToLine) return;
|
|
|
|
|
|
|
|
const allowedLevels = {
|
|
|
|
paragraph_open: 0,
|
|
|
|
heading_open: 0,
|
2024-02-26 12:16:23 +02:00
|
|
|
// fence: 0, // fence uses custom rendering that doesn't propagate attr so it can't be used for now
|
2021-11-03 14:10:46 +02:00
|
|
|
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];
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2021-11-03 14:10:46 +02:00
|
|
|
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];
|
2022-04-10 12:31:17 +02:00
|
|
|
const lineEnd = tokens[idx].map[1];
|
2021-11-03 14:10:46 +02:00
|
|
|
tokens[idx].attrJoin('class', 'maps-to-line');
|
|
|
|
tokens[idx].attrSet('source-line', `${line}`);
|
2022-04-10 12:31:17 +02:00
|
|
|
tokens[idx].attrSet('source-line-end', `${lineEnd}`);
|
2021-11-03 14:10:46 +02:00
|
|
|
}
|
|
|
|
if (precedentRule) {
|
|
|
|
return precedentRule(tokens, idx, options, env, self);
|
|
|
|
} else {
|
|
|
|
return self.renderToken(tokens, idx, options);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|