Skip to content

Commit c516c75

Browse files
committed
feat: support regions in JSON
1 parent deee239 commit c516c75

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

__tests__/unit/node/markdown/plugins/snippet.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,5 +397,51 @@ describe('node/markdown/plugins/snippet', () => {
397397
.join('\n')
398398
expect(extracted).toBe('const x = 1;')
399399
})
400+
401+
it('detects JSON-style region markers with double slash and without trailing comma', () => {
402+
const lines = [
403+
'{',
404+
' "// #region hello": "",',
405+
' "key": true,',
406+
' "// #endregion hello": ""',
407+
'}'
408+
]
409+
const result = findRegions(lines, 'hello')
410+
expect(result).toHaveLength(1)
411+
expect(
412+
result
413+
.flatMap((r) =>
414+
lines
415+
.slice(r.start, r.end)
416+
.filter((l) => !(r.re.start.test(l) || r.re.end.test(l)))
417+
)
418+
.join('\n')
419+
).toBe(' "key": true,')
420+
})
421+
422+
it('detects multiple JSON-style regions with the same name', () => {
423+
const lines = [
424+
'{',
425+
' "// #region hello": "",',
426+
' "one": true,',
427+
' "// #endregion hello": "",',
428+
' "two": false,',
429+
' "/// #region hello": "",',
430+
' "three": true,',
431+
' "//// #endregion hello": "",',
432+
'}'
433+
]
434+
const result = findRegions(lines, 'hello')
435+
expect(result).toHaveLength(2)
436+
const extracted = result
437+
.flatMap((r) =>
438+
lines
439+
.slice(r.start, r.end)
440+
.filter((l) => !(r.re.start.test(l) || r.re.end.test(l)))
441+
)
442+
.join('\n')
443+
const expected = [' "one": true,', ' "three": true,'].join('\n')
444+
expect(extracted).toBe(expected)
445+
})
400446
})
401447
})

src/node/markdown/plugins/snippet.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ const markers = [
8383
{
8484
start: /^\s*\(\*\s*#region\b\s*(.*?)\s*\*\)/,
8585
end: /^\s*\(\*\s*#endregion\b\s*(.*?)\s*\*\)/
86+
},
87+
{
88+
start: /^\s*"[/][/]+\s*#region\b\s*(.*?)":\s*"",?$/,
89+
end: /^\s*"[/][/]+\s*#endregion\b\s*(.*?)":\s*"",?$/
8690
}
8791
]
8892

0 commit comments

Comments
 (0)