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

add walkTokens and fix highlight async #1664

Merged
merged 7 commits into from May 14, 2020
Merged

add walkTokens and fix highlight async #1664

merged 7 commits into from May 14, 2020

Conversation

UziTech
Copy link
Member

@UziTech UziTech commented May 4, 2020

Marked version: 1.0.0

Description

Using an async highlighting function was broken in v1.0.0 when we moved block tokens in blockquote and lists from the top level of the tokens array to a tokens property on the blockquote or listitem token.

This PR adds a public method to easily iterate over all tokens and send each token to a function specified by the user (marked.walkTokens(tokens, callback)) and uses that function to highlight code blocks asynchronously.

This also adds a walkTokens option so extensions can use it to run a function for each token.

Example: Overriding heading tokens to start at h2.

const marked = require('marked');

// Override function
const walkTokens = (token) => {
  if (token.type === 'heading') {
    token.depth += 1;
  }
};

marked.use({ walkTokens });

// Run marked
console.log(marked('# heading 2\n\n## heading 3'));

Output:

<h2 id="heading-2">heading 2</h2>
<h3 id="heading-3">heading 3</h3>

Contributor

  • Test(s) exist to ensure functionality and minimize regression
  • 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 May 4, 2020

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

🔍 Inspect: https://vercel.com/markedjs/markedjs/1zxn2ys77
✅ Preview: https://markedjs-git-fork-uzitech-fix-highlight-async.markedjs.now.sh

src/marked.js Outdated Show resolved Hide resolved
src/marked.js Show resolved Hide resolved
src/marked.js Outdated Show resolved Hide resolved
src/marked.js Outdated Show resolved Hide resolved
marked.use(extension1);
marked.use(extension2);
marked('text');
expect(walkedOnce).toBe(2);
Copy link
Member

Choose a reason for hiding this comment

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

walkedOnce is 2? Maybe it should be renamed?

Copy link
Member Author

Choose a reason for hiding this comment

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

That variable counts how many tokens were walked at least once

Copy link
Contributor

@davisjam davisjam May 14, 2020

Choose a reason for hiding this comment

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

Perhaps nWalkedOnce and nWalkedTwice?

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.

Thanks!

@UziTech UziTech merged commit 5ef872b into markedjs:master May 14, 2020
@UziTech UziTech deleted the fix-highlight-async branch May 14, 2020 15:52
@UziTech UziTech mentioned this pull request May 16, 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.

highlight callback is undefined
3 participants