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

feat(gatsby-remark-prismjs): syntax highlighting inside diffs #26525

Merged
merged 5 commits into from Sep 11, 2020

Conversation

kenchandev
Copy link
Contributor

@kenchandev kenchandev commented Aug 17, 2020

Description

This feature provides support for syntax highlighting inside diffs. In the last example shown on the Diff Highlight page of the Prism documentation, it demonstrates that within a diff code block, Prism is capable of highlighting the syntax in a specific language. See below snapshot to quickly preview what this feature does:

Currently, this is not supported by the gatsby-remark-prismjs plugin. There was an issue about this missing feature, but it eventually closed due to inactivity.

To support this feature, the modifications were minor:

  • Added a regex to check for the diff-xxxx pattern (xxxx is a language such as js or javascript).
  • Adjusted className to allow language-diff-xxxx if diff-xxxx is specified as the language identifier of the code block.
  • Imported the prism-diff-highlight.js plugin. It only adds hooks (the same hooks ran by Prism.highlight) to Prism. Unlike the prism-line-highlight and prism-line-numbers plugins, there is no reference to the window object within the scope of the plugin's IIFE, etc. Using the Prism.highlight method, it highlights the syntax inside diffs if the grammar is specified as diff and the language is specified as diff-xxxx: Prism.highlight(code, Prism.languages.diff, 'diff-' + diffLanguage).
  • Adjusted the highlightCode() to accept an argument (diffLanguage) for the syntax highlighting language if diff-xxxx is specified as the language identifier of the code block.
  • Added a snapshot test for this feature.

Documentation

Based on how issue #20630 was raised, other developers might also assume that this feature is automatically supported out-of-the-box by the plugin.

If documentation for this feature is needed, then feel free to let me know and I'll add it. I think it can be written in the Usage in Markdown section, but I'm not 100% certain if it belongs there or if a new section should be devoted to this.

Related Issues

Fixes #20630

added support for syntax highlighting inside diffs using diff-xxxx
pattern as language identifier

✅ Closes: gatsbyjs#20630
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Aug 17, 2020
@laurieontech laurieontech added topic: remark/mdx Related to Markdown, remark & MDX ecosystem and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Aug 18, 2020
@laurieontech laurieontech requested a review from pieh August 18, 2020 14:20
@pieh pieh self-assigned this Aug 18, 2020
Copy link
Contributor

@pieh pieh left a comment

Choose a reason for hiding this comment

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

Thanks for your patience @kenchandev!

I just added small stub in readme so this feature has some documentation + added 1 test to cover code changes in index.js file (mostly regex matching).

I think this is great addition, thanks a ton!

@pieh pieh merged commit 60fdd22 into gatsbyjs:master Sep 11, 2020
pragmaticpat pushed a commit to pragmaticpat/gatsby that referenced this pull request Apr 28, 2022
…js#26525)

* feat(gatsby-remark-prismjs): syntax highlighting inside diffs

added support for syntax highlighting inside diffs using diff-xxxx
pattern as language identifier

✅ Closes: gatsbyjs#20630

* add minimal documentation about `diff-[language]`

* \w means [a-zA-Z0-9_] so we can drop \d ([0-9]) as it's already covered

* add top level test to also cover regex part

Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: remark/mdx Related to Markdown, remark & MDX ecosystem
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[gatsby-remark-prismjs] diff with language / highlight
3 participants