docs: fix local link to infer slug from front matter (#8353)
This commit is contained in:
3
www/.gitignore
vendored
3
www/.gitignore
vendored
@@ -7,4 +7,5 @@ node_modules
|
||||
!.yarn/plugins
|
||||
!.yarn/releases
|
||||
!.yarn/sdks
|
||||
!.yarn/versions
|
||||
!.yarn/versions
|
||||
.env.local
|
||||
@@ -1,4 +1,5 @@
|
||||
import path from "path"
|
||||
import { getFileSlugSyncUtil } from "./get-file-slug.js"
|
||||
|
||||
export type FixLinkOptions = {
|
||||
currentPageFilePath: string
|
||||
@@ -11,17 +12,22 @@ export function fixLinkUtil({
|
||||
linkedPath,
|
||||
appsPath: basePath,
|
||||
}: FixLinkOptions) {
|
||||
// get absolute path of the URL
|
||||
const linkedFilePath = path
|
||||
.resolve(currentPageFilePath, linkedPath)
|
||||
.replace(basePath, "")
|
||||
let fullLinkedFilePath = path.resolve(currentPageFilePath, linkedPath)
|
||||
// persist hash in new URL
|
||||
const hash = linkedFilePath.includes("#")
|
||||
? linkedFilePath.substring(linkedFilePath.indexOf("#"))
|
||||
const hash = fullLinkedFilePath.includes("#")
|
||||
? fullLinkedFilePath.substring(fullLinkedFilePath.indexOf("#"))
|
||||
: ""
|
||||
fullLinkedFilePath = fullLinkedFilePath.replace(hash, "")
|
||||
// get absolute path of the URL
|
||||
const linkedFilePath = fullLinkedFilePath.replace(basePath, "")
|
||||
const linkedFileSlug = getFileSlugSyncUtil(fullLinkedFilePath)
|
||||
|
||||
return `${linkedFilePath.substring(
|
||||
0,
|
||||
linkedFilePath.indexOf(`/${path.basename(linkedFilePath)}`)
|
||||
)}${hash}`
|
||||
const newLink =
|
||||
linkedFileSlug ||
|
||||
linkedFilePath.substring(
|
||||
0,
|
||||
linkedFilePath.indexOf(`/${path.basename(linkedFilePath)}`)
|
||||
)
|
||||
|
||||
return `${newLink}${hash}`
|
||||
}
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import { getFrontMatterUtil } from "./get-front-matter.js"
|
||||
import { matter } from "vfile-matter"
|
||||
import { readSync } from "to-vfile"
|
||||
import { FrontMatter } from "../types/index.js"
|
||||
|
||||
export async function getFileSlugUtil(
|
||||
filePath: string
|
||||
@@ -10,3 +13,11 @@ export async function getFileSlugUtil(
|
||||
return fileFrontmatter.slug
|
||||
}
|
||||
}
|
||||
|
||||
export function getFileSlugSyncUtil(filePath: string): string | undefined {
|
||||
const content = readSync(filePath)
|
||||
|
||||
matter(content)
|
||||
|
||||
return ((content.data.matter as FrontMatter).slug as string) || undefined
|
||||
}
|
||||
|
||||
@@ -15,16 +15,20 @@
|
||||
},
|
||||
"start:monorepo": {
|
||||
"dependsOn": [
|
||||
"build#docs-ui",
|
||||
"^start:monorepo"
|
||||
"^build-scripts#build",
|
||||
"^docs-ui#build",
|
||||
"^remark-rehype-plugins#build",
|
||||
"^types#build"
|
||||
]
|
||||
},
|
||||
"lint": { },
|
||||
"lint:content": { },
|
||||
"dev:monorepo": {
|
||||
"dependsOn": [
|
||||
"build#docs-ui",
|
||||
"^dev:monorepo"
|
||||
"^build-scripts#build",
|
||||
"^docs-ui#build",
|
||||
"^remark-rehype-plugins#build",
|
||||
"^types#build"
|
||||
],
|
||||
"cache": false,
|
||||
"persistent": true
|
||||
|
||||
Reference in New Issue
Block a user