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

This commit is contained in:
Shahed Nasser authored and GitHub committed 2024-07-30 18:59:16 +03:00
1 parent 848d0892d9
commit 6fabb7fad2
4 files changed
+36 -14

No files matched your search

+1
View File
@@ -8,3 +8,4 @@ node_modules
!.yarn/releases !.yarn/releases
!.yarn/sdks !.yarn/sdks
!.yarn/versions !.yarn/versions
.env.local
@@ -1,4 +1,5 @@
import path from "path" import path from "path"
import { getFileSlugSyncUtil } from "./get-file-slug.js"
export type FixLinkOptions = { export type FixLinkOptions = {
currentPageFilePath: string currentPageFilePath: string
@@ -11,17 +12,22 @@ export function fixLinkUtil({
linkedPath, linkedPath,
appsPath: basePath, appsPath: basePath,
}: FixLinkOptions) { }: FixLinkOptions) {
// get absolute path of the URL let fullLinkedFilePath = path.resolve(currentPageFilePath, linkedPath)
const linkedFilePath = path
.resolve(currentPageFilePath, linkedPath)
.replace(basePath, "")
// persist hash in new URL // persist hash in new URL
const hash = linkedFilePath.includes("#") const hash = fullLinkedFilePath.includes("#")
? linkedFilePath.substring(linkedFilePath.indexOf("#")) ? 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( const newLink =
0, linkedFileSlug ||
linkedFilePath.indexOf(`/${path.basename(linkedFilePath)}`) linkedFilePath.substring(
)}${hash}` 0,
linkedFilePath.indexOf(`/${path.basename(linkedFilePath)}`)
)
return `${newLink}${hash}`
} }
@@ -1,4 +1,7 @@
import { getFrontMatterUtil } from "./get-front-matter.js" 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( export async function getFileSlugUtil(
filePath: string filePath: string
@@ -10,3 +13,11 @@ export async function getFileSlugUtil(
return fileFrontmatter.slug 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
}
+8 -4
View File
@@ -15,16 +15,20 @@
}, },
"start:monorepo": { "start:monorepo": {
"dependsOn": [ "dependsOn": [
"build#docs-ui", "^build-scripts#build",
"^start:monorepo" "^docs-ui#build",
"^remark-rehype-plugins#build",
"^types#build"
] ]
}, },
"lint": { }, "lint": { },
"lint:content": { }, "lint:content": { },
"dev:monorepo": { "dev:monorepo": {
"dependsOn": [ "dependsOn": [
"build#docs-ui", "^build-scripts#build",
"^dev:monorepo" "^docs-ui#build",
"^remark-rehype-plugins#build",
"^types#build"
], ],
"cache": false, "cache": false,
"persistent": true "persistent": true