From 6fabb7fad26274dbb75be026e5d229143bc1000e Mon Sep 17 00:00:00 2001 From: Shahed Nasser Date: Tue, 30 Jul 2024 18:59:16 +0300 Subject: [PATCH] docs: fix local link to infer slug from front matter (#8353) --- www/.gitignore | 3 ++- .../src/utils/fix-link.ts | 26 ++++++++++++------- .../src/utils/get-file-slug.ts | 11 ++++++++ www/turbo.json | 12 ++++++--- 4 files changed, 37 insertions(+), 15 deletions(-) diff --git a/www/.gitignore b/www/.gitignore index 42cbb92dd6..8fe0088654 100644 --- a/www/.gitignore +++ b/www/.gitignore @@ -7,4 +7,5 @@ node_modules !.yarn/plugins !.yarn/releases !.yarn/sdks -!.yarn/versions \ No newline at end of file +!.yarn/versions +.env.local \ No newline at end of file diff --git a/www/packages/remark-rehype-plugins/src/utils/fix-link.ts b/www/packages/remark-rehype-plugins/src/utils/fix-link.ts index 8b0e1935c8..8ebab0a0b3 100644 --- a/www/packages/remark-rehype-plugins/src/utils/fix-link.ts +++ b/www/packages/remark-rehype-plugins/src/utils/fix-link.ts @@ -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}` } diff --git a/www/packages/remark-rehype-plugins/src/utils/get-file-slug.ts b/www/packages/remark-rehype-plugins/src/utils/get-file-slug.ts index eef0b7b10f..4b4b816bea 100644 --- a/www/packages/remark-rehype-plugins/src/utils/get-file-slug.ts +++ b/www/packages/remark-rehype-plugins/src/utils/get-file-slug.ts @@ -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 +} diff --git a/www/turbo.json b/www/turbo.json index a0aea41d3a..1df69dbc99 100644 --- a/www/turbo.json +++ b/www/turbo.json @@ -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