Skip to content

Commit

Permalink
YAML: Improved key pattern (#2561)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Oct 1, 2020
1 parent a409245 commit 59853a5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
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.

0 comments on commit 59853a5

Please sign in to comment.