Marked: [Documentation Request]: custom parsing

Created on 18 Apr 2019  路  6Comments  路  Source: markedjs/marked

Describe the feature
I read the docs for modifying the renderer functions here (https://marked.js.org/#/USING_PRO.md#lexer)

From which it is unclear to me how I could your libraries excellent md parsing and rendering, but add a custom change as needed.

For example

const md = `
# Markdown to be Parsed
should be parsed as expected with the supported flavors.

**However** it would be nice if I could add some extra [[flavor]] by specifying unique
regex expressions to [[change]] behavior
`
const myMDExtension = {
  re: /\[\[(.+?)\]\]/gi , // captures [[<text here>]]
  render: (ptn) => `<flavorful name="${ptn}">${ptn}</flavorful>`
}

marked(md, {extensions: [myMDExtension]})

with the expected output:

<h1>Markdown to be Parsed</h1>
<p>should be parsed as expected with the supported flavors.</p>

<p>
<em>However</em> it would be nice if I could add some extra <flavorful name="flavor">flavor</flavorful> by specifying unique
regex expressions to <flavorful name="change">change</flavorful>behavior
</p>

Why is this feature necessary?
Quality of life feature

Describe alternatives you've considered
reparse parsed input to search for extra patterns

docs proposal

Most helpful comment

You can create a PR right from GitHub:

Go to the file you want to edit and click the pencil icon in the top right corner.

image

Once you are done editing the file, fill out the change description and click the Propose file change button at the bottom.

image

Then just follow the directions to fill out the PR form.

We can help you if you get stuck.

All 6 comments

Right now we are trying to get marked spec compliant and secure. One of the steps in our plan is to make marked easier to extend.

Right now the only way to extend the lexer is to create a wrapper around marked that replaces some of the parts.

example:

const marked = require("marked");

function newParser(markdown, options) {

  // parse markdown into tokens

  return marked.parser(lexed, options); 
}

module.exports = newParser;

In the mean time could you:

  1. provide a full example (e.g. based on what is above)

  2. improve the documentation for render functions and what the defaults are. If we want to add classes to a <p> tag for instance, I do not know what the default function is I am overwriting and this may have cascading effects if your presets are not included. Either link to link to line number where this code is (in the docs) or just provide example:

render.paragraph = (text) => `<p>${text}</p>`

PRs are welcome 馃槂馃憤

@UziTech I'm not at that level of git wizardry 馃檭 yet

You can create a PR right from GitHub:

Go to the file you want to edit and click the pencil icon in the top right corner.

image

Once you are done editing the file, fill out the change description and click the Propose file change button at the bottom.

image

Then just follow the directions to fill out the PR form.

We can help you if you get stuck.

This seems to have been resolved, now that the Extendability documentation has been filled out.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pigtooter picture pigtooter  路  4Comments

mjbvz picture mjbvz  路  4Comments

amejiarosario picture amejiarosario  路  3Comments

FireflyAndStars picture FireflyAndStars  路  3Comments

chunhei2008 picture chunhei2008  路  3Comments