Skip to content

Commit

Permalink
refactor(core): reuse inferRoutePath
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Sep 11, 2024
1 parent 94f1f01 commit 68023f0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
10 changes: 6 additions & 4 deletions packages/core/src/page/inferPagePath.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { ensureLeadingSlash, resolveLocalePath } from '@vuepress/shared'
import {
ensureLeadingSlash,
inferRoutePath,
resolveLocalePath,
} from '@vuepress/shared'
import type { App } from '../types/index.js'

/**
Expand All @@ -23,9 +27,7 @@ export const inferPagePath = ({

// infer page route path from file path
// foo/bar.md -> /foo/bar.html
const pathInferred = ensureLeadingSlash(filePathRelative)
.replace(/\.md$/, '.html')
.replace(/\/(README|index).html$/i, '/')
const pathInferred = ensureLeadingSlash(inferRoutePath(filePathRelative))

// resolve page locale path
const pathLocale = resolveLocalePath(app.siteData.locales, pathInferred)
Expand Down
10 changes: 5 additions & 5 deletions packages/shared/src/utils/routes/inferRoutePath.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Infer route path according to the given (markdown file) path
* Infer route path of the given raw path
*/
export const inferRoutePath = (path: string): string => {
// if the pathname is empty or ends with `/`, return as is
if (!path || path.endsWith('/')) return path
export const inferRoutePath = (rawPath: string): string => {
// if the raw path is empty or ends with `/`, return as is
if (!rawPath || rawPath.endsWith('/')) return rawPath

// convert README.md to index.html
let routePath = path.replace(/(^|\/)README.md$/i, '$1index.html')
let routePath = rawPath.replace(/(^|\/)README.md$/i, '$1index.html')

// convert /foo/bar.md to /foo/bar.html
if (routePath.endsWith('.md')) {
Expand Down

0 comments on commit 68023f0

Please sign in to comment.