Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Specify the different entry file rather than index.md #1429

Closed
wants to merge 9 commits into from
13 changes: 13 additions & 0 deletions docs/config/app-configs.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ export default {
}
```

## entry

- Type: `string`
- Default: `index`

Specify the entry file for Vitepress.

```ts
export default {
entry: 'readme'
}
```

## head

- Type: `HeadConfig[]`
Expand Down
5 changes: 3 additions & 2 deletions src/client/app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ export function withBase(path: string) {
* Converts a url path to the corresponding js chunk filename.
*/
export function pathToFile(path: string): string {
const entry: string = siteDataRef.value.entry || 'index'
let pagePath = path.replace(/\.html$/, '')
pagePath = decodeURIComponent(pagePath)
if (pagePath.endsWith('/')) {
pagePath += 'index'
pagePath += entry
}

if (import.meta.env.DEV) {
Expand All @@ -36,7 +37,7 @@ export function pathToFile(path: string): string {
if (inBrowser) {
const base = import.meta.env.BASE_URL
pagePath =
(pagePath.slice(base.length).replace(/\//g, '_') || 'index') + '.md'
(pagePath.slice(base.length).replace(/\//g, '_') || entry) + '.md'
// client production build needs to account for page hash, which is
// injected directly in the page's html
const pageHash = __VP_HASH_MAP__[pagePath.toLowerCase()]
Expand Down
2 changes: 2 additions & 0 deletions src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const debug = _debug('vitepress:config')

export interface UserConfig<ThemeConfig = any> {
extends?: RawConfigExports<ThemeConfig>
entry?: string
base?: string
lang?: string
title?: string
Expand Down Expand Up @@ -313,6 +314,7 @@ export async function resolveSiteData(
userConfig = userConfig || (await resolveUserConfig(root, command, mode))[0]

return {
entry: userConfig.entry || 'index',
lang: userConfig.lang || 'en-US',
title: userConfig.title || 'VitePress',
titleTemplate: userConfig.titleTemplate,
Expand Down
14 changes: 11 additions & 3 deletions src/node/serve/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,17 @@ export async function serve(options: ServeOptions = {}) {
const notAnAsset = (pathname: string) => !pathname.includes('/assets/')
const notFound = fs.readFileSync(path.resolve(site.outDir, './404.html'))
const onNoMatch: IOptions['onNoMatch'] = (req, res) => {
res.statusCode = 404
if (notAnAsset(req.path)) res.write(notFound.toString())
res.end()
if (site.site.entry !== 'index' && req.path === '/') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah but this logic won't work when someone is deploying their site. It only works with our custom server which is only suitable for previewing the website locally after build. The servers specifically need index.html to be present. That being said, we need to build entry as if it was index.md (and emit index.html).

VuePress has probably done this using their permalink feature, which is still missing in VitePress.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I was worried by this too. I will look into VuePress implementation for this.

const entryPage = fs.readFileSync(
path.resolve(site.outDir, `./${site.site.entry}.html`)
)
res.write(entryPage.toString())
res.end()
} else {
res.statusCode = 404
if (notAnAsset(req.path)) res.write(notFound.toString())
res.end()
}
}

const compress = compression() as RequestHandler
Expand Down
1 change: 1 addition & 0 deletions types/shared.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export type CleanUrlsMode =
| 'with-subfolders'

export interface SiteData<ThemeConfig = any> {
entry?: string
base: string
cleanUrls?: CleanUrlsMode

Expand Down