2017-11-23 03:55:00 -05:00
|
|
|
export function setup(helper) {
|
2017-12-05 06:30:13 -05:00
|
|
|
const opts = helper.getOptions();
|
|
|
|
|
|
|
|
if (opts.previewing && opts.injectLineNumbersToPreview) {
|
2020-10-27 22:22:06 -04:00
|
|
|
helper.allowList([
|
2017-11-23 03:55:00 -05:00
|
|
|
"p.preview-sync-line",
|
|
|
|
"p[data-line-number]",
|
|
|
|
"h1.preview-sync-line",
|
|
|
|
"h1[data-line-number]",
|
|
|
|
"h2.preview-sync-line",
|
|
|
|
"h2[data-line-number]",
|
|
|
|
"h3.preview-sync-line",
|
|
|
|
"h3[data-line-number]",
|
|
|
|
"h4.preview-sync-line",
|
|
|
|
"h4[data-line-number]",
|
|
|
|
"h5.preview-sync-line",
|
|
|
|
"h5[data-line-number]",
|
|
|
|
"h6.preview-sync-line",
|
|
|
|
"h6[data-line-number]",
|
|
|
|
"blockquote.preview-sync-line",
|
|
|
|
"blockquote[data-line-number]",
|
|
|
|
"hr.preview-sync-line",
|
|
|
|
"hr[data-line-number]",
|
|
|
|
"ul.preview-sync-line",
|
|
|
|
"ul[data-line-number]",
|
|
|
|
"ol.preview-sync-line",
|
|
|
|
"ol[data-line-number]",
|
|
|
|
]);
|
|
|
|
|
|
|
|
helper.registerPlugin((md) => {
|
|
|
|
const injectLineNumber = (tokens, index, options, env, self) => {
|
|
|
|
let line;
|
2017-11-24 01:57:36 -05:00
|
|
|
const token = tokens[index];
|
2017-11-23 03:55:00 -05:00
|
|
|
|
2017-11-24 01:57:36 -05:00
|
|
|
if (token.map && token.level === 0) {
|
|
|
|
line = token.map[0];
|
|
|
|
token.attrJoin("class", "preview-sync-line");
|
|
|
|
token.attrSet("data-line-number", String(line));
|
2017-11-23 03:55:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return self.renderToken(tokens, index, options, env, self);
|
|
|
|
};
|
|
|
|
|
|
|
|
md.renderer.rules.paragraph_open = injectLineNumber;
|
|
|
|
md.renderer.rules.heading_open = injectLineNumber;
|
|
|
|
md.renderer.rules.blockquote_open = injectLineNumber;
|
|
|
|
md.renderer.rules.hr = injectLineNumber;
|
|
|
|
md.renderer.rules.ordered_list_open = injectLineNumber;
|
|
|
|
md.renderer.rules.bullet_list_open = injectLineNumber;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|