-
Notifications
You must be signed in to change notification settings - Fork 923
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): make
content
have higher priority than filePath
in pag…
…e options
- Loading branch information
Showing
5 changed files
with
42 additions
and
19 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
18 changes: 13 additions & 5 deletions
18
...s/core/src/page/resolvePageFileContent.ts → packages/core/src/page/resolvePageContent.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,28 +1,36 @@ | ||
import { isString } from '@vuepress/shared' | ||
import { debug, fs } from '@vuepress/utils' | ||
import type { PageOptions } from '../types/index.js' | ||
|
||
const log = debug('vuepress:core/page') | ||
|
||
// fallback to empty string | ||
const FALLBACK_CONTENT = '' | ||
|
||
/** | ||
* Resolve page file content according to filePath or options content | ||
* Resolve page content according to `content` or `filePath` | ||
*/ | ||
export const resolvePageFileContent = async ({ | ||
export const resolvePageContent = async ({ | ||
filePath, | ||
options, | ||
}: { | ||
filePath: string | null | ||
options: PageOptions | ||
}): Promise<string> => { | ||
// if `content` is provided by options, use it directly | ||
if (isString(options.content)) { | ||
return options.content | ||
} | ||
|
||
// if `filePath` is resolved, read content from file | ||
if (filePath) { | ||
try { | ||
// read page content from file | ||
const content = await fs.readFile(filePath, 'utf-8') | ||
return content | ||
} catch (e) { | ||
log(e instanceof Error ? e.message : e) | ||
} | ||
} | ||
|
||
// load raw content from options | ||
return options.content ?? '' | ||
return FALLBACK_CONTENT | ||
} |
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
30 changes: 23 additions & 7 deletions
30
...tests/page/resolvePageFileContent.spec.ts → ...ore/tests/page/resolvePageContent.spec.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