Skip to content

Commit

Permalink
(yaml) add support for timestamps (#2496)
Browse files Browse the repository at this point in the history
  • Loading branch information
pplantinga committed Apr 17, 2020
1 parent 18be9ee commit c7c561d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -56,6 +56,7 @@ Language Improvements:
- (delphi) highlight hexadecimal, octal, and binary numbers (#2370) [Robert Riebisch]()
- 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][]

Developer Tools:

Expand Down
10 changes: 10 additions & 0 deletions src/languages/yaml.js
Expand Up @@ -44,6 +44,15 @@ export default function(hljs) {
]
};

var DATE_RE = '[0-9]{4}(-[0-9][0-9]){0,2}';
var TIME_RE = '([Tt \\t][0-9][0-9]?(:[0-9][0-9]){2})?';
var FRACTION_RE = '(\\.[0-9]*)?';
var ZONE_RE = '([ \\t])*(Z|[-+][0-9][0-9]?(:[0-9][0-9])?)?';
var TIMESTAMP = {
className: 'number',
begin: '\\b' + DATE_RE + TIME_RE + FRACTION_RE + ZONE_RE + '\\b',
}

return {
name: 'YAML',
case_insensitive: true,
Expand Down Expand Up @@ -97,6 +106,7 @@ export default function(hljs) {
beginKeywords: LITERALS,
keywords: {literal: LITERALS}
},
TIMESTAMP,
// numbers are any valid C-style number that
// sit isolated from other words
{
Expand Down
6 changes: 6 additions & 0 deletions test/markup/yaml/numbers.expect.txt
Expand Up @@ -3,3 +3,9 @@
<span class="hljs-attr">hex:</span> <span class="hljs-number">0x999fff</span>
<span class="hljs-attr">numkey999:</span> <span class="hljs-number">1234</span>
<span class="hljs-attr">exp:</span> <span class="hljs-number">-2.3e-5</span>
<span class="hljs-attr">canonical:</span> <span class="hljs-number">2001-12-15T02:59:43.1Z</span>
<span class="hljs-attr">iso8601:</span> <span class="hljs-number">2001-12-14t21:59:43.10-05:00</span>
<span class="hljs-attr">space:</span> <span class="hljs-number">2001-12-14 21:59:43.10 -5</span>
<span class="hljs-attr">nozone:</span> <span class="hljs-number">2001-12-15 2:59:43.10</span>
<span class="hljs-attr">date:</span> <span class="hljs-number">2002-12-14</span>

5 changes: 5 additions & 0 deletions test/markup/yaml/numbers.txt
Expand Up @@ -3,3 +3,8 @@ not_hex: 999fff
hex: 0x999fff
numkey999: 1234
exp: -2.3e-5
canonical: 2001-12-15T02:59:43.1Z
iso8601: 2001-12-14t21:59:43.10-05:00
space: 2001-12-14 21:59:43.10 -5
nozone: 2001-12-15 2:59:43.10
date: 2002-12-14

0 comments on commit c7c561d

Please sign in to comment.