Skip to content

add walkTokens and fix highlight async - #1664

Merged
UziTech merged 7 commits into
markedjs:masterfrom
UziTech:fix-highlight-async
May 14, 2020
Merged

add walkTokens and fix highlight async#1664
UziTech merged 7 commits into
markedjs:masterfrom
UziTech:fix-highlight-async

Conversation

@UziTech

@UziTech UziTech commented May 4, 2020

Copy link
Copy Markdown
Member

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

vercel Bot commented May 4, 2020

Copy link
Copy Markdown

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

Comment thread src/marked.js Outdated
Comment thread src/marked.js
Comment thread src/marked.js Outdated
Comment thread src/marked.js Outdated
@UziTech
UziTech marked this pull request as ready for review May 13, 2020 01:02
@UziTech
UziTech requested review from davisjam, joshbruce and styfle May 13, 2020 01:02
Comment thread test/unit/marked-spec.js
marked.use(extension1);
marked.use(extension2);
marked('text');
expect(walkedOnce).toBe(2);

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.

walkedOnce is 2? Maybe it should be renamed?

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

@davisjam davisjam May 14, 2020

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.

Perhaps nWalkedOnce and nWalkedTwice?

@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.

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