Skip to content

Commit be82e33

Browse files
committed
code review (x2)
1 parent 34204d7 commit be82e33

File tree

3 files changed

+45
-21
lines changed

3 files changed

+45
-21
lines changed

src/generators/jsx/constants.mjs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,22 @@ export const CHANGE_TYPES = {
3030
removed_in: 'Removed in',
3131
introduced_in: 'Introduced in',
3232
};
33+
34+
export const AST_NODES = {
35+
MDX: {
36+
// https://github.com/syntax-tree/mdast-util-mdx-jsx#mdxjsxtextelement
37+
JSX_INLINE_ELEMENT: 'mdxJsxTextElement',
38+
// https://github.com/syntax-tree/mdast-util-mdx-jsx#mdxjsxflowelement
39+
JSX_BLOCK_ELEMENT: 'mdxJsxFlowElement',
40+
// https://github.com/syntax-tree/mdast-util-mdx-jsx#mdxjsxattribute
41+
JSX_ATTRIBUTE: 'mdxJsxAttribute',
42+
// https://github.com/syntax-tree/mdast-util-mdx-jsx#mdxjsxattributevalueexpression
43+
JSX_ATTRIBUTE_EXPRESSION: 'mdxJsxAttributeValueExpression',
44+
},
45+
ESTREE: {
46+
// https://github.com/estree/estree/blob/master/es5.md#programs
47+
PROGRAM: 'Program',
48+
// https://github.com/estree/estree/blob/master/es5.md#expressionstatement
49+
EXPRESSION_STATEMENT: 'ExpressionStatement',
50+
},
51+
};

src/generators/jsx/utils/ast.mjs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { u as createTree } from 'unist-builder';
44
import { valueToEstree } from 'estree-util-value-to-estree';
5+
import { AST_NODES } from '../constants.mjs';
56

67
/**
78
* Creates an MDX JSX element with support for complex attribute values.
@@ -18,54 +19,59 @@ export const createJSXElement = (
1819
name,
1920
{ inline = true, children = [], ...attributes } = {}
2021
) => {
21-
// Process children: convert string to text node or use array as is
22+
// Convert string children to text node or use array directly
2223
const processedChildren =
2324
typeof children === 'string'
2425
? [createTree('text', { value: children })]
25-
: (children ?? []);
26+
: children;
27+
28+
const elementType = inline
29+
? AST_NODES.MDX.JSX_INLINE_ELEMENT
30+
: AST_NODES.MDX.JSX_BLOCK_ELEMENT;
2631

27-
// Create attribute nodes, handling complex objects and primitive values differently
2832
const attrs = Object.entries(attributes).map(([key, value]) =>
2933
createAttributeNode(key, value)
3034
);
3135

32-
// Create and return the appropriate JSX element type
33-
return createTree(inline ? 'mdxJsxTextElement' : 'mdxJsxFlowElement', {
36+
return createTree(elementType, {
3437
name,
3538
attributes: attrs,
3639
children: processedChildren,
3740
});
3841
};
3942

4043
/**
41-
* Creates an MDX JSX attribute node from the input.
44+
* Creates an MDX JSX attribute node based on the value type.
4245
*
4346
* @param {string} name - The attribute name
44-
* @param {any} value - The attribute value (can be any valid JS value)
47+
* @param {any} value - The attribute value
4548
* @returns {import('unist').Node} The MDX JSX attribute node
4649
*/
4750
function createAttributeNode(name, value) {
48-
// For objects and arrays, create expression nodes to preserve structure
51+
// Use expression for objects and arrays
4952
if (value !== null && typeof value === 'object') {
50-
return createTree('mdxJsxAttribute', {
53+
return createTree(AST_NODES.MDX.JSX_ATTRIBUTE, {
5154
name,
52-
value: createTree('mdxJsxAttributeValueExpression', {
55+
value: createTree(AST_NODES.MDX.JSX_ATTRIBUTE_EXPRESSION, {
5356
data: {
5457
estree: {
55-
type: 'Program',
58+
type: AST_NODES.ESTREE.PROGRAM,
5659
body: [
5760
{
58-
type: 'ExpressionStatement',
61+
type: AST_NODES.ESTREE.EXPRESSION_STATEMENT,
5962
expression: valueToEstree(value),
6063
},
6164
],
62-
sourceType: 'module',
6365
},
6466
},
6567
}),
6668
});
6769
}
6870

69-
// For primitives, use simple string conversion
70-
return createTree('mdxJsxAttribute', { name, value: String(value) });
71+
// For primitives, use simple string conversion.
72+
// If undefined, pass nothing.
73+
return createTree(AST_NODES.MDX.JSX_ATTRIBUTE, {
74+
name,
75+
value: value === undefined ? value : String(value),
76+
});
7177
}

src/generators/jsx/utils/buildBarProps.mjs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import readingTime from 'reading-time';
2-
import { DOC_API_BLOB_EDIT_BASE_URL } from '../../../constants.mjs';
32
import { visit } from 'unist-util-visit';
43

54
/**
@@ -29,12 +28,12 @@ export const buildSideBarDocPages = (groupedModules, headNodes) =>
2928
*/
3029
export const buildMetaBarProps = (head, entries) => {
3130
// Extract text content for reading time calculation
32-
let textContent = '';
33-
entries.forEach(entry => {
31+
const textContent = entries.reduce((acc, entry) => {
3432
visit(entry.content, ['text', 'code'], node => {
35-
textContent += node.value || '';
33+
acc += node.value || '';
3634
});
37-
});
35+
return acc;
36+
}, '');
3837

3938
const headings = entries
4039
.filter(entry => entry.heading?.data?.name)
@@ -48,6 +47,6 @@ export const buildMetaBarProps = (head, entries) => {
4847
addedIn: head.introduced_in || head.added_in || '',
4948
readingTime: readingTime(textContent).text,
5049
viewAs: [['JSON', `${head.api}.json`]],
51-
editThisPage: `${DOC_API_BLOB_EDIT_BASE_URL}${head.api}.md`,
50+
editThisPage: `${head.edit_link}`,
5251
};
5352
};

0 commit comments

Comments
 (0)