Skip to content

Commit 59853a5

Browse files
YAML: Improved key pattern (#2561)
1 parent a409245 commit 59853a5

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

components/prism-yaml.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
// https://yaml.org/spec/1.2/spec.html#c-ns-properties(n,c)
99
var properties = '(?:' + tag.source + '(?:[ \t]+' + anchorOrAlias.source + ')?|'
1010
+ anchorOrAlias.source + '(?:[ \t]+' + tag.source + ')?)';
11+
// https://yaml.org/spec/1.2/spec.html#ns-plain(n,c)
12+
// This is a simplified version that doesn't support "#" and multiline keys
13+
// All these long scarry character classes are simplified versions of YAML's characters
14+
var plainKey = /(?:[^\s\x00-\x08\x0e-\x1f!"#%&'*,\-:>?@[\]`{|}\x7f-\x84\x86-\x9f\ud800-\udfff\ufffe\uffff]|[?:-]<PLAIN>)(?:[ \t]*(?:(?![#:])<PLAIN>|:<PLAIN>))*/.source
15+
.replace(/<PLAIN>/g, function () { return /[^\s\x00-\x08\x0e-\x1f,[\]{}\x7f-\x84\x86-\x9f\ud800-\udfff\ufffe\uffff]/.source; });
16+
var string = /"(?:[^"\\\r\n]|\\.)*"|'(?:[^'\\\r\n]|\\.)*'/.source;
1117

1218
/**
1319
*
@@ -31,9 +37,11 @@
3137
},
3238
'comment': /#.*/,
3339
'key': {
34-
pattern: RegExp(/((?:^|[:\-,[{\r\n?])[ \t]*(?:<<prop>>[ \t]+)?)[^\r\n{[\]},#\s]+?(?=\s*:\s)/.source
35-
.replace(/<<prop>>/g, function () { return properties; })),
40+
pattern: RegExp(/((?:^|[:\-,[{\r\n?])[ \t]*(?:<<prop>>[ \t]+)?)<<key>>(?=\s*:\s)/.source
41+
.replace(/<<prop>>/g, function () { return properties; })
42+
.replace(/<<key>>/g, function () { return '(?:' + plainKey + '|' + string + ')'; })),
3643
lookbehind: true,
44+
greedy: true,
3745
alias: 'atrule'
3846
},
3947
'directive': {
@@ -57,8 +65,7 @@
5765
alias: 'important'
5866
},
5967
'string': {
60-
// \2 because of the lookbehind group
61-
pattern: createValuePattern(/("|')(?:(?!\2)[^\\\r\n]|\\.)*\2/.source),
68+
pattern: createValuePattern(string),
6269
lookbehind: true,
6370
greedy: true
6471
},

components/prism-yaml.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/languages/yaml/key_feature.test

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
---
22
foo: 4
33
FooBar : 5
4+
hello the-world: 23
5+
"\"foo# : {}[]": 23
6+
'\'foo# : {}[]': 23
47

58
----------------------------------------------------
69

710
[
811
["punctuation", "---"],
912
["key", "foo"], ["punctuation", ":"], ["number", "4"],
10-
["key", "FooBar"], ["punctuation", ":"], ["number", "5"]
13+
["key", "FooBar"], ["punctuation", ":"], ["number", "5"],
14+
["key", "hello the-world"], ["punctuation", ":"], ["number", "23"],
15+
["key", "\"\\\"foo# : {}[]\""], ["punctuation", ":"], ["number", "23"],
16+
["key", "'\\'foo# : {}[]'"], ["punctuation", ":"], ["number", "23"]
1117
]
1218

1319
----------------------------------------------------
1420

15-
Checks for keys.
21+
Checks for keys.

0 commit comments

Comments
 (0)