Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions components/prism-yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
// 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 + ')?)';
// https://yaml.org/spec/1.2/spec.html#ns-plain(n,c)
// This is a simplified version that doesn't support "#" and multiline keys
// All these long scarry character classes are simplified versions of YAML's characters
var plainKey = /(?:[^\s\x00-\x08\x0e-\x1f!"#%&'*,\-:>?@[\]`{|}\x7f-\x84\x86-\x9f\ud800-\udfff\ufffe\uffff]|[?:-]<PLAIN>)(?:[ \t]*(?:(?![#:])<PLAIN>|:<PLAIN>))*/.source
.replace(/<PLAIN>/g, function () { return /[^\s\x00-\x08\x0e-\x1f,[\]{}\x7f-\x84\x86-\x9f\ud800-\udfff\ufffe\uffff]/.source; });
var string = /"(?:[^"\\\r\n]|\\.)*"|'(?:[^'\\\r\n]|\\.)*'/.source;

/**
*
Expand All @@ -31,9 +37,11 @@
},
'comment': /#.*/,
'key': {
pattern: RegExp(/((?:^|[:\-,[{\r\n?])[ \t]*(?:<<prop>>[ \t]+)?)[^\r\n{[\]},#\s]+?(?=\s*:\s)/.source
.replace(/<<prop>>/g, function () { return properties; })),
pattern: RegExp(/((?:^|[:\-,[{\r\n?])[ \t]*(?:<<prop>>[ \t]+)?)<<key>>(?=\s*:\s)/.source
.replace(/<<prop>>/g, function () { return properties; })
.replace(/<<key>>/g, function () { return '(?:' + plainKey + '|' + string + ')'; })),
lookbehind: true,
greedy: true,
alias: 'atrule'
},
'directive': {
Expand All @@ -57,8 +65,7 @@
alias: 'important'
},
'string': {
// \2 because of the lookbehind group
pattern: createValuePattern(/("|')(?:(?!\2)[^\\\r\n]|\\.)*\2/.source),
pattern: createValuePattern(string),
lookbehind: true,
greedy: true
},
Expand Down
2 changes: 1 addition & 1 deletion components/prism-yaml.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions tests/languages/yaml/key_feature.test
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
---
foo: 4
FooBar : 5
hello the-world: 23
"\"foo# : {}[]": 23
'\'foo# : {}[]': 23

----------------------------------------------------

[
["punctuation", "---"],
["key", "foo"], ["punctuation", ":"], ["number", "4"],
["key", "FooBar"], ["punctuation", ":"], ["number", "5"]
["key", "FooBar"], ["punctuation", ":"], ["number", "5"],
["key", "hello the-world"], ["punctuation", ":"], ["number", "23"],
["key", "\"\\\"foo# : {}[]\""], ["punctuation", ":"], ["number", "23"],
["key", "'\\'foo# : {}[]'"], ["punctuation", ":"], ["number", "23"]
]

----------------------------------------------------

Checks for keys.
Checks for keys.