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

(yaml) improve tag support; add verbatim tags #2487

Merged
merged 13 commits into from
Apr 27, 2020
Merged
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Language Improvements:
- enh(plaintext) added `text` and `txt` as alias (#2360) [Taufik Nurrohman][]
- enh(powershell) added PowerShell v5.1/v7 default aliases as "built_in"s (#2423) [Sean Williams][]
- enh(yaml) added support for timestamps (#2475) [Peter Plantinga][]
- fix(yaml) Fix tags to include non-word characters (#2486) [Peter Plantinga][]

Developer Tools:

Expand Down
20 changes: 16 additions & 4 deletions src/languages/yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Category: common, config
export default function(hljs) {
var LITERALS = 'true false yes no null';

// YAML spec allows non-reserved URI characters in tags.
var URI_CHARACTERS = '[\\w#;/?:@&=+$,.~*\\\'()[\\]]+'

// Define keys as starting with a word character
// ...containing word chars, spaces, colons, forward-slashes, hyphens and periods
// ...and ending with a colon followed immediately by a space, tab or newline.
Expand Down Expand Up @@ -79,13 +82,22 @@ export default function(hljs) {
excludeEnd: true,
relevance: 0
},
{ // local tags
{ // named tags
className: 'type',
begin: '!\\w+!' + URI_CHARACTERS,
},
// https://yaml.org/spec/1.2/spec.html#id2784064
{ // verbatim tags
className: 'type',
begin: '!<' + URI_CHARACTERS + ">",
},
{ // primary tags
className: 'type',
begin: '!' + hljs.UNDERSCORE_IDENT_RE,
begin: '!' + URI_CHARACTERS,
},
{ // data type
{ // secondary tags
className: 'type',
begin: '!!' + hljs.UNDERSCORE_IDENT_RE,
begin: '!!' + URI_CHARACTERS,
},
joshgoebel marked this conversation as resolved.
Show resolved Hide resolved
{ // fragment id &ref
className: 'meta',
Expand Down
8 changes: 8 additions & 0 deletions test/markup/yaml/tag.expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@
<span class="hljs-attr">key:</span> <span class="hljs-type">!localtagname</span> <span class="hljs-string">test</span>
<span class="hljs-attr">key:</span> <span class="hljs-string">"!notatag"</span>
<span class="hljs-attr">key:</span> <span class="hljs-string">'!!notatageither'</span>
<span class="hljs-attr">key:</span> <span class="hljs-type">!!python/dict</span> <span class="hljs-string">test</span>
<span class="hljs-attr">key:</span> <span class="hljs-type">!!python/name:module.name</span> <span class="hljs-string">test</span>
<span class="hljs-attr">key:</span> <span class="hljs-type">!foo2.bar</span> <span class="hljs-string">test</span>
<span class="hljs-attr">key:</span> <span class="hljs-type">!(foo.bar?):tag</span> <span class="hljs-string">test</span>
<span class="hljs-attr">key:</span> <span class="hljs-type">!named!tag</span> <span class="hljs-string">test</span>

<span class="hljs-string">---</span> <span class="hljs-type">!&lt;tag:clarkevans.com,2002:invoice&gt;</span>
<span class="hljs-attr">invoice:</span> <span class="hljs-number">34843</span>
8 changes: 8 additions & 0 deletions test/markup/yaml/tag.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@ key: !!builtintagname test
key: !localtagname test
key: "!notatag"
key: '!!notatageither'
key: !!python/dict test
key: !!python/name:module.name test
key: !foo2.bar test
key: !(foo.bar?):tag test
key: !named!tag test

--- !<tag:clarkevans.com,2002:invoice>
invoice: 34843