我想有条件地忽略一些标记,所以我写道:
var md = require("markdown-it")();
var mk = require("@iktakahiro/markdown-it-katex");
var mkit = require("markdown-it");
md.use(mk);
import Renderer from "markdown-it/lib/renderer";
let renderer = new Renderer()
markdown = `
# Title
## Ignored section
I am ignored...
## Other section
...
`
tokens = md.parse(markdown)
tokens = process_tokens(tokens)
console.log(renderer.render(token)) // <-----
我得到错误:
Error TypeError: Cannot read property 'breaks' of undefined
在renderer.js:108
default_rules.softbreak = function (tokens, idx, options /*, env */) {
return options.breaks ? (options.xhtmlOut ? '<br />\n' : '<br>\n') : '\n'; // <----
};
是否可以分两步渲染markdown?
markdown -1-> tokens -2-> html
在此示例中,我将拥有以下标记:
0: Token {type: "heading_open", tag: "h1", attrs: null, map: Array(2), nesting: 1, …}
1: Token {type: "inline", tag: "", attrs: null, map: Array(2), nesting: 0, …}
2: Token {type: "heading_close", tag: "h1", attrs: null, map: null, nesting: -1, …}
3: Token {type: "heading_open", tag: "h2", attrs: null, map: Array(2), nesting: 1, …}
4: Token {type: "inline", tag: "", attrs: null, map: Array(2), nesting: 0, …}
5: Token {type: "heading_close", tag: "h2", attrs: null, map: null, nesting: -1, …}
6: Token {type: "paragraph_open", tag: "p", attrs: null, map: Array(2), nesting: 1, …}
7: Token {type: "inline", tag: "", attrs: null, map: Array(2), nesting: 0, …}
8: Token {type: "paragraph_close", tag: "p", attrs: null, map: null, nesting: -1, …}
9: Token {type: "heading_open", tag: "h2", attrs: null, map: Array(2), nesting: 1, …}
10: Token {type: "inline", tag: "", attrs: null, map: Array(2), nesting: 0, …}
11: Token {type: "heading_close", tag: "h2", attrs: null, map: null, nesting: -1, …}
12: Token {type: "paragraph_open", tag: "p", attrs: null, map: Array(2), nesting: 1, …}
13: Token {type: "inline", tag: "", attrs: null, map: Array(2), nesting: 0, …}
14: Token {type: "paragraph_close", tag: "p", attrs: null, map: null, nesting: -1, …}
在检测到h2
我要删除的内容后,我得到了这个:
0: Token {type: "heading_open", tag: "h1", attrs: null, map: Array(2), nesting: 1, …}
1: Token {type: "inline", tag: "", attrs: null, map: Array(2), nesting: 0, …}
2: Token {type: "heading_close", tag: "h1", attrs: null, map: null, nesting: -1, …}
3: Token {type: "heading_open", tag: "h2", attrs: null, map: Array(2), nesting: 1, …}
4: Token {type: "inline", tag: "", attrs: null, map: Array(2), nesting: 0, …}
5: Token {type: "heading_close", tag: "h2", attrs: null, map: null, nesting: -1, …}
6: Token {type: "paragraph_open", tag: "p", attrs: null, map: Array(2), nesting: 1, …}
7: Token {type: "inline", tag: "", attrs: null, map: Array(2), nesting: 0, …}
8: Token {type: "paragraph_close", tag: "p", attrs: null, map: null, nesting: -1, …}