-
Notifications
You must be signed in to change notification settings - Fork 3.5k
docs: clarify extension types (#3182) #3797
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
Open
rinas21
wants to merge
2
commits into
markedjs:master
Choose a base branch
from
rinas21:hacktoberfest-fix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+214
−16
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| # Extension Types in Marked.js | ||
|
|
||
| This document clarifies the different types of extensions in marked.js to help developers understand the API better. | ||
|
|
||
| ## Two Types of Extensions | ||
|
|
||
| Marked.js has two distinct concepts that are both called "extensions" in the documentation, which can cause confusion: | ||
|
|
||
| ### 1. MarkedExtensions (Configuration Objects) | ||
|
|
||
| **Type**: `MarkedExtension` interface | ||
| **Usage**: Passed to `marked.use()` or `new Marked()` | ||
| **Purpose**: Configuration objects that can contain various options and settings | ||
|
|
||
| ```javascript | ||
| // MarkedExtension examples | ||
| marked.use({ | ||
| gfm: true, | ||
| breaks: false, | ||
| pedantic: false | ||
| }); | ||
|
|
||
| marked.use({ | ||
| renderer: { | ||
| heading(text, level) { | ||
| return `<h${level}>${text}</h${level}>`; | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| marked.use({ | ||
| extensions: [/* SyntaxExtensions go here */] | ||
| }); | ||
| ``` | ||
|
|
||
| ### 2. SyntaxExtensions (Custom Parsing Logic) | ||
|
|
||
| **Type**: `SyntaxExtension` interface | ||
| **Usage**: Objects inside the `extensions` array of a MarkedExtension | ||
| **Purpose**: Define custom tokenizers and renderers for new syntax | ||
|
|
||
| ```javascript | ||
| // SyntaxExtension example | ||
| const customExtension = { | ||
| name: 'customBlock', | ||
| level: 'block', | ||
| start: (src) => src.match(/:::/)?.index, | ||
| tokenizer(src) { | ||
| const match = src.match(/^:::\s*(\w+)\s*\n([\s\S]*?)\n:::/); | ||
| if (match) { | ||
| return { | ||
| type: 'customBlock', | ||
| raw: match[0], | ||
| name: match[1], | ||
| content: match[2] | ||
| }; | ||
| } | ||
| }, | ||
| renderer(token) { | ||
| return `<div class="custom-${token.name}">${token.content}</div>`; | ||
| } | ||
| }; | ||
|
|
||
| // Using the SyntaxExtension | ||
| marked.use({ | ||
| extensions: [customExtension] // This is a MarkedExtension with SyntaxExtensions | ||
| }); | ||
| ``` | ||
|
|
||
| ## API Signatures Clarified | ||
|
|
||
| ### 1. `new Marked(extension, extension, extension)` | ||
| - Each `extension` parameter is a **MarkedExtension** | ||
| - Can contain any MarkedExtension options including `extensions` array | ||
|
|
||
| ### 2. `marked.use(extension)` | ||
| - The `extension` parameter is a **MarkedExtension** | ||
| - Can contain any MarkedExtension options including `extensions` array | ||
|
|
||
| ### 3. `marked.use({ extensions: [SyntaxExtensions] })` | ||
| - The outer object is a **MarkedExtension** | ||
| - The `extensions` property contains an array of **SyntaxExtensions** | ||
|
|
||
| ## Clear Terminology | ||
|
|
||
| To avoid confusion, we recommend using these prefixes in documentation: | ||
|
|
||
| - **MarkedExtension**: Configuration objects for `marked.use()` | ||
| - **SyntaxExtension**: Custom parsing logic objects for the `extensions` array | ||
| - **Extension Options**: The `extensions` property within a MarkedExtension | ||
|
|
||
| ## Examples of Clear Usage | ||
|
|
||
| ```javascript | ||
| // MarkedExtension with various options | ||
| const markdownConfig = { | ||
| gfm: true, | ||
| breaks: false, | ||
| renderer: { | ||
| // custom renderer methods | ||
| } | ||
| }; | ||
|
|
||
| // MarkedExtension with SyntaxExtensions | ||
| const customSyntaxConfig = { | ||
| extensions: [ | ||
| { | ||
| name: 'alert', | ||
| level: 'block', | ||
| start: (src) => src.match(/^!!!\s/)?.index, | ||
| tokenizer(src) { | ||
| // custom tokenizer logic | ||
| }, | ||
| renderer(token) { | ||
| // custom renderer logic | ||
| } | ||
| } | ||
| ] | ||
| }; | ||
|
|
||
| // Using both | ||
| marked.use(markdownConfig); | ||
| marked.use(customSyntaxConfig); | ||
| ``` | ||
|
|
||
| ## Migration from Previous Versions | ||
|
|
||
| When upgrading from older versions of marked.js, you may need to separate your configuration: | ||
|
|
||
| ```javascript | ||
| // Old way (confusing) | ||
| const config = { | ||
| gfm: true, | ||
| extensions: [customExtension1, customExtension2] | ||
| }; | ||
|
|
||
| // New way (clear separation) | ||
| const markdownOptions = { | ||
| gfm: true | ||
| }; | ||
|
|
||
| const customExtensions = { | ||
| extensions: [customExtension1, customExtension2] | ||
| }; | ||
|
|
||
| marked.use(markdownOptions); | ||
| marked.use(customExtensions); | ||
| ``` | ||
|
|
||
| This separation makes it clear which parts are general configuration and which are custom parsing extensions. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do encourage setting them together. Often an external extension will set options and add syntax extensions and we want them to be easy to use.
I think we should remove this section