Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix highlighting of YAMLs anchor & alias #2217

Merged
merged 4 commits into from
Feb 19, 2020
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
16 changes: 8 additions & 8 deletions components/prism-yaml.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Prism.languages.yaml = {
'scalar': {
pattern: /([\-:]\s*(?:![^\s]+)?[ \t]*[|>])[ \t]*(?:((?:\r?\n|\r)[ \t]+)[^\r\n]+(?:\2[^\r\n]+)*)/,
pattern: /([\-:]\s*(?:\s[!&*][^\s]+)*[ \t]*[|>])[ \t]*(?:((?:\r?\n|\r)[ \t]+)[^\r\n]+(?:\2[^\r\n]+)*)/,
lookbehind: true,
alias: 'string'
},
Expand All @@ -16,32 +16,32 @@ Prism.languages.yaml = {
alias: 'important'
},
'datetime': {
pattern: /([:\-,[{]\s*(?:![^\s]+)?[ \t]*)(?:\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*)?)?)(?=[ \t]*(?:$|,|]|}))/m,
pattern: /([:\-,[{]\s*(?:\s[!&*][^\s]+)*[ \t]*)(?:\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*)?)?)(?=[ \t]*(?:$|,|]|}))/m,
lookbehind: true,
alias: 'number'
},
'boolean': {
pattern: /([:\-,[{]\s*(?:![^\s]+)?[ \t]*)(?:true|false)[ \t]*(?=$|,|]|})/im,
pattern: /([:\-,[{]\s*(?:\s[!&*][^\s]+)*[ \t]*)(?:true|false)[ \t]*(?=$|,|]|})/im,
lookbehind: true,
alias: 'important'
},
'null': {
pattern: /([:\-,[{]\s*(?:![^\s]+)?[ \t]*)(?:null|~)[ \t]*(?=$|,|]|})/im,
pattern: /([:\-,[{]\s*(?:\s[!&*][^\s]+)*[ \t]*)(?:null|~)[ \t]*(?=$|,|]|})/im,
lookbehind: true,
alias: 'important'
},
'string': {
pattern: /([:\-,[{]\s*(?:![^\s]+)?[ \t]*)("|')(?:(?!\2)[^\\\r\n]|\\.)*\2(?=[ \t]*(?:$|,|]|}|\s*#))/m,
pattern: /([:\-,[{]\s*(?:\s[!&*][^\s]+)*[ \t]*)("|')(?:(?!\2)[^\\\r\n]|\\.)*\2(?=[ \t]*(?:$|,|]|}|\s*#))/m,
lookbehind: true,
greedy: true
},
'number': {
pattern: /([:\-,[{]\s*(?:![^\s]+)?[ \t]*)[+-]?(?:0x[\da-f]+|0o[0-7]+|(?:\d+\.?\d*|\.?\d+)(?:e[+-]?\d+)?|\.inf|\.nan)[ \t]*(?=$|,|]|})/im,
pattern: /([:\-,[{]\s*(?:\s[!&*][^\s]+)*[ \t]*)[+-]?(?:0x[\da-f]+|0o[0-7]+|(?:\d+\.?\d*|\.?\d+)(?:e[+-]?\d+)?|\.inf|\.nan)[ \t]*(?=$|,|]|})/im,
lookbehind: true
},
'tag': /![^\s]+/,
'important': /[&*][\w]+/,
'important': /[&*][^\s\[\]{},]+/,
'punctuation': /---|[:[\]{}\-,|>?]|\.\.\./
};

Prism.languages.yml = Prism.languages.yaml;
Prism.languages.yml = Prism.languages.yaml;
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.

174 changes: 174 additions & 0 deletions tests/languages/yaml/anchor_and_alias_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
x-number: &num-ber 13
x-string: &stri_ng "good"
x-null: &nu-ll null
x-boolean: &boo-lean true
x-datetime: &date-time 2001-12-15T02:59:43.1Z
x-scalar: &sca-lar |
foo
bar

x-utf8: &アンカー "japanese anchor"

x-tag-string: &tag-string !!tag-string "13"
x-tag-number: &tag-number !!tag-number 42
x-tag-null: &tag-null !!tag-null null
x-tag-boolean: &tag-bool !!tag-bool true
x-tag-datetime: &tag-date !!tag-date 2001-12-15T02:59:43.1Z
x-tag-scalar: &tag-scalar !!tag-scalar |
foo
bar

x-tag-string: !!tag-string &tag-string "13"
x-tag-number: !!tag-number &tag-number 42
x-tag-null: !!tag-null &tag-null null
x-tag-boolean: !!tag-bool &tag-bool true
x-tag-datetime: !!tag-date &tag-date 2001-12-15T02:59:43.1Z
x-tag-scalar: !!tag-scalar &tag-scalar |
foo
bar

foobar: *num-ber
fubar: *stri_ng
japanese: *アンカー
taga: *tag-a !!taga
tagb: !!tagb *tag-b

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

[
["key", "x-number"],
["punctuation", ":"],
["important", "&num-ber"],
["number", "13"],

["key", "x-string"],
["punctuation", ":"],
["important", "&stri_ng"],
["string", "\"good\""],

["key", "x-null"],
["punctuation", ":"],
["important", "&nu-ll"],
["null", "null"],

["key", "x-boolean"],
["punctuation", ":"],
["important", "&boo-lean"],
["boolean", "true"],

["key", "x-datetime"],
["punctuation", ":"],
["important", "&date-time"],
["datetime", "2001-12-15T02:59:43.1Z"],

["key", "x-scalar"],
["punctuation", ":"],
["important", "&sca-lar"],
["punctuation", "|"],
["scalar", "\n foo\n bar"],

["key", "x-utf8"],
["punctuation", ":"],
["important", "&アンカー"],
["string", "\"japanese anchor\""],

["key", "x-tag-string"],
["punctuation", ":"],
["important", "&tag-string"],
["tag", "!!tag-string"],
["string", "\"13\""],

["key", "x-tag-number"],
["punctuation", ":"],
["important", "&tag-number"],
["tag", "!!tag-number"],
["number", "42"],

["key", "x-tag-null"],
["punctuation", ":"],
["important", "&tag-null"],
["tag", "!!tag-null"],
["null", "null"],

["key", "x-tag-boolean"],
["punctuation", ":"],
["important", "&tag-bool"],
["tag", "!!tag-bool"],
["boolean", "true"],

["key", "x-tag-datetime"],
["punctuation", ":"],
["important", "&tag-date"],
["tag", "!!tag-date"],
["datetime", "2001-12-15T02:59:43.1Z"],

["key", "x-tag-scalar"],
["punctuation", ":"],
["important", "&tag-scalar"],
["tag", "!!tag-scalar"],
["punctuation", "|"],
["scalar", "\n foo\n bar"],

["key", "x-tag-string"],
["punctuation", ":"],
["tag", "!!tag-string"],
["important", "&tag-string"],
["string", "\"13\""],

["key", "x-tag-number"],
["punctuation", ":"],
["tag", "!!tag-number"],
["important", "&tag-number"],
["number", "42"],

["key", "x-tag-null"],
["punctuation", ":"],
["tag", "!!tag-null"],
["important", "&tag-null"],
["null", "null"],

["key", "x-tag-boolean"],
["punctuation", ":"],
["tag", "!!tag-bool"],
["important", "&tag-bool"],
["boolean", "true"],

["key", "x-tag-datetime"],
["punctuation", ":"],
["tag", "!!tag-date"],
["important", "&tag-date"],
["datetime", "2001-12-15T02:59:43.1Z"],

["key", "x-tag-scalar"],
["punctuation", ":"],
["tag", "!!tag-scalar"],
["important", "&tag-scalar"],
["punctuation", "|"],
["scalar", "\n foo\n bar"],

["key", "foobar"],
["punctuation", ":"],
["important", "*num-ber"],

["key", "fubar"],
["punctuation", ":"],
["important", "*stri_ng"],

["key", "japanese"],
["punctuation", ":"],
["important", "*アンカー"],

["key", "taga"],
["punctuation", ":"],
["important", "*tag-a"],
["tag", "!!taga"],

["key", "tagb"],
["punctuation", ":"],
["tag", "!!tagb"],
["important", "*tag-b"]
]

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

Checks for anchors and aliases.