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

Diff: Added support for syntax highlighting inside diffs #1889

Merged
merged 14 commits into from
Jul 21, 2019

Conversation

RunDevelopment
Copy link
Member

@RunDevelopment RunDevelopment commented May 14, 2019

This PR adds support for syntax highlighting inside diffs.

Instead of using language-diff, it is now possible to use language-diff-xxxx highlight in a specific language. If you call the highlighting function directly use Prism.highlight(code, Prism.languages.diff, 'diff-' + diffLanguage).

To be compatible with other plugins that need access to the language-specific diff grammar, a new alias for every diff-variant will be created during run-time. (See the before-sanity-check and before-tokenize hooks.)

Before

image

After

image

Slightly tweaked CSS

image

Known issues

  1. If the attributes of the highlighted embedded code contain line breaks, incorrect HTML code is generated. Fixed
  2. Tokens spaning diff blocks aren't highlighted correctly.
    E.g. a multiline comment spanning multiple lines, one of which has been modified.
     /*
    - * foo
    + * bar
      */
  3. If other plugins add tags to the HTML of a block, that block won't be highlighted. I.e. the show Invisibles plugin adds nested tokens which generate tokens (and later tags) inside the blocks. Fixed
  4. The diff-xxxx-pattern is incompatible with auto loader.

This PR resolves #1878.

components/prism-diff.js Outdated Show resolved Hide resolved
var content = env.content.replace(HTML_TAG, ''); // remove all HTML tags

/** @type {string} */
var decoded = content.replace(/&lt;/g, '<').replace(/&amp;/g, '&');
Copy link
Member Author

Choose a reason for hiding this comment

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

I wrote the same code in another PR (can't remember which one), so maybe we should move this line to something like Prism.util.decode as an inverse function to encode (ignoring that encode can deal with more than only strings).

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, after #1844, Prism.util.encode(…) will only know how to deal with strings.

Copy link
Member

Choose a reason for hiding this comment

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

It's in #1898.

@RunDevelopment RunDevelopment added this to the 1.17.0 milestone Jul 17, 2019
themes/prism-funky.css Outdated Show resolved Hide resolved
@RunDevelopment RunDevelopment merged commit e7702ae into PrismJS:master Jul 21, 2019
@RunDevelopment RunDevelopment deleted the diff-improvments branch July 21, 2019 23:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Is there a way to use both language and diff highlights?
3 participants