prism-diff.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. (function (Prism) {
  2. Prism.languages.diff = {
  3. 'coord': [
  4. // Match all kinds of coord lines (prefixed by "+++", "---" or "***").
  5. /^(?:\*{3}|-{3}|\+{3}).*$/m,
  6. // Match "@@ ... @@" coord lines in unified diff.
  7. /^@@.*@@$/m,
  8. // Match coord lines in normal diff (starts with a number).
  9. /^\d.*$/m
  10. ]
  11. // deleted, inserted, unchanged, diff
  12. };
  13. /**
  14. * A map from the name of a block to its line prefix.
  15. *
  16. * @type {Object<string, string>}
  17. */
  18. var PREFIXES = {
  19. 'deleted-sign': '-',
  20. 'deleted-arrow': '<',
  21. 'inserted-sign': '+',
  22. 'inserted-arrow': '>',
  23. 'unchanged': ' ',
  24. 'diff': '!',
  25. };
  26. // add a token for each prefix
  27. Object.keys(PREFIXES).forEach(function (name) {
  28. var prefix = PREFIXES[name];
  29. var alias = [];
  30. if (!/^\w+$/.test(name)) { // "deleted-sign" -> "deleted"
  31. alias.push(/\w+/.exec(name)[0]);
  32. }
  33. if (name === 'diff') {
  34. alias.push('bold');
  35. }
  36. Prism.languages.diff[name] = {
  37. pattern: RegExp('^(?:[' + prefix + '].*(?:\r\n?|\n|(?![\\s\\S])))+', 'm'),
  38. alias: alias,
  39. inside: {
  40. 'line': {
  41. pattern: /(.)(?=[\s\S]).*(?:\r\n?|\n)?/,
  42. lookbehind: true
  43. },
  44. 'prefix': {
  45. pattern: /[\s\S]/,
  46. alias: /\w+/.exec(name)[0]
  47. }
  48. }
  49. };
  50. });
  51. // make prefixes available to Diff plugin
  52. Object.defineProperty(Prism.languages.diff, 'PREFIXES', {
  53. value: PREFIXES
  54. });
  55. }(Prism));