UNPKG

2.55 kBJavaScriptView Raw
1(function (Prism) {
2
3 // https://yaml.org/spec/1.2/spec.html#c-ns-anchor-property
4 // https://yaml.org/spec/1.2/spec.html#c-ns-alias-node
5 var anchorOrAlias = /[*&][^\s[\]{},]+/;
6 // https://yaml.org/spec/1.2/spec.html#c-ns-tag-property
7 var tag = /!(?:<[\w\-%#;/?:@&=+$,.!~*'()[\]]+>|(?:[a-zA-Z\d-]*!)?[\w\-%#;/?:@&=+$.~*'()]+)?/;
8 // https://yaml.org/spec/1.2/spec.html#c-ns-properties(n,c)
9 var properties = '(?:' + tag.source + '(?:[ \t]+' + anchorOrAlias.source + ')?|'
10 + anchorOrAlias.source + '(?:[ \t]+' + tag.source + ')?)';
11
12 /**
13 *
14 * @param {string} value
15 * @param {string} [flags]
16 * @returns {RegExp}
17 */
18 function createValuePattern(value, flags) {
19 flags = (flags || '').replace(/m/g, '') + 'm'; // add m flag
20 var pattern = /([:\-,[{]\s*(?:\s<<prop>>[ \t]+)?)(?:<<value>>)(?=[ \t]*(?:$|,|]|}|\s*#))/.source
21 .replace(/<<prop>>/g, function () { return properties; }).replace(/<<value>>/g, function () { return value; });
22 return RegExp(pattern, flags)
23 }
24
25 Prism.languages.yaml = {
26 'scalar': {
27 pattern: RegExp(/([\-:]\s*(?:\s<<prop>>[ \t]+)?[|>])[ \t]*(?:((?:\r?\n|\r)[ \t]+)[^\r\n]+(?:\2[^\r\n]+)*)/.source
28 .replace(/<<prop>>/g, function () { return properties; })),
29 lookbehind: true,
30 alias: 'string'
31 },
32 'comment': /#.*/,
33 'key': {
34 pattern: RegExp(/((?:^|[:\-,[{\r\n?])[ \t]*(?:<<prop>>[ \t]+)?)[^\r\n{[\]},#\s]+?(?=\s*:\s)/.source
35 .replace(/<<prop>>/g, function () { return properties; })),
36 lookbehind: true,
37 alias: 'atrule'
38 },
39 'directive': {
40 pattern: /(^[ \t]*)%.+/m,
41 lookbehind: true,
42 alias: 'important'
43 },
44 'datetime': {
45 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),
46 lookbehind: true,
47 alias: 'number'
48 },
49 'boolean': {
50 pattern: createValuePattern(/true|false/.source, 'i'),
51 lookbehind: true,
52 alias: 'important'
53 },
54 'null': {
55 pattern: createValuePattern(/null|~/.source, 'i'),
56 lookbehind: true,
57 alias: 'important'
58 },
59 'string': {
60 // \2 because of the lookbehind group
61 pattern: createValuePattern(/("|')(?:(?!\2)[^\\\r\n]|\\.)*\2/.source),
62 lookbehind: true,
63 greedy: true
64 },
65 'number': {
66 pattern: createValuePattern(/[+-]?(?:0x[\da-f]+|0o[0-7]+|(?:\d+\.?\d*|\.?\d+)(?:e[+-]?\d+)?|\.inf|\.nan)/.source, 'i'),
67 lookbehind: true
68 },
69 'tag': tag,
70 'important': anchorOrAlias,
71 'punctuation': /---|[:[\]{}\-,|>?]|\.\.\./
72 };
73
74 Prism.languages.yml = Prism.languages.yaml;
75
76}(Prism));