Skip to content

Commit e6a3144

Browse files
committed
cleaner inlines
1 parent 5fea0cd commit e6a3144

File tree

4 files changed

+60
-78
lines changed

4 files changed

+60
-78
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"remark-github-admonitions-to-directives": "^2.0.0",
4949
"sass": "^1.37.5",
5050
"semver": "^7.5.4",
51+
"unist-util-inspect": "^8.1.0",
5152
"yaml": "^2.4.3"
5253
},
5354
"devDependencies": {
@@ -101,7 +102,7 @@
101102
"tar-stream": "^2.2.0",
102103
"tsx": "^4.11.2",
103104
"typescript": "5.3.2",
104-
"unist-util-remove": "^4.0.0",
105+
"unist-util-filter": "^5.0.1",
105106
"unist-util-visit-parents": "^6.0.1"
106107
},
107108
"browserslist": {

src/components/APIStructurePreview.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ interface PreviewProps {
1010
content: string;
1111
title: string;
1212
url: string;
13-
inline: boolean;
1413
}
1514

1615
/**
@@ -71,11 +70,6 @@ function APIStructurePreview(props: PreviewProps) {
7170
}, []);
7271

7372
return (
74-
// <div
75-
// dangerouslySetInnerHTML={{
76-
// __html: toHtml(toHast(JSON.parse(props.content))),
77-
// }}
78-
// ></div>
7973
<a
8074
aria-describedby="structures-tooltip" // for accessibility purposes
8175
href={props.url}

src/transformers/api-structure-previews.ts

Lines changed: 42 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logger from '@docusaurus/logger';
22
import { visitParents } from 'unist-util-visit-parents';
3-
import { remove } from 'unist-util-remove';
3+
import { filter } from 'unist-util-filter';
44
import fs from 'fs';
55
import path from 'path';
66
import { Node, Parent } from 'unist';
@@ -23,8 +23,6 @@ const fileContent = new Map<
2323
const structureDefinitions = new Map<string, string>();
2424
const modifiers = new Set<Promise<void>>();
2525

26-
const EXCLUDE_LIST = ['browser-window-options', 'web-preferences'];
27-
2826
export default function attacher() {
2927
return transformer;
3028
}
@@ -52,8 +50,6 @@ async function transformer(tree: Parent, file: VFile) {
5250
relativePath = `/${locale}/docs/${docPath}`;
5351
}
5452

55-
remove(tree, 'heading');
56-
5753
if (fileContent.has(relativePath)) {
5854
const { resolve } = fileContent.get(relativePath);
5955
if (resolve) resolve(tree);
@@ -68,8 +64,8 @@ async function transformer(tree: Parent, file: VFile) {
6864
const importNode = getJSXImport('APIStructurePreview');
6965
if (modifiers.size) {
7066
tree.children.unshift(importNode);
71-
await Promise.all(Array.from(modifiers));
7267
}
68+
await Promise.all(Array.from(modifiers));
7369
}
7470

7571
/**
@@ -84,9 +80,7 @@ const checkLinksandDefinitions = (node: Node): node is Link => {
8480
structureDefinitions.set(node.identifier, node.url);
8581
}
8682
if (isLink(node) && node.url.includes('/api/structures/')) {
87-
return EXCLUDE_LIST.every(
88-
(excludedFile) => !node.url.endsWith(`/api/structures/${excludedFile}`)
89-
);
83+
return true;
9084
}
9185

9286
return false;
@@ -100,7 +94,7 @@ function isStructureLinkReference(node: Node): node is LinkReference {
10094
return isLinkReference(node) && structureDefinitions.has(node.identifier);
10195
}
10296

103-
function replaceLinkWithPreview(node: Link | LinkReference) {
97+
function replaceLinkWithPreview(node: Link | LinkReference, parents: Parent[]) {
10498
// depending on if the node is a direct link or a reference-style link,
10599
// we get its URL differently.
106100
let relativeStructureUrl: string;
@@ -116,7 +110,6 @@ function replaceLinkWithPreview(node: Link | LinkReference) {
116110
if (relativeStructureUrl.endsWith('?inline')) {
117111
relativeStructureUrl = relativeStructureUrl.split('?inline')[0];
118112
isInline = true;
119-
console.log({ isInline });
120113
}
121114

122115
const relativeStructurePath = `${relativeStructureUrl}.md`;
@@ -173,8 +166,6 @@ function replaceLinkWithPreview(node: Link | LinkReference) {
173166

174167
const { promise } = fileContent.get(relativeStructurePath);
175168

176-
// replace the raw link file with our JSX component.
177-
// See src/components/APIStructurePreview.jsx for implementation.
178169
if (
179170
(Array.isArray(node.children) &&
180171
node.children.length > 0 &&
@@ -186,56 +177,45 @@ function replaceLinkWithPreview(node: Link | LinkReference) {
186177
.then((content) => {
187178
const title = (node.children[0] as Text | InlineCode).value;
188179

189-
// replace the Link node with an MDX element in-place
190-
const previewNode = node as unknown as MdxJsxFlowElement;
191-
previewNode.type = 'mdxJsxFlowElement';
192-
previewNode.name = 'APIStructurePreview';
193-
previewNode.children = [];
194-
previewNode.data = {
195-
_mdxExplicitJsx: true,
196-
};
197-
previewNode.attributes = [
198-
{
199-
type: 'mdxJsxAttribute',
200-
name: 'url',
201-
value: `${relativeStructureUrl}`,
202-
},
203-
{
204-
type: 'mdxJsxAttribute',
205-
name: 'title',
206-
value: title,
207-
},
208-
{
209-
type: 'mdxJsxAttribute',
210-
name: 'content',
211-
value: JSON.stringify(content),
212-
},
213-
{
214-
type: 'mdxJsxAttribute',
215-
name: 'inline',
216-
value: {
217-
type: 'mdxJsxAttributeValueExpression',
218-
value: String(isInline),
219-
data: {
220-
estree: {
221-
type: 'Program',
222-
body: [
223-
{
224-
type: 'ExpressionStatement',
225-
expression: {
226-
type: 'Literal',
227-
value: isInline,
228-
raw: String(isInline),
229-
},
230-
},
231-
],
232-
sourceType: 'module',
233-
comments: [],
234-
},
235-
},
180+
if (isInline) {
181+
const siblings = parents[parents.length - 1].children;
182+
const filtered = filter(
183+
content,
184+
(node) => node.type !== 'mdxjsEsm' && node.type !== 'heading'
185+
);
186+
visitParents(
187+
filtered,
188+
checkLinksandDefinitions,
189+
replaceLinkWithPreview
190+
);
191+
siblings.push(filtered);
192+
} else {
193+
// replace the Link node with an MDX element in-place
194+
const previewNode = node as unknown as MdxJsxFlowElement;
195+
previewNode.type = 'mdxJsxFlowElement';
196+
previewNode.name = 'APIStructurePreview';
197+
previewNode.children = [];
198+
previewNode.data = {
199+
_mdxExplicitJsx: true,
200+
};
201+
previewNode.attributes = [
202+
{
203+
type: 'mdxJsxAttribute',
204+
name: 'url',
205+
value: `${relativeStructureUrl}`,
236206
},
237-
},
238-
];
207+
{
208+
type: 'mdxJsxAttribute',
209+
name: 'title',
210+
value: title,
211+
},
212+
{
213+
type: 'mdxJsxAttribute',
214+
name: 'content',
215+
value: JSON.stringify(content),
216+
},
217+
];
218+
}
239219
})
240220
.catch((err) => {
241221
logger.error(err);

yarn.lock

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14644,6 +14644,22 @@ unique-string@^3.0.0:
1464414644
dependencies:
1464514645
crypto-random-string "^4.0.0"
1464614646

14647+
unist-util-filter@^5.0.1:
14648+
version "5.0.1"
14649+
resolved "https://registry.yarnpkg.com/unist-util-filter/-/unist-util-filter-5.0.1.tgz#f9f3a0bdee007e040964c274dda27bac663d0a39"
14650+
integrity sha512-pHx7D4Zt6+TsfwylH9+lYhBhzyhEnCXs/lbq/Hstxno5z4gVdyc2WEW0asfjGKPyG4pEKrnBv5hdkO6+aRnQJw==
14651+
dependencies:
14652+
"@types/unist" "^3.0.0"
14653+
unist-util-is "^6.0.0"
14654+
unist-util-visit-parents "^6.0.0"
14655+
14656+
unist-util-inspect@^8.1.0:
14657+
version "8.1.0"
14658+
resolved "https://registry.yarnpkg.com/unist-util-inspect/-/unist-util-inspect-8.1.0.tgz#ff2729b543c483041b3c29cbe04c5460a406ee25"
14659+
integrity sha512-mOlg8Mp33pR0eeFpo5d2902ojqFFOKMMG2hF8bmH7ZlhnmjFgh0NI3/ZDwdaBJNbvrS7LZFVrBVtIE9KZ9s7vQ==
14660+
dependencies:
14661+
"@types/unist" "^3.0.0"
14662+
1464714663
unist-util-is@^5.0.0:
1464814664
version "5.2.1"
1464914665
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:
1468014696
"@types/unist" "^3.0.0"
1468114697
unist-util-visit "^5.0.0"
1468214698

14683-
unist-util-remove@^4.0.0:
14684-
version "4.0.0"
14685-
resolved "https://registry.yarnpkg.com/unist-util-remove/-/unist-util-remove-4.0.0.tgz#94b7d6bbd24e42d2f841e947ed087be5c82b222e"
14686-
integrity sha512-b4gokeGId57UVRX/eVKej5gXqGlc9+trkORhFJpu9raqZkZhU0zm8Doi05+HaiBsMEIJowL+2WtQ5ItjsngPXg==
14687-
dependencies:
14688-
"@types/unist" "^3.0.0"
14689-
unist-util-is "^6.0.0"
14690-
unist-util-visit-parents "^6.0.0"
14691-
1469214699
unist-util-stringify-position@^3.0.0:
1469314700
version "3.0.3"
1469414701
resolved "https://registry.yarnpkg.com/unist-util-stringify-position/-/unist-util-stringify-position-3.0.3.tgz#03ad3348210c2d930772d64b489580c13a7db39d"

0 commit comments

Comments
 (0)