-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: process md includes before building local search index (#2906)
- Loading branch information
Showing
3 changed files
with
46 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import path from 'path' | ||
import fs from 'fs-extra' | ||
import { slash } from '../shared' | ||
|
||
export function processIncludes( | ||
srcDir: string, | ||
src: string, | ||
file: string, | ||
includes: string[] | ||
): string { | ||
const includesRE = /<!--\s*@include:\s*(.*?)\s*-->/g | ||
const rangeRE = /\{(\d*),(\d*)\}$/ | ||
return src.replace(includesRE, (m: string, m1: string) => { | ||
if (!m1.length) return m | ||
|
||
const range = m1.match(rangeRE) | ||
range && (m1 = m1.slice(0, -range[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 (range) { | ||
const [, startLine, endLine] = range | ||
const lines = content.split(/\r?\n/) | ||
content = lines | ||
.slice( | ||
startLine ? parseInt(startLine, 10) - 1 : undefined, | ||
endLine ? parseInt(endLine, 10) : undefined | ||
) | ||
.join('\n') | ||
} | ||
includes.push(slash(includePath)) | ||
// recursively process includes in the content | ||
return processIncludes(srcDir, content, includePath, includes) | ||
} catch (error) { | ||
return m // silently ignore error if file is not present | ||
} | ||
}) | ||
} |