77 lines
2.5 KiB
JavaScript
77 lines
2.5 KiB
JavaScript
(function (Prism) {
|
|
|
|
// https://yaml.org/spec/1.2/spec.html#c-ns-anchor-property
|
|
// https://yaml.org/spec/1.2/spec.html#c-ns-alias-node
|
|
var anchorOrAlias = /[*&][^\s[\]{},]+/;
|
|
// https://yaml.org/spec/1.2/spec.html#c-ns-tag-property
|
|
var tag = /!(?:<[\w\-%#;/?:@&=+$,.!~*'()[\]]+>|(?:[a-zA-Z\d-]*!)?[\w\-%#;/?:@&=+$.~*'()]+)?/;
|
|
// https://yaml.org/spec/1.2/spec.html#c-ns-properties(n,c)
|
|
var properties = '(?:' + tag.source + '(?:[ \t]+' + anchorOrAlias.source + ')?|'
|
|
+ anchorOrAlias.source + '(?:[ \t]+' + tag.source + ')?)';
|
|
|
|
/**
|
|
*
|
|
* @param {string} value
|
|
* @param {string} [flags]
|
|
* @returns {RegExp}
|
|
*/
|
|
function createValuePattern(value, flags) {
|
|
flags = (flags || '').replace(/m/g, '') + 'm'; // add m flag
|
|
var pattern = /([:\-,[{]\s*(?:\s<<prop>>[ \t]+)?)(?:<<value>>)(?=[ \t]*(?:$|,|]|}|\s*#))/.source
|
|
.replace(/<<prop>>/g, function () { return properties; }).replace(/<<value>>/g, function () { return value; });
|
|
return RegExp(pattern, flags)
|
|
}
|
|
|
|
Prism.languages.yaml = {
|
|
'scalar': {
|
|
pattern: RegExp(/([\-:]\s*(?:\s<<prop>>[ \t]+)?[|>])[ \t]*(?:((?:\r?\n|\r)[ \t]+)[^\r\n]+(?:\2[^\r\n]+)*)/.source
|
|
.replace(/<<prop>>/g, function () { return properties; })),
|
|
lookbehind: true,
|
|
alias: 'string'
|
|
},
|
|
'comment': /#.*/,
|
|
'key': {
|
|
pattern: RegExp(/((?:^|[:\-,[{\r\n?])[ \t]*(?:<<prop>>[ \t]+)?)[^\r\n{[\]},#\s]+?(?=\s*:\s)/.source
|
|
.replace(/<<prop>>/g, function () { return properties; })),
|
|
lookbehind: true,
|
|
alias: 'atrule'
|
|
},
|
|
'directive': {
|
|
pattern: /(^[ \t]*)%.+/m,
|
|
lookbehind: true,
|
|
alias: 'important'
|
|
},
|
|
'datetime': {
|
|
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),
|
|
lookbehind: true,
|
|
alias: 'number'
|
|
},
|
|
'boolean': {
|
|
pattern: createValuePattern(/true|false/.source, 'i'),
|
|
lookbehind: true,
|
|
alias: 'important'
|
|
},
|
|
'null': {
|
|
pattern: createValuePattern(/null|~/.source, 'i'),
|
|
lookbehind: true,
|
|
alias: 'important'
|
|
},
|
|
'string': {
|
|
// \2 because of the lookbehind group
|
|
pattern: createValuePattern(/("|')(?:(?!\2)[^\\\r\n]|\\.)*\2/.source),
|
|
lookbehind: true,
|
|
greedy: true
|
|
},
|
|
'number': {
|
|
pattern: createValuePattern(/[+-]?(?:0x[\da-f]+|0o[0-7]+|(?:\d+\.?\d*|\.?\d+)(?:e[+-]?\d+)?|\.inf|\.nan)/.source, 'i'),
|
|
lookbehind: true
|
|
},
|
|
'tag': tag,
|
|
'important': anchorOrAlias,
|
|
'punctuation': /---|[:[\]{}\-,|>?]|\.\.\./
|
|
};
|
|
|
|
Prism.languages.yml = Prism.languages.yaml;
|
|
|
|
}(Prism));
|