File tree Expand file tree Collapse file tree 2 files changed +31
-12
lines changed
Expand file tree Collapse file tree 2 files changed +31
-12
lines changed Original file line number Diff line number Diff line change @@ -147,6 +147,11 @@ export interface MarkdownOptions extends MarkdownItAsyncOptions {
147147 * @default 'Copy Code'
148148 */
149149 codeCopyButtonTitle ?: string
150+ /**
151+ * Remove all #region markers when including snippets
152+ * @default false
153+ */
154+ stripMarkersFromSnippets ?: boolean
150155
151156 /* ==================== Markdown It Plugins ==================== */
152157
@@ -281,7 +286,7 @@ export async function createMarkdownRenderer(
281286 codeCopyButtonTitle,
282287 languageLabel : options . languageLabel
283288 } )
284- snippetPlugin ( md , srcDir )
289+ snippetPlugin ( md , srcDir , options . stripMarkersFromSnippets )
285290 containerPlugin ( md , options . container )
286291 imagePlugin ( md , options . image )
287292 linkPlugin (
Original file line number Diff line number Diff line change @@ -126,7 +126,11 @@ export function findRegions(lines: string[], regionName: string) {
126126 return returned
127127}
128128
129- export const snippetPlugin = ( md : MarkdownItAsync , srcDir : string ) => {
129+ export const snippetPlugin = (
130+ md : MarkdownItAsync ,
131+ srcDir : string ,
132+ stripMarkersFromSnippets = false
133+ ) => {
130134 const parser : RuleBlock = ( state , startLine , endLine , silent ) => {
131135 const CH = '<' . charCodeAt ( 0 )
132136 const pos = state . bMarks [ startLine ] + state . tShift [ startLine ]
@@ -209,23 +213,33 @@ export const snippetPlugin = (md: MarkdownItAsync, srcDir: string) => {
209213 const regions = findRegions ( lines , region )
210214
211215 if ( regions . length > 0 ) {
212- content = dedent (
213- regions
214- . flatMap ( ( r ) =>
215- lines
216- . slice ( r . start , r . end )
217- . filter ( ( l ) => ! ( r . re . start . test ( l ) || r . re . end . test ( l ) ) )
218- )
219- . join ( '\n' )
220- )
216+ content = regions
217+ . flatMap ( ( r ) =>
218+ lines
219+ . slice ( r . start , r . end )
220+ . filter ( ( l ) => ! ( r . re . start . test ( l ) || r . re . end . test ( l ) ) )
221+ )
222+ . join ( '\n' )
221223 } else {
222224 token . content = `No region #${ region } found in path: ${ src } `
223225 token . info = ''
224226 return fence ( ...args )
225227 }
226228 }
227229
228- token . content = content
230+ if ( stripMarkersFromSnippets ) {
231+ content = content
232+ . split ( '\n' )
233+ . filter ( ( l ) => {
234+ for ( const m of markers ) {
235+ if ( m . start . test ( l ) || m . end . test ( l ) ) return false
236+ }
237+ return true
238+ } )
239+ . join ( '\n' )
240+ }
241+
242+ token . content = dedent ( content )
229243 return fence ( ...args )
230244 }
231245
You can’t perform that action at this time.
0 commit comments