docs: fix local link to infer slug from front matter (#8353)

This commit is contained in:
Shahed Nasser
2024-07-30 18:59:16 +03:00
committed by GitHub
parent 848d0892d9
commit 6fabb7fad2
4 changed files with 37 additions and 15 deletions

3
www/.gitignore vendored
View File

@@ -7,4 +7,5 @@ node_modules
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
!.yarn/versions
.env.local

View File

@@ -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}`
}

View File

@@ -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
}

View File

@@ -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