2022-04-30 13:19:27 +02:00
|
|
|
import { ensureLeadingSlash } from './ensure-leading-slash'
|
|
|
|
import { normalizePathSep } from './normalize-path-sep'
|
|
|
|
import { relative } from '../isomorphic/path'
|
|
|
|
import { removePagePathTail } from './remove-page-path-tail'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given the absolute path to the pages folder, an absolute file path for a
|
|
|
|
* page and the page extensions, this function will return the page path
|
|
|
|
* relative to the pages folder. It doesn't consider index tail. Example:
|
|
|
|
* - `/Users/rick/my-project/pages/foo/bar/baz.js` -> `/foo/bar/baz`
|
|
|
|
*
|
|
|
|
* @param filepath Absolute path to the page.
|
2022-05-19 17:46:21 +02:00
|
|
|
* @param opts.pagesDir Absolute path to the pages folder.
|
|
|
|
* @param opts.extensions Extensions allowed for the page.
|
|
|
|
* @param opts.keepIndex When true the trailing `index` kept in the path.
|
2022-04-30 13:19:27 +02:00
|
|
|
*/
|
|
|
|
export function absolutePathToPage(
|
|
|
|
pagePath: string,
|
2022-05-19 17:46:21 +02:00
|
|
|
options: {
|
|
|
|
extensions: string[]
|
|
|
|
keepIndex?: boolean
|
|
|
|
pagesDir: string
|
|
|
|
}
|
2022-04-30 13:19:27 +02:00
|
|
|
) {
|
|
|
|
return removePagePathTail(
|
2022-05-19 17:46:21 +02:00
|
|
|
normalizePathSep(ensureLeadingSlash(relative(options.pagesDir, pagePath))),
|
|
|
|
{
|
|
|
|
extensions: options.extensions,
|
|
|
|
keepIndex: options.keepIndex,
|
|
|
|
}
|
2022-04-30 13:19:27 +02:00
|
|
|
)
|
|
|
|
}
|