diff --git a/packages/shared/src/utils/index.ts b/packages/shared/src/utils/index.ts index a14fe7c3d9..885236d8b1 100644 --- a/packages/shared/src/utils/index.ts +++ b/packages/shared/src/utils/index.ts @@ -6,6 +6,7 @@ export * from './isLinkExternal.js' export * from './isLinkHttp.js' export * from './isLinkWithProtocol.js' export * from './isPlainObject.js' +export * from './inferRoutePath.js' export * from './normalizeRoutePath.js' export * from './omit.js' export * from './removeEndingSlash.js' diff --git a/packages/shared/src/utils/inferRoutePath.ts b/packages/shared/src/utils/inferRoutePath.ts new file mode 100644 index 0000000000..96b36e28de --- /dev/null +++ b/packages/shared/src/utils/inferRoutePath.ts @@ -0,0 +1,23 @@ +export const inferRoutePath = (path: string): string => { + // if the pathname is empty or ends with `/`, return as is + if (!path || path.endsWith('/')) return path + + // convert README.md to index.html + let routePath = path.replace(/(^|\/)README.md$/i, '$1index.html') + + // convert /foo/bar.md to /foo/bar.html + if (routePath.endsWith('.md')) { + routePath = routePath.substring(0, routePath.length - 3) + '.html' + } + // convert /foo/bar to /foo/bar.html + else if (!routePath.endsWith('.html')) { + routePath = routePath + '.html' + } + + // convert /foo/index.html to /foo/ + if (routePath.endsWith('/index.html')) { + routePath = routePath.substring(0, routePath.length - 10) + } + + return routePath +} diff --git a/packages/shared/src/utils/normalizeRoutePath.ts b/packages/shared/src/utils/normalizeRoutePath.ts index e398e44416..334fc308c1 100644 --- a/packages/shared/src/utils/normalizeRoutePath.ts +++ b/packages/shared/src/utils/normalizeRoutePath.ts @@ -1,28 +1,6 @@ -const FAKE_HOST = 'http://.' - -export const inferRoutePath = (path: string): string => { - // if the pathname is empty or ends with `/`, return as is - if (!path || path.endsWith('/')) return path - - // convert README.md to index.html - let routePath = path.replace(/(^|\/)README.md$/i, '$1index.html') - - // convert /foo/bar.md to /foo/bar.html - if (routePath.endsWith('.md')) { - routePath = routePath.substring(0, routePath.length - 3) + '.html' - } - // convert /foo/bar to /foo/bar.html - else if (!routePath.endsWith('.html')) { - routePath = routePath + '.html' - } - - // convert /foo/index.html to /foo/ - if (routePath.endsWith('/index.html')) { - routePath = routePath.substring(0, routePath.length - 10) - } +import { inferRoutePath } from './inferRoutePath.js' - return routePath -} +const FAKE_HOST = 'http://.' /** * Normalize the given path to the final route path