From e6a3144be68a76b7382dd55f52f7110264a1a0ff Mon Sep 17 00:00:00 2001 From: Erick Zhao Date: Wed, 27 Nov 2024 09:43:34 -0800 Subject: [PATCH] cleaner inlines --- package.json | 3 +- src/components/APIStructurePreview.tsx | 6 -- src/transformers/api-structure-previews.ts | 104 +++++++++------------ yarn.lock | 25 +++-- 4 files changed, 60 insertions(+), 78 deletions(-) diff --git a/package.json b/package.json index 09e8f7b16..e08a59ca3 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "remark-github-admonitions-to-directives": "^2.0.0", "sass": "^1.37.5", "semver": "^7.5.4", + "unist-util-inspect": "^8.1.0", "yaml": "^2.4.3" }, "devDependencies": { @@ -101,7 +102,7 @@ "tar-stream": "^2.2.0", "tsx": "^4.11.2", "typescript": "5.3.2", - "unist-util-remove": "^4.0.0", + "unist-util-filter": "^5.0.1", "unist-util-visit-parents": "^6.0.1" }, "browserslist": { diff --git a/src/components/APIStructurePreview.tsx b/src/components/APIStructurePreview.tsx index 522dd3b34..dcbb1b92f 100644 --- a/src/components/APIStructurePreview.tsx +++ b/src/components/APIStructurePreview.tsx @@ -10,7 +10,6 @@ interface PreviewProps { content: string; title: string; url: string; - inline: boolean; } /** @@ -71,11 +70,6 @@ function APIStructurePreview(props: PreviewProps) { }, []); return ( - //
(); const modifiers = new Set>(); -const EXCLUDE_LIST = ['browser-window-options', 'web-preferences']; - export default function attacher() { return transformer; } @@ -52,8 +50,6 @@ async function transformer(tree: Parent, file: VFile) { relativePath = `/${locale}/docs/${docPath}`; } - remove(tree, 'heading'); - if (fileContent.has(relativePath)) { const { resolve } = fileContent.get(relativePath); if (resolve) resolve(tree); @@ -68,8 +64,8 @@ async function transformer(tree: Parent, file: VFile) { const importNode = getJSXImport('APIStructurePreview'); if (modifiers.size) { tree.children.unshift(importNode); - await Promise.all(Array.from(modifiers)); } + await Promise.all(Array.from(modifiers)); } /** @@ -84,9 +80,7 @@ const checkLinksandDefinitions = (node: Node): node is Link => { structureDefinitions.set(node.identifier, node.url); } if (isLink(node) && node.url.includes('/api/structures/')) { - return EXCLUDE_LIST.every( - (excludedFile) => !node.url.endsWith(`/api/structures/${excludedFile}`) - ); + return true; } return false; @@ -100,7 +94,7 @@ function isStructureLinkReference(node: Node): node is LinkReference { return isLinkReference(node) && structureDefinitions.has(node.identifier); } -function replaceLinkWithPreview(node: Link | LinkReference) { +function replaceLinkWithPreview(node: Link | LinkReference, parents: Parent[]) { // depending on if the node is a direct link or a reference-style link, // we get its URL differently. let relativeStructureUrl: string; @@ -116,7 +110,6 @@ function replaceLinkWithPreview(node: Link | LinkReference) { if (relativeStructureUrl.endsWith('?inline')) { relativeStructureUrl = relativeStructureUrl.split('?inline')[0]; isInline = true; - console.log({ isInline }); } const relativeStructurePath = `${relativeStructureUrl}.md`; @@ -173,8 +166,6 @@ function replaceLinkWithPreview(node: Link | LinkReference) { const { promise } = fileContent.get(relativeStructurePath); - // replace the raw link file with our JSX component. - // See src/components/APIStructurePreview.jsx for implementation. if ( (Array.isArray(node.children) && node.children.length > 0 && @@ -186,56 +177,45 @@ function replaceLinkWithPreview(node: Link | LinkReference) { .then((content) => { const title = (node.children[0] as Text | InlineCode).value; - // replace the Link node with an MDX element in-place - const previewNode = node as unknown as MdxJsxFlowElement; - previewNode.type = 'mdxJsxFlowElement'; - previewNode.name = 'APIStructurePreview'; - previewNode.children = []; - previewNode.data = { - _mdxExplicitJsx: true, - }; - previewNode.attributes = [ - { - type: 'mdxJsxAttribute', - name: 'url', - value: `${relativeStructureUrl}`, - }, - { - type: 'mdxJsxAttribute', - name: 'title', - value: title, - }, - { - type: 'mdxJsxAttribute', - name: 'content', - value: JSON.stringify(content), - }, - { - type: 'mdxJsxAttribute', - name: 'inline', - value: { - type: 'mdxJsxAttributeValueExpression', - value: String(isInline), - data: { - estree: { - type: 'Program', - body: [ - { - type: 'ExpressionStatement', - expression: { - type: 'Literal', - value: isInline, - raw: String(isInline), - }, - }, - ], - sourceType: 'module', - comments: [], - }, - }, + if (isInline) { + const siblings = parents[parents.length - 1].children; + const filtered = filter( + content, + (node) => node.type !== 'mdxjsEsm' && node.type !== 'heading' + ); + visitParents( + filtered, + checkLinksandDefinitions, + replaceLinkWithPreview + ); + siblings.push(filtered); + } else { + // replace the Link node with an MDX element in-place + const previewNode = node as unknown as MdxJsxFlowElement; + previewNode.type = 'mdxJsxFlowElement'; + previewNode.name = 'APIStructurePreview'; + previewNode.children = []; + previewNode.data = { + _mdxExplicitJsx: true, + }; + previewNode.attributes = [ + { + type: 'mdxJsxAttribute', + name: 'url', + value: `${relativeStructureUrl}`, }, - }, - ]; + { + type: 'mdxJsxAttribute', + name: 'title', + value: title, + }, + { + type: 'mdxJsxAttribute', + name: 'content', + value: JSON.stringify(content), + }, + ]; + } }) .catch((err) => { logger.error(err); diff --git a/yarn.lock b/yarn.lock index 8e2486d05..18d8767f4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -14644,6 +14644,22 @@ unique-string@^3.0.0: dependencies: crypto-random-string "^4.0.0" +unist-util-filter@^5.0.1: + version "5.0.1" + resolved "https://registry.yarnpkg.com/unist-util-filter/-/unist-util-filter-5.0.1.tgz#f9f3a0bdee007e040964c274dda27bac663d0a39" + integrity sha512-pHx7D4Zt6+TsfwylH9+lYhBhzyhEnCXs/lbq/Hstxno5z4gVdyc2WEW0asfjGKPyG4pEKrnBv5hdkO6+aRnQJw== + dependencies: + "@types/unist" "^3.0.0" + unist-util-is "^6.0.0" + unist-util-visit-parents "^6.0.0" + +unist-util-inspect@^8.1.0: + version "8.1.0" + resolved "https://registry.yarnpkg.com/unist-util-inspect/-/unist-util-inspect-8.1.0.tgz#ff2729b543c483041b3c29cbe04c5460a406ee25" + integrity sha512-mOlg8Mp33pR0eeFpo5d2902ojqFFOKMMG2hF8bmH7ZlhnmjFgh0NI3/ZDwdaBJNbvrS7LZFVrBVtIE9KZ9s7vQ== + dependencies: + "@types/unist" "^3.0.0" + unist-util-is@^5.0.0: version "5.2.1" resolved "https://registry.yarnpkg.com/unist-util-is/-/unist-util-is-5.2.1.tgz#b74960e145c18dcb6226bc57933597f5486deae9" @@ -14680,15 +14696,6 @@ unist-util-remove-position@^5.0.0: "@types/unist" "^3.0.0" unist-util-visit "^5.0.0" -unist-util-remove@^4.0.0: - version "4.0.0" - resolved "https://registry.yarnpkg.com/unist-util-remove/-/unist-util-remove-4.0.0.tgz#94b7d6bbd24e42d2f841e947ed087be5c82b222e" - integrity sha512-b4gokeGId57UVRX/eVKej5gXqGlc9+trkORhFJpu9raqZkZhU0zm8Doi05+HaiBsMEIJowL+2WtQ5ItjsngPXg== - dependencies: - "@types/unist" "^3.0.0" - unist-util-is "^6.0.0" - unist-util-visit-parents "^6.0.0" - unist-util-stringify-position@^3.0.0: version "3.0.3" resolved "https://registry.yarnpkg.com/unist-util-stringify-position/-/unist-util-stringify-position-3.0.3.tgz#03ad3348210c2d930772d64b489580c13a7db39d"