|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | // External dependencies |
| 4 | +import { h as createElement } from 'hastscript'; |
4 | 5 | import { u as createTree } from 'unist-builder'; |
5 | 6 | import { SKIP, visit } from 'unist-util-visit'; |
6 | 7 |
|
7 | 8 | // Internal dependencies |
8 | 9 | import createQueries from '../../../utils/queries/index.mjs'; |
9 | 10 | import { createJSXElement } from './ast.mjs'; |
10 | | -import { STABILITY_LEVELS } from '../constants.mjs'; |
| 11 | +import { ICON_SYMBOL_MAP, STABILITY_LEVELS } from '../constants.mjs'; |
| 12 | +import { DOC_NODE_BLOB_BASE_URL } from '../../../constants.mjs'; |
11 | 13 |
|
12 | 14 | /** |
13 | 15 | * Transforms a stability node into an AlertBox JSX element |
14 | 16 | * |
15 | | - * @param {Object} node - The stability node to transform |
| 17 | + * @param {import('mdast').Blockquote} node - The stability node to transform |
16 | 18 | * @param {number} index - The index of the node in its parent's children array |
17 | | - * @param {Object} parent - The parent node containing the stability node |
18 | | - * @returns {Array} - Returns [SKIP] to indicate this node should be skipped in further traversal |
| 19 | + * @param {import('unist').Node} parent - The parent node containing the stability node |
19 | 20 | */ |
20 | 21 | function visitStabilityNode(node, index, parent) { |
21 | | - parent.children.splice( |
22 | | - index, |
23 | | - 1, |
24 | | - createJSXElement('AlertBox', { |
25 | | - children: node.data.description, |
26 | | - level: STABILITY_LEVELS[node.data.index], |
27 | | - title: node.data.index, |
28 | | - }) |
29 | | - ); |
| 22 | + parent.children[index] = createJSXElement('AlertBox', { |
| 23 | + children: node.data.description, |
| 24 | + level: STABILITY_LEVELS[node.data.index], |
| 25 | + title: node.data.index, |
| 26 | + }); |
| 27 | + return [SKIP]; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Adds the metadata around the header node |
| 32 | + * |
| 33 | + * @param {ApiDocMetadataEntry} entry - The content object to process |
| 34 | + * @param {import('mdast').Heading} node - The stability node to transform |
| 35 | + * @param {number} index - The index of the node in its parent's children array |
| 36 | + * @param {import('unist').Node} parent - The parent node containing the stability node |
| 37 | + */ |
| 38 | +function visitHeadingNode(entry, { data, children }, index, parent) { |
| 39 | + console.error(data); |
| 40 | + // Add type icon if available |
| 41 | + if (ICON_SYMBOL_MAP[data.type]) { |
| 42 | + // TODO: This hasn't been implemented yet. This is an assumption of what a |
| 43 | + // potential implementation could look like |
| 44 | + children.unshift( |
| 45 | + createJSXElement('CircularIcon', ICON_SYMBOL_MAP[data.type]) |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + // Replace node with proper heading and anchor |
| 50 | + parent.children[index] = createElement(`h${data.depth + 1}`, [ |
| 51 | + createElement(`a.mark#${data.slug}`, { href: `#${data.slug}` }, children), |
| 52 | + ]); |
| 53 | + |
| 54 | + // Add source link if available |
| 55 | + if (entry.source_link) { |
| 56 | + parent.children.splice( |
| 57 | + index + 1, |
| 58 | + 0, |
| 59 | + createElement('span', [ |
| 60 | + 'Source Code: ', |
| 61 | + createElement( |
| 62 | + 'a', |
| 63 | + { href: `${DOC_NODE_BLOB_BASE_URL}${entry.source_link}` }, |
| 64 | + entry.source_link |
| 65 | + ), |
| 66 | + ]) |
| 67 | + ); |
| 68 | + } |
| 69 | + |
30 | 70 | return [SKIP]; |
31 | 71 | } |
32 | 72 |
|
33 | 73 | /** |
34 | 74 | * Processes content by transforming stability nodes |
35 | 75 | * |
36 | | - * @param {Object} content - The content object to process |
37 | | - * @returns {Object} - The processed content with stability nodes transformed |
| 76 | + * @param {ApiDocMetadataEntry} entry - The entry |
38 | 77 | */ |
39 | | -function processContent(content) { |
40 | | - const contentCopy = { ...content }; |
41 | | - visit(contentCopy, createQueries.UNIST.isStabilityNode, visitStabilityNode); |
42 | | - return contentCopy; |
| 78 | +function process(entry) { |
| 79 | + // Create a shallow copy to avoid modifying the original |
| 80 | + const content = structuredClone(entry.content); |
| 81 | + const { UNIST } = createQueries; |
| 82 | + |
| 83 | + // Apply all transformations to the content |
| 84 | + visit(content, UNIST.isStabilityNode, visitStabilityNode); |
| 85 | + visit(content, UNIST.isHeading, (node, idx, parent) => |
| 86 | + visitHeadingNode(entry, node, idx, parent) |
| 87 | + ); |
| 88 | + |
| 89 | + return content; |
43 | 90 | } |
44 | 91 |
|
45 | 92 | /** |
46 | 93 | * Transforms API metadata entries into processed MDX content |
47 | 94 | * |
48 | | - * @param {Array} metadataEntries - API documentation metadata entries |
| 95 | + * @param {Array<ApiDocMetadataEntry>} metadataEntries - API documentation metadata entries |
49 | 96 | * @param {import('unified').Processor} remark - Remark processor instance for markdown processing |
50 | | - * @returns {React.ReactElement} - Evaluated React element from processed MDX, ready for rendering |
51 | 97 | */ |
52 | 98 | export default function buildContent(metadataEntries, remark) { |
53 | | - // Create and process the root node with all entries |
54 | | - const rootNode = createTree( |
55 | | - 'root', |
56 | | - metadataEntries.map(entry => processContent(entry.content)) |
57 | | - ); |
58 | | - |
| 99 | + const rootNode = createTree('root', metadataEntries.map(process)); |
59 | 100 | return remark.stringify(remark.runSync(rootNode)); |
60 | 101 | } |
0 commit comments