-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
RunDevelopment
merged 14 commits into
PrismJS:master
from
RunDevelopment:diff-improvments
Jul 21, 2019
Merged
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
99edee6
Improved Diff
RunDevelopment cd952c5
Better example
RunDevelopment 5682cdc
Fixed line splitting bug with \r\n
RunDevelopment d84ef71
Better alias creation
RunDevelopment dccdf9f
More doc
RunDevelopment 70c2157
HTML aware line splitting
RunDevelopment d44c1a4
Fixed HTML decoding
RunDevelopment 905b5b8
Merge branch 'master' into diff-improvments
RunDevelopment 723edd5
Merge branch 'master' into diff-improvments
RunDevelopment 9d74276
Made it a plugin
RunDevelopment 3deb48d
Resolved conflict
RunDevelopment b021ffe
Updated examples
RunDevelopment 04f2a9c
Funky fix
RunDevelopment a8756fd
Used specifity instead of !important
RunDevelopment 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 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 |
---|---|---|
@@ -1,20 +1,122 @@ | ||
Prism.languages.diff = { | ||
'coord': [ | ||
// Match all kinds of coord lines (prefixed by "+++", "---" or "***"). | ||
/^(?:\*{3}|-{3}|\+{3}).*$/m, | ||
// Match "@@ ... @@" coord lines in unified diff. | ||
/^@@.*@@$/m, | ||
// Match coord lines in normal diff (starts with a number). | ||
/^\d+.*$/m | ||
], | ||
|
||
// Match inserted and deleted lines. Support both +/- and >/< styles. | ||
'deleted': /^[-<].*$/m, | ||
'inserted': /^[+>].*$/m, | ||
|
||
// Match "different" lines (prefixed with "!") in context diff. | ||
'diff': { | ||
'pattern': /^!(?!!).+$/m, | ||
'alias': 'important' | ||
} | ||
}; | ||
(function (Prism) { | ||
|
||
Prism.languages.diff = { | ||
'coord': [ | ||
// Match all kinds of coord lines (prefixed by "+++", "---" or "***"). | ||
/^(?:\*{3}|-{3}|\+{3}).*$/m, | ||
// Match "@@ ... @@" coord lines in unified diff. | ||
/^@@.*@@$/m, | ||
// Match coord lines in normal diff (starts with a number). | ||
/^\d+.*$/m | ||
] | ||
|
||
// deleted, inserted, unchanged, diff | ||
}; | ||
|
||
/** | ||
* A map from the name of a block to its line prefix. | ||
* | ||
* @type {Object<string, string>} | ||
*/ | ||
var prefixes = { | ||
'deleted-sign': '-', | ||
'deleted-arrow': '<', | ||
'inserted-sign': '+', | ||
'inserted-arrow': '>', | ||
'unchanged': ' ', | ||
'diff': '!', | ||
}; | ||
|
||
// add a token for each prefix | ||
Object.keys(prefixes).forEach(function (name) { | ||
var prefix = prefixes[name]; | ||
|
||
var alias = []; | ||
if (!/^\w+$/.test(name)) { // "deleted-sign" -> "deleted" | ||
alias.push(/\w+/.exec(name)[0]); | ||
} | ||
if (name === "diff") { | ||
alias.push("bold"); | ||
} | ||
|
||
Prism.languages.diff[name] = { | ||
// pattern: /^(?:[_].*(?:\r\n?|\n|(?![\s\S])))+/m | ||
pattern: RegExp('^(?:[' + prefix + '].*(?:\r\n?|\n|(?![\\s\\S])))+', 'm'), | ||
alias: alias | ||
}; | ||
}); | ||
|
||
|
||
var LANGUAGE_REGEX = /diff-([\w-]+)/i; | ||
var HTML_TAG = /<\/?(?!\d)[^\s>\/=$<%]+(?:\s(?:\s*[^\s>\/=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))|(?=[\s/>])))+)?\s*\/?>/gi; | ||
//this will match a line plus the line break while ignoring the line breaks HTML tags may contain. | ||
var HTML_LINE = RegExp(/(?:__|[^\r\n<])*(?:\r\n?|\n|(?:__|[^\r\n<])(?![^\r\n]))/.source.replace(/__/g, HTML_TAG.source), 'gi'); | ||
|
||
Prism.hooks.add('before-sanity-check', function (env) { | ||
var lang = env.language; | ||
if (LANGUAGE_REGEX.test(lang) && !env.grammar) { | ||
env.grammar = Prism.languages[lang] = Prism.languages.diff; | ||
} | ||
}) | ||
Prism.hooks.add('before-tokenize', function (env) { | ||
var lang = env.language; | ||
if (LANGUAGE_REGEX.test(lang) && !Prism.languages[lang]) { | ||
Prism.languages[lang] = Prism.languages.diff; | ||
} | ||
}) | ||
|
||
Prism.hooks.add('wrap', function (env) { | ||
var diffLanguage, diffGrammar; | ||
|
||
if (env.language !== 'diff') { | ||
var langMatch = LANGUAGE_REGEX.exec(env.language); | ||
if (!langMatch) { | ||
return; // not a language specific diff | ||
} | ||
|
||
diffLanguage = langMatch[1]; | ||
diffGrammar = Prism.languages[diffLanguage]; | ||
} | ||
|
||
// one of the diff tokens without any nested tokens | ||
if (env.type in prefixes) { | ||
/** @type {string} */ | ||
var content = env.content.replace(HTML_TAG, ''); // remove all HTML tags | ||
|
||
/** @type {string} */ | ||
var decoded = content.replace(/</g, '<').replace(/&/g, '&'); | ||
|
||
// remove any one-character prefix | ||
var code = decoded.replace(/(^|[\r\n])./g, '$1'); | ||
|
||
// highlight, if possible | ||
var highlighted; | ||
if (diffGrammar) { | ||
highlighted = Prism.highlight(code, diffGrammar, diffLanguage); | ||
} else { | ||
highlighted = Prism.util.encode(code); | ||
} | ||
|
||
// get the HTML source of the prefix token | ||
var prefixToken = new Prism.Token('prefix', prefixes[env.type], [/\w+/.exec(env.type)[0]]); | ||
var prefix = Prism.Token.stringify(prefixToken, env.language); | ||
|
||
// add prefix | ||
var lines = [], m; | ||
HTML_LINE.lastIndex = 0; | ||
while (m = HTML_LINE.exec(highlighted)) { | ||
lines.push(prefix + m[0]); | ||
} | ||
if (/(?:^|[\r\n]).$/.test(decoded)) { | ||
// because both "+a\n+" and "+a\n" will map to "a\n" after the line prefixes are removed | ||
lines.push(prefix); | ||
} | ||
env.content = lines.join(''); | ||
|
||
if (diffGrammar) { | ||
env.classes.push('language-' + diffLanguage); | ||
} | ||
} | ||
}); | ||
|
||
}(Prism)); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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.
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.
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 toencode
(ignoring thatencode
can deal with more than only strings).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.
Well, after #1844,
Prism.util.encode(…)
will only know how to deal with strings.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.
It's in #1898.