简体   繁体   中英

Render tokens in Markdown-it?

I would like to ignore some tokens conditionally so I wrote:

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)) // <----- 

I get the error:

Error TypeError: Cannot read property 'breaks' of undefined

In renderer.js:108

default_rules.softbreak = function (tokens, idx, options /*, env */) {
  return options.breaks ? (options.xhtmlOut ? '<br />\n' : '<br>\n') : '\n'; // <----
};

Is it possible to render markdown in two steps?

markdown -1-> tokens -2-> html

In this example I will have the following tokens:

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, …} 

After detecting the h2 that I want to remove I get this:

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, …} 

You don't need to instantiate another renderer. Your issue was that render requires the options from md :

tokens = md.parse(markdown)
md.renderer.render(tokens, md.options)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM