Skip to content

Commit 60922b4

Browse files
committed
feat(generator): add jsx generator
1 parent 6f70de4 commit 60922b4

File tree

14 files changed

+941
-320
lines changed

14 files changed

+941
-320
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ npm-debug.log
44

55
# Default Output Directory
66
out
7+
dist

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# Tests files
22
src/generators/api-links/test/fixtures/
33
*.snapshot
4+
5+
dist

declarations.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module "*.module.css" {
2+
const classes: { [key: string]: string };
3+
export default classes;
4+
}

eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import jsdoc from 'eslint-plugin-jsdoc';
44
import globals from 'globals';
55

66
export default [
7+
{ ignores: ['dist', 'out'] },
78
// @see https://eslint.org/docs/latest/use/configure/configuration-files#specifying-files-and-ignores
89
{
910
files: ['src/**/*.mjs', 'bin/**/*.mjs'],

package-lock.json

Lines changed: 740 additions & 313 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
"hast-util-to-string": "^3.0.1",
4848
"hastscript": "^9.0.1",
4949
"html-minifier-terser": "^7.2.0",
50+
"recma-build-jsx": "^1.0.0",
51+
"recma-jsx": "^1.0.0",
52+
"recma-stringify": "^1.0.0",
53+
"rehype-recma": "^1.0.0",
5054
"rehype-stringify": "^10.0.1",
5155
"remark-gfm": "^4.0.1",
5256
"remark-parse": "^11.0.0",

src/generators/index.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import apiLinks from './api-links/index.mjs';
1111
import oramaDb from './orama-db/index.mjs';
1212
import astJs from './ast-js/index.mjs';
1313
import llmsTxt from './llms-txt/index.mjs';
14+
import jsx from './jsx/index.mjs';
1415

1516
export const publicGenerators = {
1617
'json-simple': jsonSimple,
@@ -23,6 +24,7 @@ export const publicGenerators = {
2324
'api-links': apiLinks,
2425
'orama-db': oramaDb,
2526
'llms-txt': llmsTxt,
27+
jsx,
2628
};
2729

2830
export const allGenerators = {

src/generators/jsx/constants.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const STABILITY_LEVELS = [
2+
'danger', // Deprecated
3+
'warning', // Experimental
4+
'success', // Stable
5+
'info', // Legacy
6+
];

src/generators/jsx/index.mjs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { groupNodesByModule } from '../../utils/generators.mjs';
2+
import buildContent from './utils/buildContent.mjs';
3+
import { getRemarkRecma } from '../../utils/remark.mjs';
4+
5+
/**
6+
* This generator generates an JSX AST from an input MDAST
7+
*
8+
* @typedef {Array<ApiDocMetadataEntry>} Input
9+
*
10+
* @type {GeneratorMetadata<Input, string>}
11+
*/
12+
export default {
13+
name: 'jsx',
14+
version: '1.0.0',
15+
description: 'Generates JSX from the input AST',
16+
dependsOn: 'ast',
17+
18+
/**
19+
* Generates a JSX AST
20+
*
21+
* @param {Input} entries
22+
* @returns {Promise<string[]>} Array of generated content
23+
*/
24+
async generate(entries) {
25+
const remarkRecma = getRemarkRecma();
26+
27+
const results = await Promise.all(
28+
Array.from(groupNodesByModule(entries).values()).map(entry =>
29+
buildContent(entry, remarkRecma)
30+
)
31+
);
32+
33+
console.log(results);
34+
return results;
35+
},
36+
};

src/generators/jsx/utils/ast.mjs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
import { u as createTree } from 'unist-builder';
4+
5+
/**
6+
* Creates an MDX JSX element.
7+
*
8+
* @param {string} name - The name of the JSX element
9+
* @param {{
10+
* inline?: boolean,
11+
* children?: string | import('unist').Node[],
12+
* [key: string]: string
13+
* }} [options={}] - Options including type, children, and JSX attributes
14+
* @returns {import('unist').Node} The created MDX JSX element node
15+
*/
16+
export const createJSXElement = (
17+
name,
18+
{ inline = true, children = [], ...attributes } = {}
19+
) => {
20+
const processedChildren =
21+
typeof children === 'string'
22+
? [createTree('text', { value: children })]
23+
: (children ?? []);
24+
25+
const attrs = Object.entries(attributes).map(([key, value]) =>
26+
createTree('mdxJsxAttribute', { name: key, value: String(value) })
27+
);
28+
29+
return createTree(inline ? 'mdxJsxTextElement' : 'mdxJsxFlowElement', {
30+
name,
31+
attributes: attrs,
32+
children: processedChildren,
33+
});
34+
};

0 commit comments

Comments
 (0)