prism-yaml.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. (function (Prism) {
  2. // https://yaml.org/spec/1.2/spec.html#c-ns-anchor-property
  3. // https://yaml.org/spec/1.2/spec.html#c-ns-alias-node
  4. var anchorOrAlias = /[*&][^\s[\]{},]+/;
  5. // https://yaml.org/spec/1.2/spec.html#c-ns-tag-property
  6. var tag = /!(?:<[\w\-%#;/?:@&=+$,.!~*'()[\]]+>|(?:[a-zA-Z\d-]*!)?[\w\-%#;/?:@&=+$.~*'()]+)?/;
  7. // https://yaml.org/spec/1.2/spec.html#c-ns-properties(n,c)
  8. var properties = '(?:' + tag.source + '(?:[ \t]+' + anchorOrAlias.source + ')?|'
  9. + anchorOrAlias.source + '(?:[ \t]+' + tag.source + ')?)';
  10. // https://yaml.org/spec/1.2/spec.html#ns-plain(n,c)
  11. // This is a simplified version that doesn't support "#" and multiline keys
  12. // All these long scarry character classes are simplified versions of YAML's characters
  13. var plainKey = /(?:[^\s\x00-\x08\x0e-\x1f!"#%&'*,\-:>?@[\]`{|}\x7f-\x84\x86-\x9f\ud800-\udfff\ufffe\uffff]|[?:-]<PLAIN>)(?:[ \t]*(?:(?![#:])<PLAIN>|:<PLAIN>))*/.source
  14. .replace(/<PLAIN>/g, function () { return /[^\s\x00-\x08\x0e-\x1f,[\]{}\x7f-\x84\x86-\x9f\ud800-\udfff\ufffe\uffff]/.source; });
  15. var string = /"(?:[^"\\\r\n]|\\.)*"|'(?:[^'\\\r\n]|\\.)*'/.source;
  16. /**
  17. *
  18. * @param {string} value
  19. * @param {string} [flags]
  20. * @returns {RegExp}
  21. */
  22. function createValuePattern(value, flags) {
  23. flags = (flags || '').replace(/m/g, '') + 'm'; // add m flag
  24. var pattern = /([:\-,[{]\s*(?:\s<<prop>>[ \t]+)?)(?:<<value>>)(?=[ \t]*(?:$|,|\]|\}|(?:[\r\n]\s*)?#))/.source
  25. .replace(/<<prop>>/g, function () { return properties; }).replace(/<<value>>/g, function () { return value; });
  26. return RegExp(pattern, flags);
  27. }
  28. Prism.languages.yaml = {
  29. 'scalar': {
  30. pattern: RegExp(/([\-:]\s*(?:\s<<prop>>[ \t]+)?[|>])[ \t]*(?:((?:\r?\n|\r)[ \t]+)\S[^\r\n]*(?:\2[^\r\n]+)*)/.source
  31. .replace(/<<prop>>/g, function () { return properties; })),
  32. lookbehind: true,
  33. alias: 'string'
  34. },
  35. 'comment': /#.*/,
  36. 'key': {
  37. pattern: RegExp(/((?:^|[:\-,[{\r\n?])[ \t]*(?:<<prop>>[ \t]+)?)<<key>>(?=\s*:\s)/.source
  38. .replace(/<<prop>>/g, function () { return properties; })
  39. .replace(/<<key>>/g, function () { return '(?:' + plainKey + '|' + string + ')'; })),
  40. lookbehind: true,
  41. greedy: true,
  42. alias: 'atrule'
  43. },
  44. 'directive': {
  45. pattern: /(^[ \t]*)%.+/m,
  46. lookbehind: true,
  47. alias: 'important'
  48. },
  49. 'datetime': {
  50. pattern: createValuePattern(/\d{4}-\d\d?-\d\d?(?:[tT]|[ \t]+)\d\d?:\d{2}:\d{2}(?:\.\d*)?(?:[ \t]*(?:Z|[-+]\d\d?(?::\d{2})?))?|\d{4}-\d{2}-\d{2}|\d\d?:\d{2}(?::\d{2}(?:\.\d*)?)?/.source),
  51. lookbehind: true,
  52. alias: 'number'
  53. },
  54. 'boolean': {
  55. pattern: createValuePattern(/false|true/.source, 'i'),
  56. lookbehind: true,
  57. alias: 'important'
  58. },
  59. 'null': {
  60. pattern: createValuePattern(/null|~/.source, 'i'),
  61. lookbehind: true,
  62. alias: 'important'
  63. },
  64. 'string': {
  65. pattern: createValuePattern(string),
  66. lookbehind: true,
  67. greedy: true
  68. },
  69. 'number': {
  70. pattern: createValuePattern(/[+-]?(?:0x[\da-f]+|0o[0-7]+|(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?|\.inf|\.nan)/.source, 'i'),
  71. lookbehind: true
  72. },
  73. 'tag': tag,
  74. 'important': anchorOrAlias,
  75. 'punctuation': /---|[:[\]{}\-,|>?]|\.\.\./
  76. };
  77. Prism.languages.yml = Prism.languages.yaml;
  78. }(Prism));