|
5 | 5 | * LICENSE file in the root directory of this source tree. |
6 | 6 | */ |
7 | 7 |
|
8 | | -const INLINE_HIGHLIGHT_REGEX = /(\[\[.*\]\])/; |
| 8 | +const INLINE_HIGHLIGHT_REGEX = /\[\[(.*?)\]\]/g; |
9 | 9 |
|
10 | 10 | function validateInlineHighlights(meta, code) { |
11 | | - const match = INLINE_HIGHLIGHT_REGEX.exec(meta); |
12 | | - if (!match) { |
| 11 | + const matches = [...meta.matchAll(INLINE_HIGHLIGHT_REGEX)]; |
| 12 | + if (matches.length === 0) { |
13 | 13 | return []; |
14 | 14 | } |
15 | 15 |
|
16 | | - let highlights; |
17 | | - try { |
18 | | - highlights = JSON.parse(match[1]); |
19 | | - } catch (error) { |
20 | | - return ['Code highlight metadata must be valid JSON']; |
21 | | - } |
22 | | - |
23 | | - if (!Array.isArray(highlights)) { |
24 | | - return ['Code highlight metadata must be an array']; |
| 16 | + const errors = []; |
| 17 | + let highlights = []; |
| 18 | + for (const match of matches) { |
| 19 | + let parsed; |
| 20 | + try { |
| 21 | + // The fence marker [[...]] is not itself valid JSON, so wrap the |
| 22 | + // captured content in brackets to parse it as an array. This makes |
| 23 | + // both [1, 1, "text"] and [ [1, 1, "text"] ] (nested form) parse |
| 24 | + // into a three-element highlight tuple. |
| 25 | + parsed = JSON.parse(`[${match[1]}]`); |
| 26 | + } catch (error) { |
| 27 | + errors.push('Code highlight metadata must be valid JSON'); |
| 28 | + continue; |
| 29 | + } |
| 30 | + if (!Array.isArray(parsed)) { |
| 31 | + errors.push('Code highlight metadata must be an array'); |
| 32 | + continue; |
| 33 | + } |
| 34 | + // A single group may either wrap the list of highlights |
| 35 | + // ([[ [1, 1, "a"], [2, 3, "b"] ]] style) or be one bare |
| 36 | + // three-element tuple ([1, 1, "a"] style). |
| 37 | + if (parsed.length > 0 && parsed.every(entry => Array.isArray(entry))) { |
| 38 | + highlights = highlights.concat(parsed); |
| 39 | + } else { |
| 40 | + highlights.push(parsed); |
| 41 | + } |
25 | 42 | } |
26 | 43 |
|
27 | 44 | const lines = code.split('\n'); |
28 | | - const errors = []; |
29 | 45 |
|
30 | 46 | for (const highlight of highlights) { |
31 | 47 | if (!Array.isArray(highlight) || highlight.length < 3) { |
|
0 commit comments