-
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: make
@include
effective early to parse included markdown text
- Loading branch information
Showing
5 changed files
with
31 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
title: "@include 测试" | ||
categories: Test | ||
--- | ||
|
||
<!-- @include: ./markdown.md{9,} --> |
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
38 changes: 16 additions & 22 deletions
38
packages/valaxy/node/plugins/markdown/transform/include.ts
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 |
---|---|---|
@@ -1,35 +1,29 @@ | ||
import path from 'pathe' | ||
import fs from 'fs-extra' | ||
import { slash } from '@antfu/utils' | ||
import type { ResolvedValaxyOptions } from '../../../options' | ||
import { processIncludes } from '../utils' | ||
|
||
const includesRE = /<!--\s*@include:\s*(.*?)\s*-->/g | ||
const includedRE = /<!--\s*@included:\s*(.*?)\s*-->/g | ||
|
||
export function createTransformIncludes(options: ResolvedValaxyOptions) { | ||
const srcDir = options.userRoot | ||
|
||
return (code: string, id: string) => { | ||
const fileOrig = id | ||
// resolve includes | ||
const includes: string[] = [] | ||
const dir = path.dirname(id) | ||
|
||
code = processIncludes(srcDir, code, fileOrig, includes) | ||
code = code.replace(includesRE, (m, m1) => { | ||
try { | ||
const includePath = path.join(dir, m1) | ||
const content = fs.readFileSync(includePath, 'utf-8') | ||
includes.push(includePath) | ||
return content | ||
} | ||
catch (error) { | ||
return m // silently ignore error if file is not present | ||
} | ||
}) | ||
return processIncludes(srcDir, code, fileOrig) | ||
} | ||
} | ||
|
||
return { | ||
code, | ||
includes, | ||
} | ||
export function resolveTransformIncludes(code: string, id: string) { | ||
const includes: string[] = [] | ||
const dir = path.dirname(id) | ||
code = code.replace(includedRE, (m, m1) => { | ||
const includePath = path.join(dir, m1) | ||
includes.push(slash(includePath)) | ||
return '' | ||
}) | ||
return { | ||
code, | ||
includes, | ||
} | ||
} |
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