prism-diff-highlight.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. (function () {
  2. if (typeof Prism === 'undefined' || !Prism.languages['diff']) {
  3. return;
  4. }
  5. var LANGUAGE_REGEX = /diff-([\w-]+)/i;
  6. var HTML_TAG = /<\/?(?!\d)[^\s>\/=$<%]+(?:\s(?:\s*[^\s>\/=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))|(?=[\s/>])))+)?\s*\/?>/gi;
  7. //this will match a line plus the line break while ignoring the line breaks HTML tags may contain.
  8. var HTML_LINE = RegExp(/(?:__|[^\r\n<])*(?:\r\n?|\n|(?:__|[^\r\n<])(?![^\r\n]))/.source.replace(/__/g, function () { return HTML_TAG.source; }), 'gi');
  9. var PREFIXES = Prism.languages.diff.PREFIXES;
  10. Prism.hooks.add('before-sanity-check', function (env) {
  11. var lang = env.language;
  12. if (LANGUAGE_REGEX.test(lang) && !env.grammar) {
  13. env.grammar = Prism.languages[lang] = Prism.languages['diff'];
  14. }
  15. });
  16. Prism.hooks.add('before-tokenize', function (env) {
  17. var lang = env.language;
  18. if (LANGUAGE_REGEX.test(lang) && !Prism.languages[lang]) {
  19. Prism.languages[lang] = Prism.languages['diff'];
  20. }
  21. });
  22. Prism.hooks.add('wrap', function (env) {
  23. var diffLanguage, diffGrammar;
  24. if (env.language !== 'diff') {
  25. var langMatch = LANGUAGE_REGEX.exec(env.language);
  26. if (!langMatch) {
  27. return; // not a language specific diff
  28. }
  29. diffLanguage = langMatch[1];
  30. diffGrammar = Prism.languages[diffLanguage];
  31. }
  32. // one of the diff tokens without any nested tokens
  33. if (env.type in PREFIXES) {
  34. /** @type {string} */
  35. var content = env.content.replace(HTML_TAG, ''); // remove all HTML tags
  36. /** @type {string} */
  37. var decoded = content.replace(/&lt;/g, '<').replace(/&amp;/g, '&');
  38. // remove any one-character prefix
  39. var code = decoded.replace(/(^|[\r\n])./g, '$1');
  40. // highlight, if possible
  41. var highlighted;
  42. if (diffGrammar) {
  43. highlighted = Prism.highlight(code, diffGrammar, diffLanguage);
  44. } else {
  45. highlighted = Prism.util.encode(code);
  46. }
  47. // get the HTML source of the prefix token
  48. var prefixToken = new Prism.Token('prefix', PREFIXES[env.type], [/\w+/.exec(env.type)[0]]);
  49. var prefix = Prism.Token.stringify(prefixToken, env.language);
  50. // add prefix
  51. var lines = [], m;
  52. HTML_LINE.lastIndex = 0;
  53. while (m = HTML_LINE.exec(highlighted)) {
  54. lines.push(prefix + m[0]);
  55. }
  56. if (/(?:^|[\r\n]).$/.test(decoded)) {
  57. // because both "+a\n+" and "+a\n" will map to "a\n" after the line prefixes are removed
  58. lines.push(prefix);
  59. }
  60. env.content = lines.join('');
  61. if (diffGrammar) {
  62. env.classes.push('language-' + diffLanguage);
  63. }
  64. }
  65. });
  66. }());