diff --git a/src/node/markdown/plugins/snippet.ts b/src/node/markdown/plugins/snippet.ts index dc732d37..99b84740 100644 --- a/src/node/markdown/plugins/snippet.ts +++ b/src/node/markdown/plugins/snippet.ts @@ -66,7 +66,7 @@ function testLine( ) } -function findRegion(lines: Array, regionName: string) { +export function findRegion(lines: Array, regionName: string) { const regionRegexps = [ /^\/\/ ?#?((?:end)?region) ([\w*-]+)$/, // javascript, typescript, java /^\/\* ?#((?:end)?region) ([\w*-]+) ?\*\/$/, // css, less, scss diff --git a/src/node/utils/processIncludes.ts b/src/node/utils/processIncludes.ts index 02177697..1134a196 100644 --- a/src/node/utils/processIncludes.ts +++ b/src/node/utils/processIncludes.ts @@ -2,6 +2,7 @@ import fs from 'fs-extra' import matter from 'gray-matter' import path from 'path' import { slash } from '../shared' +import { findRegion } from '../markdown/plugins/snippet' export function processIncludes( srcDir: string, @@ -11,17 +12,33 @@ export function processIncludes( ): string { const includesRE = //g const rangeRE = /\{(\d*),(\d*)\}$/ + const regionRE = /(#[\w-]+)/ return src.replace(includesRE, (m: string, m1: string) => { if (!m1.length) return m const range = m1.match(rangeRE) + const region = m1.match(regionRE) range && (m1 = m1.slice(0, -range[0].length)) + region && (m1 = m1.slice(0, -region[0].length)) + const atPresent = m1[0] === '@' + try { const includePath = atPresent ? path.join(srcDir, m1.slice(m1[1] === '/' ? 2 : 1)) : path.join(path.dirname(file), m1) let content = fs.readFileSync(includePath, 'utf-8') + + if (region) { + const [ regionName ] = region + const lines = content.split(/\r?\n/); + const regionLines = findRegion(lines, regionName.replace('#', '')) + content = lines.slice( + regionLines?.start || undefined, + regionLines?.end || undefined + ).join("\n") + } + if (range) { const [, startLine, endLine] = range const lines = content.split(/\r?\n/)