diff --git a/docs/USING_PRO.md b/docs/USING_PRO.md index 0b2aea001a..40aadea661 100644 --- a/docs/USING_PRO.md +++ b/docs/USING_PRO.md @@ -2,6 +2,16 @@ To champion the single-responsibility and open/closed principles, we have tried to make it relatively painless to extend marked. If you are looking to add custom functionality, this is the place to start. +

marked.use()

+ +`marked.use(options)` is the recommended way to extend marked. The options object can contain any [option](#/USING_ADVANCED.md#options) available in marked. + +The `renderer` and `tokenizer` options can be an object with functions that will be merged into the `renderer` and `tokenizer` respectively. + +The `renderer` and `tokenizer` functions can return false to fallback to the previous function. + +All other options will overwrite previously set options. +

The renderer

The renderer defines the output of the parser. @@ -12,24 +22,25 @@ The renderer defines the output of the parser. // Create reference instance const marked = require('marked'); -// Get reference -const renderer = new marked.Renderer(); - // Override function -renderer.heading = function(text, level) { - const escapedText = text.toLowerCase().replace(/[^\w]+/g, '-'); - - return ` - - - - - ${text} - `; +const renderer = { + heading(text, level) { + const escapedText = text.toLowerCase().replace(/[^\w]+/g, '-'); + + return ` + + + + + ${text} + `; + } }; +marked.use({ renderer }); + // Run marked -console.log(marked('# heading+', { renderer })); +console.log(marked('# heading+')); ``` **Output:** @@ -99,30 +110,34 @@ The tokenizer defines how to turn markdown text into tokens. // Create reference instance const marked = require('marked'); -// Get reference -const tokenizer = new marked.Tokenizer(); -const originalCodespan = tokenizer.codespan; // Override function -tokenizer.codespan = function(src) { - const match = src.match(/\$+([^\$\n]+?)\$+/); - if (match) { - return { - type: 'codespan', - raw: match[0], - text: match[1].trim() - }; +const tokenizer = { + codespan(src) { + const match = src.match(/\$+([^\$\n]+?)\$+/); + if (match) { + return { + type: 'codespan', + raw: match[0], + text: match[1].trim() + }; + } + + // return false to use original codespan tokenizer + return false; } - return originalCodespan.apply(this, arguments); }; +marked.use({ tokenizer }); + // Run marked -console.log(marked('$ latex code $', { tokenizer })); +console.log(marked('$ latex code $\n\n` other code `')); ``` **Output:** ```html -

latext code

+

latex code

+

other code

``` ### Block level tokenizer methods diff --git a/docs/index.html b/docs/index.html index 9d68e7bb41..a7de1818c0 100644 --- a/docs/index.html +++ b/docs/index.html @@ -154,6 +154,7 @@

Marked.js Documentation

  • Extensibility