Skip to content

Commit ecd672e

Browse files
committed
fix(md)!: warn if region not found, strip #regions
when a region is not in the source file, instead of including the whole file, warn that the #region was not found in it. fix #4625 --- `<<<` snippets now also strip out all #region markers: ```file.ts // #region A // #region B console.log("Hello, World!"); // #endregion // #endregion ``` <<< file.ts#A ...does not include "#region B" anymore
1 parent 6f86723 commit ecd672e

File tree

3 files changed

+97
-4
lines changed

3 files changed

+97
-4
lines changed

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

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {
22
dedent,
33
findRegions,
4-
rawPathToToken
4+
rawPathToToken,
5+
stripRegionMarkers
56
} from 'node/markdown/plugins/snippet'
67
import { expect } from 'vitest'
78

@@ -406,5 +407,74 @@ describe('node/markdown/plugins/snippet', () => {
406407
expect(extracted).toBe(expected)
407408
}
408409
})
410+
411+
it('handles region names with hyphens and special characters', () => {
412+
const lines = [
413+
'// #region complex-name_123',
414+
'const x = 1;',
415+
'// #endregion complex-name_123'
416+
]
417+
const result = findRegions(lines, 'complex-name_123')
418+
expect(result).toHaveLength(1)
419+
if (result) {
420+
const extracted = result
421+
.flatMap((r) =>
422+
lines
423+
.slice(r.start, r.end)
424+
.filter((l) => !(r.re.start.test(l) || r.re.end.test(l)))
425+
)
426+
.join('\n')
427+
expect(extracted).toBe('const x = 1;')
428+
}
429+
})
430+
})
431+
432+
describe('stripRegionMarkers', () => {
433+
it('removes #region and #endregion lines', () => {
434+
const src = [
435+
'// #region A',
436+
'// #region B',
437+
'console.log("Hello, World!");',
438+
'// #endregion B',
439+
'// #endregion A'
440+
].join('\n')
441+
expect(stripRegionMarkers(src)).toBe('console.log("Hello, World!");')
442+
})
443+
444+
it('removes region markers for various syntaxes', () => {
445+
const src = [
446+
'<!-- #region html -->',
447+
'<div>hi</div>',
448+
'<!-- #endregion html -->',
449+
'/* #region css */',
450+
'body {}',
451+
'/* #endregion css */',
452+
'#pragma region cpp',
453+
'int main(){}',
454+
'#pragma endregion cpp',
455+
'::#region bat',
456+
'ECHO ON',
457+
'REM #endregion bat'
458+
].join('\n')
459+
const out = stripRegionMarkers(src)
460+
expect(out).not.toContain('#region')
461+
expect(out).not.toContain('#endregion')
462+
expect(out).toContain('<div>hi</div>')
463+
expect(out).toContain('body {}')
464+
expect(out).toContain('int main(){}')
465+
expect(out).toContain('ECHO ON')
466+
})
467+
468+
it('removes markers even if indented or with extra spaces', () => {
469+
const src = [
470+
' // #region spaced ',
471+
'\t/* #region */',
472+
'code();',
473+
' // #endregion spaced',
474+
'/* #endregion */'
475+
].join('\n')
476+
const out = stripRegionMarkers(src)
477+
expect(out.trim()).toBe('code();')
478+
})
409479
})
410480
})

src/node/markdown/plugins/snippet.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,18 @@ export function findRegions(lines: string[], regionName: string) {
126126
return returned
127127
}
128128

129+
export function stripRegionMarkers(content: string): string {
130+
return content
131+
.split('\n')
132+
.filter((l) => {
133+
for (const m of markers) {
134+
if (m.start.test(l) || m.end.test(l)) return false
135+
}
136+
return true
137+
})
138+
.join('\n')
139+
}
140+
129141
export const snippetPlugin = (md: MarkdownItAsync, srcDir: string) => {
130142
const parser: RuleBlock = (state, startLine, endLine, silent) => {
131143
const CH = '<'.charCodeAt(0)
@@ -218,7 +230,14 @@ export const snippetPlugin = (md: MarkdownItAsync, srcDir: string) => {
218230
)
219231
.join('\n')
220232
)
233+
content = stripRegionMarkers(content)
234+
} else {
235+
token.content = `Could not find region #${regionName} in file ${src}`
236+
token.info = ''
237+
return fence(...args)
221238
}
239+
} else {
240+
content = stripRegionMarkers(content)
222241
}
223242

224243
token.content = content

src/node/utils/processIncludes.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,13 @@ export function processIncludes(
7171
}
7272
}
7373

74-
content = regions
75-
.flatMap((region) => lines.slice(region.start, region.end))
76-
.join('\n')
74+
if (regions.length > 0) {
75+
content = regions
76+
.flatMap((region) => lines.slice(region.start, region.end))
77+
.join('\n')
78+
} else {
79+
content = `Could not find region or heading ${region[0]} in file ${includePath}`
80+
}
7781
}
7882

7983
if (range) {

0 commit comments

Comments
 (0)