Skip to content

Commit 12dbc67

Browse files
committedSep 11, 2024·
fix(core): make content have higher priority than filePath in page options
1 parent 68023f0 commit 12dbc67

File tree

5 files changed

+42
-19
lines changed

5 files changed

+42
-19
lines changed
 

‎packages/core/src/page/createPage.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { inferPagePath } from './inferPagePath.js'
33
import { parsePageContent } from './parsePageContent.js'
44
import { resolvePageChunkInfo } from './resolvePageChunkInfo.js'
55
import { resolvePageComponentInfo } from './resolvePageComponentInfo.js'
6+
import { resolvePageContent } from './resolvePageContent.js'
67
import { resolvePageDate } from './resolvePageDate.js'
7-
import { resolvePageFileContent } from './resolvePageFileContent.js'
88
import { resolvePageFilePath } from './resolvePageFilePath.js'
99
import { resolvePageHtmlInfo } from './resolvePageHtmlInfo.js'
1010
import { resolvePageLang } from './resolvePageLang.js'
@@ -27,7 +27,7 @@ export const createPage = async (
2727
})
2828

2929
// read the raw file content according to the absolute file path
30-
const content = await resolvePageFileContent({ filePath, options })
30+
const content = await resolvePageContent({ filePath, options })
3131

3232
// render page content and extract information
3333
const {

‎packages/core/src/page/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export * from './renderPageSfcBlocksToVue.js'
55
export * from './resolvePageChunkInfo.js'
66
export * from './resolvePageComponentInfo.js'
77
export * from './resolvePageDate.js'
8-
export * from './resolvePageFileContent.js'
8+
export * from './resolvePageContent.js'
99
export * from './resolvePageFilePath.js'
1010
export * from './resolvePageHtmlInfo.js'
1111
export * from './resolvePageLang.js'
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
1+
import { isString } from '@vuepress/shared'
12
import { debug, fs } from '@vuepress/utils'
23
import type { PageOptions } from '../types/index.js'
34

45
const log = debug('vuepress:core/page')
56

7+
// fallback to empty string
8+
const FALLBACK_CONTENT = ''
9+
610
/**
7-
* Resolve page file content according to filePath or options content
11+
* Resolve page content according to `content` or `filePath`
812
*/
9-
export const resolvePageFileContent = async ({
13+
export const resolvePageContent = async ({
1014
filePath,
1115
options,
1216
}: {
1317
filePath: string | null
1418
options: PageOptions
1519
}): Promise<string> => {
20+
// if `content` is provided by options, use it directly
21+
if (isString(options.content)) {
22+
return options.content
23+
}
24+
25+
// if `filePath` is resolved, read content from file
1626
if (filePath) {
1727
try {
18-
// read page content from file
1928
const content = await fs.readFile(filePath, 'utf-8')
2029
return content
2130
} catch (e) {
2231
log(e instanceof Error ? e.message : e)
2332
}
2433
}
2534

26-
// load raw content from options
27-
return options.content ?? ''
35+
return FALLBACK_CONTENT
2836
}

‎packages/core/src/types/page.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,10 @@ export type Page<
147147
*/
148148
export interface PageOptions {
149149
/**
150-
* If `filePath` is not set, this option will be used as the raw
151-
* markdown content of the page.
150+
* The raw markdown content of the page.
152151
*
153-
* If `filePath` is set, this option will be ignored, while the
154-
* content of the file will be used.
152+
* If `content` is not provided, the file content of the `filePath`
153+
* will be used.
155154
*/
156155
content?: string
157156

‎packages/core/tests/page/resolvePageFileContent.spec.ts ‎packages/core/tests/page/resolvePageContent.spec.ts

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,51 @@
11
import { fs, path } from '@vuepress/utils'
22
import { expect, it } from 'vitest'
3-
import { resolvePageFileContent } from '../../src/index.js'
3+
import { resolvePageContent } from '../../src/index.js'
44

55
it('should resolve file content correctly from file path', async () => {
66
const filePath = path.resolve(__dirname, '../__fixtures__/pages/foo.md')
7-
const resolved = await resolvePageFileContent({ filePath, options: {} })
7+
const resolved = await resolvePageContent({ filePath, options: {} })
88

99
const expected = (await fs.readFile(filePath)).toString()
1010
expect(resolved).toBe(expected)
1111
})
1212

1313
it('should use content from page options', async () => {
1414
const content = 'foobar'
15-
const resolved = await resolvePageFileContent({
15+
const resolved = await resolvePageContent({
1616
filePath: null,
1717
options: { content },
1818
})
19-
expect(resolved).toBe(resolved)
19+
20+
const expected = content
21+
expect(resolved).toBe(expected)
2022
})
2123

2224
it('should return empty string if nothing provided', async () => {
23-
const resolved = await resolvePageFileContent({
25+
const resolved = await resolvePageContent({
2426
filePath: null,
2527
options: {},
2628
})
27-
expect(resolved).toBe('')
29+
30+
const expected = ''
31+
expect(resolved).toBe(expected)
32+
})
33+
34+
it('should use content from page options and ignore file path', async () => {
35+
const filePath = path.resolve(__dirname, '../__fixtures__/pages/foo.md')
36+
const content = 'foobar'
37+
const resolved = await resolvePageContent({
38+
filePath,
39+
options: { content },
40+
})
41+
42+
const expected = content
43+
expect(resolved).toBe(expected)
2844
})
2945

3046
it('should throw error if the file does not exist', async () => {
3147
try {
32-
await resolvePageFileContent({
48+
await resolvePageContent({
3349
filePath: '404',
3450
options: {},
3551
})

0 commit comments

Comments
 (0)