Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

marked.use #1646

Merged
merged 6 commits into from
Apr 20, 2020
Merged

marked.use #1646

merged 6 commits into from
Apr 20, 2020

Conversation

UziTech
Copy link
Member

@UziTech UziTech commented Apr 19, 2020

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
Copy link

vercel bot commented Apr 19, 2020

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

Copy link
Contributor

@davisjam davisjam left a comment

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?

Copy link
Member

@joshbruce joshbruce left a comment

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
Copy link
Member Author

UziTech commented Apr 20, 2020

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
Copy link
Member Author

UziTech commented Apr 20, 2020

@davisjam I added a section explaining marked.use

@UziTech
Copy link
Member Author

UziTech commented Apr 20, 2020

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.

Copy link
Contributor

@davisjam davisjam left a comment

Choose a reason for hiding this comment

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

LGTM, thanks

@UziTech
Copy link
Member Author

UziTech commented Apr 20, 2020

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'));
...

src/marked.js Show resolved Hide resolved
Copy link
Member

@styfle styfle left a comment

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