Skip to content

Commit

Permalink
fix(core): make content have higher priority than filePath in pag…
Browse files Browse the repository at this point in the history
…e options
  • Loading branch information
meteorlxy committed Sep 11, 2024
1 parent 68023f0 commit 12dbc67
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 19 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/page/createPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { inferPagePath } from './inferPagePath.js'
import { parsePageContent } from './parsePageContent.js'
import { resolvePageChunkInfo } from './resolvePageChunkInfo.js'
import { resolvePageComponentInfo } from './resolvePageComponentInfo.js'
import { resolvePageContent } from './resolvePageContent.js'
import { resolvePageDate } from './resolvePageDate.js'
import { resolvePageFileContent } from './resolvePageFileContent.js'
import { resolvePageFilePath } from './resolvePageFilePath.js'
import { resolvePageHtmlInfo } from './resolvePageHtmlInfo.js'
import { resolvePageLang } from './resolvePageLang.js'
Expand All @@ -27,7 +27,7 @@ export const createPage = async (
})

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

// render page content and extract information
const {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/page/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export * from './renderPageSfcBlocksToVue.js'
export * from './resolvePageChunkInfo.js'
export * from './resolvePageComponentInfo.js'
export * from './resolvePageDate.js'
export * from './resolvePageFileContent.js'
export * from './resolvePageContent.js'
export * from './resolvePageFilePath.js'
export * from './resolvePageHtmlInfo.js'
export * from './resolvePageLang.js'
Expand Down
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
}
7 changes: 3 additions & 4 deletions packages/core/src/types/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,10 @@ export type Page<
*/
export interface PageOptions {
/**
* If `filePath` is not set, this option will be used as the raw
* markdown content of the page.
* The raw markdown content of the page.
*
* If `filePath` is set, this option will be ignored, while the
* content of the file will be used.
* If `content` is not provided, the file content of the `filePath`
* will be used.
*/
content?: string

Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,51 @@
import { fs, path } from '@vuepress/utils'
import { expect, it } from 'vitest'
import { resolvePageFileContent } from '../../src/index.js'
import { resolvePageContent } from '../../src/index.js'

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

const expected = (await fs.readFile(filePath)).toString()
expect(resolved).toBe(expected)
})

it('should use content from page options', async () => {
const content = 'foobar'
const resolved = await resolvePageFileContent({
const resolved = await resolvePageContent({
filePath: null,
options: { content },
})
expect(resolved).toBe(resolved)

const expected = content
expect(resolved).toBe(expected)
})

it('should return empty string if nothing provided', async () => {
const resolved = await resolvePageFileContent({
const resolved = await resolvePageContent({
filePath: null,
options: {},
})
expect(resolved).toBe('')

const expected = ''
expect(resolved).toBe(expected)
})

it('should use content from page options and ignore file path', async () => {
const filePath = path.resolve(__dirname, '../__fixtures__/pages/foo.md')
const content = 'foobar'
const resolved = await resolvePageContent({
filePath,
options: { content },
})

const expected = content
expect(resolved).toBe(expected)
})

it('should throw error if the file does not exist', async () => {
try {
await resolvePageFileContent({
await resolvePageContent({
filePath: '404',
options: {},
})
Expand Down

0 comments on commit 12dbc67

Please sign in to comment.