turndown icon indicating copy to clipboard operation
turndown copied to clipboard

limiting the markdown syntax

Open lsmith77 opened this issue 4 years ago • 4 comments

right now I only want to support a minimal set of markdown rules (emphasis, lists and links).

I am not sure that I properly understood the approach I should take here.

lsmith77 avatar Jun 08 '20 15:06 lsmith77

would this require https://github.com/domchristie/turndown/issues/206 ?

lsmith77 avatar Jun 08 '20 15:06 lsmith77

ok this is my workaround to reduce the common markdown syntax

 const keepKeys = [
  1, // new line
  4, // list
  5, // list element
  9, // links
  10, // links
  11, // emphasis
  12, // emphasis
  14, // images
];
 turndownService.rules.array = turndownService.rules.array.filter((value, key) => keepKeys.includes(key));

lsmith77 avatar Jun 09 '20 16:06 lsmith77

Rather than digging into the internals, you may wish to try adding a custom rule to handle everything other than your subset. For example, something like:

turndownService.addRule({
  filter: function (node) {
    var subset = ['BR', 'UL', 'OL', 'LI', 'A', 'EM', 'IMG']
    return !subset.includes(node.nodeName)
  },
  replacement: function (content, node) {
    return node.outerHTML
  }
})

domchristie avatar Jun 09 '20 17:06 domchristie

hmm .. not managing to get this to work. one small issue is easy to fix. the rule needs a string parameter. but I then struggle to both ensure that the common rules for the tags I want are applied and that all the unwanted stuff like <meta> is stripped and other tags just the text is extracted.

I do realize that messing with internals like this isn't the way to go. would you be willing to accept a PR that allows me to either:

  • removeRule('identedCodeBlock')
  • filterRules(['linebreak', 'list', 'listItem])
  • optionally pass in the "common rules"

lsmith77 avatar Jun 11 '20 10:06 lsmith77