Skip to content

Commit 317f38b

Browse files
committed
feat(md): make stripping markers configurable
1 parent 49425d3 commit 317f38b

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
dedent,
33
findRegions,
44
rawPathToToken,
5-
stripRegionMarkers
5+
stripMarkers
66
} from 'node/markdown/plugins/snippet'
77
import { expect } from 'vitest'
88

@@ -438,7 +438,18 @@ describe('node/markdown/plugins/snippet', () => {
438438
'// #endregion B',
439439
'// #endregion A'
440440
]
441-
expect(stripRegionMarkers(src)).toBe('console.log("Hello, World!");')
441+
expect(stripMarkers(src, true)).toBe('console.log("Hello, World!");')
442+
})
443+
444+
it('does not remove any marker if stripRegionMarkers is false', () => {
445+
const src = [
446+
'// #region A',
447+
'// #region B',
448+
'console.log("Hello, World!");',
449+
'// #endregion B',
450+
'// #endregion A'
451+
]
452+
expect(stripMarkers(src, false)).toBe(src.join('\n'))
442453
})
443454

444455
it('removes region markers for various syntaxes', () => {
@@ -456,7 +467,7 @@ describe('node/markdown/plugins/snippet', () => {
456467
'ECHO ON',
457468
'REM #endregion bat'
458469
]
459-
const out = stripRegionMarkers(src)
470+
const out = stripMarkers(src, true)
460471
expect(out).not.toContain('#region')
461472
expect(out).not.toContain('#endregion')
462473
expect(out).toContain('<div>hi</div>')
@@ -473,7 +484,7 @@ describe('node/markdown/plugins/snippet', () => {
473484
' // #endregion spaced',
474485
'/* #endregion */'
475486
]
476-
const out = stripRegionMarkers(src)
487+
const out = stripMarkers(src, true)
477488
expect(out.trim()).toBe('code();')
478489
})
479490
})

src/node/markdown/markdown.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ export interface MarkdownOptions extends MarkdownItAsyncOptions {
140140
* @default 'Copy Code'
141141
*/
142142
codeCopyButtonTitle?: string
143+
/**
144+
* Remove all #region markers when including snippets
145+
* @default false
146+
*/
147+
stripMarkersFromSnippets?: boolean
143148

144149
/* ==================== Markdown It Plugins ==================== */
145150

@@ -274,7 +279,7 @@ export async function createMarkdownRenderer(
274279
codeCopyButtonTitle,
275280
languageLabel: options.languageLabel
276281
})
277-
snippetPlugin(md, srcDir)
282+
snippetPlugin(md, srcDir, options.stripMarkersFromSnippets)
278283
containerPlugin(md, options.container)
279284
imagePlugin(md, options.image)
280285
linkPlugin(

src/node/markdown/plugins/snippet.ts

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

129-
export function stripRegionMarkers(lines: string[]): string {
129+
export function stripMarkers(lines: string[], stripMarkers: boolean): string {
130+
if (!stripMarkers) return lines.join('\n')
130131
return lines
131132
.filter((l) => {
132133
for (const m of markers) {
@@ -137,7 +138,11 @@ export function stripRegionMarkers(lines: string[]): string {
137138
.join('\n')
138139
}
139140

140-
export const snippetPlugin = (md: MarkdownItAsync, srcDir: string) => {
141+
export const snippetPlugin = (
142+
md: MarkdownItAsync,
143+
srcDir: string,
144+
stripMarkersFromSnippets = false
145+
) => {
141146
const parser: RuleBlock = (state, startLine, endLine, silent) => {
142147
const CH = '<'.charCodeAt(0)
143148
const pos = state.bMarks[startLine] + state.tShift[startLine]
@@ -221,12 +226,13 @@ export const snippetPlugin = (md: MarkdownItAsync, srcDir: string) => {
221226

222227
if (regions.length > 0) {
223228
content = dedent(
224-
stripRegionMarkers(
229+
stripMarkers(
225230
regions.flatMap((r) =>
226231
lines
227232
.slice(r.start, r.end)
228233
.filter((l) => !(r.re.start.test(l) || r.re.end.test(l)))
229-
)
234+
),
235+
stripMarkersFromSnippets
230236
)
231237
)
232238
} else {
@@ -235,7 +241,7 @@ export const snippetPlugin = (md: MarkdownItAsync, srcDir: string) => {
235241
return fence(...args)
236242
}
237243
} else {
238-
content = stripRegionMarkers(content.split('\n'))
244+
content = stripMarkers(content.split('\n'), stripMarkersFromSnippets)
239245
}
240246

241247
token.content = content

0 commit comments

Comments
 (0)