From 54e1e70d780936f5c5aa6b153d2a5ec29310e055 Mon Sep 17 00:00:00 2001 From: Shahed Nasser Date: Thu, 2 Jan 2025 12:51:53 +0200 Subject: [PATCH] docs: fix cross-project link for Card components (#10794) --- .../src/broken-link-checker.ts | 18 +++++++++++++----- .../src/cross-project-links.ts | 15 ++++++++++----- .../src/utils/component-link-fixer.ts | 18 ++++++++++++------ 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/www/packages/remark-rehype-plugins/src/broken-link-checker.ts b/www/packages/remark-rehype-plugins/src/broken-link-checker.ts index 4d9eb542d0a4f..9307bc44b4f38 100644 --- a/www/packages/remark-rehype-plugins/src/broken-link-checker.ts +++ b/www/packages/remark-rehype-plugins/src/broken-link-checker.ts @@ -90,11 +90,19 @@ function componentChecker({ const attribute = getAttribute(node, attributeName) - if ( - !attribute || - typeof attribute.value === "string" || - !attribute.value.data?.estree - ) { + if (!attribute) { + return + } + + if (typeof attribute.value === "string") { + checkLink({ + link: attribute.value, + ...rest, + }) + return + } + + if (!attribute.value.data?.estree) { return } diff --git a/www/packages/remark-rehype-plugins/src/cross-project-links.ts b/www/packages/remark-rehype-plugins/src/cross-project-links.ts index 3fe859358c3a3..6adc53f277759 100644 --- a/www/packages/remark-rehype-plugins/src/cross-project-links.ts +++ b/www/packages/remark-rehype-plugins/src/cross-project-links.ts @@ -65,11 +65,16 @@ function componentFixer( const attribute = getAttribute(node, attributeName) - if ( - !attribute || - typeof attribute.value === "string" || - !attribute.value.data?.estree - ) { + if (!attribute) { + return + } + + if (typeof attribute.value === "string") { + attribute.value = matchAndFixLinks(attribute.value, options) + return + } + + if (!attribute.value.data?.estree) { return } diff --git a/www/packages/remark-rehype-plugins/src/utils/component-link-fixer.ts b/www/packages/remark-rehype-plugins/src/utils/component-link-fixer.ts index 085190c7f8bcc..0a08c11fd7b11 100644 --- a/www/packages/remark-rehype-plugins/src/utils/component-link-fixer.ts +++ b/www/packages/remark-rehype-plugins/src/utils/component-link-fixer.ts @@ -77,6 +77,7 @@ export function componentLinkFixer( "" ) const appsPath = basePath || path.join(file.cwd, "app") + const linkFn = checkLinksType === "md" ? matchMdLinks : matchValueLink visit(tree as UnistTree, "mdxJsxFlowElement", (node: UnistNodeWithData) => { if (node.name !== componentName) { return @@ -84,11 +85,7 @@ export function componentLinkFixer( const attribute = getAttribute(node, attributeName) - if ( - !attribute || - typeof attribute.value === "string" || - !attribute.value.data?.estree - ) { + if (!attribute) { return } @@ -97,13 +94,22 @@ export function componentLinkFixer( appsPath, } + if (typeof attribute.value === "string") { + attribute.value = + linkFn(attribute.value, linkOptions) || attribute.value + return + } + + if (!attribute.value.data?.estree) { + return + } + const itemJsVar = estreeToJs(attribute.value.data.estree) if (!itemJsVar) { return } - const linkFn = checkLinksType === "md" ? matchMdLinks : matchValueLink performActionOnLiteral(itemJsVar, (item) => { item.original.value = linkFn(item.original.value as string, linkOptions) item.original.raw = JSON.stringify(item.original.value)