|
|
@@ -2,43 +2,33 @@ export default class HeaderLineNumberConfigurer {
|
|
|
|
|
|
constructor(crowi) {
|
|
|
this.crowi = crowi;
|
|
|
-
|
|
|
- this.injectLineNumbers = this.injectLineNumbers.bind(this);
|
|
|
- this.combineRules = this.combineRules.bind(this);
|
|
|
+ this.firstLine = 0;
|
|
|
}
|
|
|
|
|
|
configure(md) {
|
|
|
- const rules = md.renderer.rules;
|
|
|
- const headingOpenOrg = rules.heading_open;
|
|
|
- const paragraphOpenOrg = rules.paragraph_open;
|
|
|
- // combine rule and set
|
|
|
- rules.heading_open = this.combineRules(this.injectLineNumbers, headingOpenOrg);
|
|
|
- rules.paragraph_open = this.combineRules(this.injectLineNumbers, paragraphOpenOrg);
|
|
|
+ for (const renderName of ['paragraph_open', 'heading_open', 'image', 'code_block', 'blockquote_open', 'list_item_open']) {
|
|
|
+ this.addLineNumberRenderer(md, renderName);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Inject line numbers for sync scroll
|
|
|
- * @see https://github.com/markdown-it/markdown-it/blob/e6f19eab4204122e85e4a342e0c1c8486ff40c2d/support/demo_template/index.js#L169
|
|
|
+ * Add line numbers for sync scroll
|
|
|
+ * @see https://github.com/Microsoft/vscode/blob/6e8d4d057bd1152d49a1e9780ec6db6363593855/extensions/markdown/src/markdownEngine.ts#L118
|
|
|
*/
|
|
|
- injectLineNumbers(tokens, idx, options, env, slf) {
|
|
|
- var line;
|
|
|
- if (tokens[idx].map && tokens[idx].level === 0) {
|
|
|
- line = tokens[idx].map[0] + 1; // add 1 to convert to line number
|
|
|
- tokens[idx].attrJoin('class', 'line');
|
|
|
- tokens[idx].attrSet('data-line', String(line));
|
|
|
- }
|
|
|
- return slf.renderToken(tokens, idx, options, env, slf);
|
|
|
- }
|
|
|
+ addLineNumberRenderer(md, ruleName) {
|
|
|
+ const original = md.renderer.rules[ruleName];
|
|
|
+ md.renderer.rules[ruleName] = (tokens, idx, options, env, self) => {
|
|
|
+ const token = tokens[idx];
|
|
|
+ if (token.map && token.map.length) {
|
|
|
+ token.attrSet('data-line', this.firstLine + token.map[0]);
|
|
|
+ token.attrJoin('class', 'code-line');
|
|
|
+ }
|
|
|
|
|
|
- combineRules(rule1, rule2) {
|
|
|
- return (tokens, idx, options, env, slf) => {
|
|
|
- if (rule1 != null) {
|
|
|
- rule1(tokens, idx, options, env, slf);
|
|
|
- }
|
|
|
- if (rule2 != null) {
|
|
|
- rule2(tokens, idx, options, env, slf);
|
|
|
- }
|
|
|
- return slf.renderToken(tokens, idx, options, env, slf);
|
|
|
- }
|
|
|
- }
|
|
|
+ if (original) {
|
|
|
+ return original(tokens, idx, options, env, self);
|
|
|
+ } else {
|
|
|
+ return self.renderToken(tokens, idx, options, env, self);
|
|
|
+ }
|
|
|
+ };
|
|
|
+}
|
|
|
}
|