Marked: Ignore certain rules defined by markdown.

Created on 2 Jul 2018  路  3Comments  路  Source: markedjs/marked

I want to convert only bold, italic, underline and link to markdown and ignore other rules mentioned by the lexer.
How do i achieve this?
I could not find a example on the extensibility page.
Can someone help me with this?
I want to ignore all rules except above mentioned four conditions.

NFE - new feature (should be an extension)

Most helpful comment

It could be really great to have an out-of-the-box solution for this scenario. For the time being, I settled on using the following approach:

const enabled = ['list', 'listitem', 'strong', 'em', 'paragraph', 'br'];
const renderer = new marked.TextRenderer();
const realRenderer = new marked.Renderer();
enabled.forEach((option) => {
  renderer[option] = realRenderer[option];
});

All 3 comments

You can create your own renderer and replace all other render functions to just return the text.

const marked = require('marked');
const renderer = new marked.Renderer();

renderer.code =
renderer.blockquote =
renderer.html =
renderer.heading =
renderer.hr =
renderer.list =
renderer.listitem =
renderer.checkbox =
renderer.paragraph =
renderer.table =
renderer.tablerow =
renderer.tablecell =
renderer.codespan =
renderer.br =
renderer.del =
renderer.image = function (text) {
  return text;
};

console.log(marked(markdownString, { renderer: renderer }));

It could be really great to have an out-of-the-box solution for this scenario. For the time being, I settled on using the following approach:

const enabled = ['list', 'listitem', 'strong', 'em', 'paragraph', 'br'];
const renderer = new marked.TextRenderer();
const realRenderer = new marked.Renderer();
enabled.forEach((option) => {
  renderer[option] = realRenderer[option];
});

Related: #420

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chunhei2008 picture chunhei2008  路  3Comments

FireflyAndStars picture FireflyAndStars  路  3Comments

james4388 picture james4388  路  3Comments

camwiegert picture camwiegert  路  4Comments

cusalvi picture cusalvi  路  3Comments