Skip to content

Commit

Permalink
cleaner inlines
Browse files Browse the repository at this point in the history
  • Loading branch information
erickzhao committed Nov 27, 2024
1 parent 5fea0cd commit e6a3144
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 78 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down
6 changes: 0 additions & 6 deletions src/components/APIStructurePreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ interface PreviewProps {
content: string;
title: string;
url: string;
inline: boolean;
}

/**
Expand Down Expand Up @@ -71,11 +70,6 @@ function APIStructurePreview(props: PreviewProps) {
}, []);

return (
// <div
// dangerouslySetInnerHTML={{
// __html: toHtml(toHast(JSON.parse(props.content))),
// }}
// ></div>
<a
aria-describedby="structures-tooltip" // for accessibility purposes
href={props.url}
Expand Down
104 changes: 42 additions & 62 deletions src/transformers/api-structure-previews.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logger from '@docusaurus/logger';
import { visitParents } from 'unist-util-visit-parents';
import { remove } from 'unist-util-remove';
import { filter } from 'unist-util-filter';
import fs from 'fs';
import path from 'path';
import { Node, Parent } from 'unist';
Expand All @@ -23,8 +23,6 @@ const fileContent = new Map<
const structureDefinitions = new Map<string, string>();
const modifiers = new Set<Promise<void>>();

const EXCLUDE_LIST = ['browser-window-options', 'web-preferences'];

export default function attacher() {
return transformer;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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));
}

/**
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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`;
Expand Down Expand Up @@ -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 &&
Expand All @@ -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);
Expand Down
25 changes: 16 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit e6a3144

Please sign in to comment.