Skip to content

marked.use - #1646

Merged
UziTech merged 6 commits into
markedjs:masterfrom
UziTech:use-extension
Apr 20, 2020
Merged

marked.use#1646
UziTech merged 6 commits into
markedjs:masterfrom
UziTech:use-extension

Conversation

@UziTech

@UziTech UziTech commented Apr 19, 2020

Copy link
Copy Markdown
Member

Marked version: master

Description

Add function marked.use which takes an options object and merges it into marked options.

The renderer and tokenizer 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.

const extension = {
	breaks: true,
	renderer: {
		html(html) {
			if (html.startsWith('<div')) {
				return html.replace(/</g, '&lt;').replace(/>/g, '&gt;');
			}
			return false;
		}
	}
};
marked.use(extension);
const html = marked(`
<div>
html renderer will use extension
</div>

<span>will fallback to regular html renderer</span>

options from extension
will be used.
`);
console.log(html);
/* will log:
&lt;div&gt;
html renderer will use extension
&lt;/div&gt;

<span>will fallback to regular html renderer</span>

<p>options from extension<br>will be used.</p>

*/

Contributor

  • Test(s) exist to ensure functionality and minimize regression (if no tests added, list tests covering this PR); or,
  • no tests required for this PR.
  • If submitting new feature, it has been documented in the appropriate places.

Committer

In most cases, this should be a different person than the contributor.

  • Draft GitHub release notes have been updated.
  • CI is green (no forced merge required).
  • Merge PR

@vercel

vercel Bot commented Apr 19, 2020

Copy link
Copy Markdown

This pull request is being automatically deployed with ZEIT Now (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://zeit.co/markedjs/markedjs/2i2a4afnt
✅ Preview: https://markedjs-git-fork-uzitech-use-extension.markedjs.now.sh

@davisjam davisjam left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall. Perhaps add a sentence in USING_PRO.md to clarify the use case?

@joshbruce joshbruce left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate this solution, agree with @davisjam request for updates to docs, which seems to have happened.

Does this only work for a custom renderer at this time? Is this a reusable pattern for extending other sub-components of Marked?

@UziTech

UziTech commented Apr 20, 2020

Copy link
Copy Markdown
Member Author

This can be used to extend any option in marked. The renderer and tokenizer will be merged but all other options will overwrite previous options.

@UziTech

UziTech commented Apr 20, 2020

Copy link
Copy Markdown
Member Author

@davisjam I added a section explaining marked.use

@UziTech

UziTech commented Apr 20, 2020

Copy link
Copy Markdown
Member Author

This PR doesn't change current ways of extending marked with marked.setOptions(options) or adding options as a second parameter to marked(md, options).

It just adds a new method (marked.use(options)) that will merge the tokenizer and renderer instead of overriding them completely. This method should be recommended over other ways of changing options and maybe in the future we can change other ways to merging them as well.

@davisjam davisjam left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks

@UziTech

UziTech commented Apr 20, 2020

Copy link
Copy Markdown
Member Author

The idea is to create extensions in the form

// some-marked-extension
module.exports = {
	renderer: {...},
	tokenizer: {...}
};
// some-other-marked-extension
module.exports = {
	renderer: {...},
	tokenizer: {...}
};

that can be used by users without overriding eachother

const marked = require('marked');
marked.use(require('some-marked-extension'));
marked.use(require('some-other-marked-extension'));
...

Comment thread src/marked.js
Comment thread docs/USING_PRO.md

@styfle styfle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work 🎉

@UziTech
UziTech merged commit 1bfbd9e into markedjs:master Apr 20, 2020
@UziTech
UziTech deleted the use-extension branch April 20, 2020 18:29
@UziTech UziTech mentioned this pull request Apr 20, 2020
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants