-
Notifications
You must be signed in to change notification settings - Fork 8
[WIP] mdx generator #273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
avivkeller
wants to merge
2
commits into
main
Choose a base branch
from
wip/mdx-generator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
[WIP] mdx generator #273
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const STABILITY_LEVELS = [ | ||
'danger', // Deprecated | ||
'warning', // Experimental | ||
'success', // Stable | ||
'info', // | ||
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { groupNodesByModule } from '../../utils/generators.mjs'; | ||
import buildContent from './utils/buildContent.mjs'; | ||
import { toMarkdown } from 'mdast-util-to-markdown'; | ||
import { mdxJsxToMarkdown } from 'mdast-util-mdx-jsx'; | ||
|
||
/** | ||
* This generator generates an MDX AST from an input MDAST | ||
* | ||
* @typedef {Array<ApiDocMetadataEntry>} Input | ||
* | ||
* @type {GeneratorMetadata<Input, string>} | ||
*/ | ||
export default { | ||
name: 'llms-txt', | ||
version: '1.0.0', | ||
description: 'Generates a MDX file from the input AST', | ||
dependsOn: 'ast', | ||
|
||
/** | ||
* Generates a MDX AST | ||
* | ||
* @param {Input} entries | ||
* @returns {Promise<string[]>} Array of generated content | ||
*/ | ||
async generate(entries) { | ||
// Pre-process all data once | ||
const headNodes = entries | ||
.filter(node => node.heading.depth === 1) | ||
.sort((a, b) => a.heading.data.name.localeCompare(b.heading.data.name)); | ||
|
||
const modules = groupNodesByModule(entries); | ||
|
||
// Process nodes in parallel for better performance | ||
const results = await Promise.all( | ||
headNodes.map(node => buildContent(headNodes, modules.get(node.api))) | ||
); | ||
|
||
console.log(toMarkdown(results[0], { extensions: [mdxJsxToMarkdown()] })); | ||
return results; | ||
}, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
'use strict'; | ||
|
||
import { u as createTree } from 'unist-builder'; | ||
import { SKIP, visit } from 'unist-util-visit'; | ||
import createQueries from '../../../utils/queries/index.mjs'; | ||
import { createJSXElement } from './mdxUtilities.mjs'; | ||
import { STABILITY_LEVELS } from '../constants.mjs'; | ||
|
||
/** | ||
* | ||
* @param {import('@types/mdast').Blockquote} node The HTML AST tree of the Stability Index content | ||
* @param {number} index The index of the current node | ||
* @param {import('unist').Parent} parent The parent node of the current node | ||
*/ | ||
const visitStabilityNode = (node, index, parent) => { | ||
parent.children.splice( | ||
index, | ||
1, | ||
createJSXElement('AlertBox', { | ||
children: node.data.description, | ||
level: STABILITY_LEVELS[node.data.index], | ||
title: node.data.index, | ||
}) | ||
); | ||
return [SKIP]; | ||
}; | ||
|
||
/** | ||
* Process a single content entry by applying all transformations | ||
* @param {import('unist').Node} content The content to process | ||
* @returns {import('unist').Node} The processed content | ||
*/ | ||
const processContent = content => { | ||
const clonedContent = structuredClone(content); | ||
visit(clonedContent, createQueries.UNIST.isStabilityNode, visitStabilityNode); | ||
return clonedContent; | ||
}; | ||
|
||
/** | ||
* Transforms API metadata entries into markdown content with special handling for stability nodes | ||
* @param {any} _ Unused parameter | ||
* @param {Array<ApiDocMetadataEntry>} metadataEntries Entries to transform | ||
* @returns {import('unist').Node} Root node with processed content | ||
*/ | ||
export default (_, metadataEntries) => { | ||
const rootNode = createTree( | ||
'root', | ||
metadataEntries.map(entry => processContent(entry.content)) | ||
); | ||
|
||
return rootNode; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
|
||
import { u as createTree } from 'unist-builder'; | ||
|
||
/** | ||
* Creates an MDX JSX element. | ||
* | ||
* @param {string} name - The name of the JSX element | ||
* @param {{ | ||
* inline?: boolean, | ||
* children?: string | import('unist').Node[], | ||
* [key: string]: string | ||
* }} [options={}] - Options including type, children, and JSX attributes | ||
* @returns {import('unist').Node} The created MDX JSX element node | ||
*/ | ||
export const createJSXElement = ( | ||
name, | ||
{ inline = false, children = [], ...attributes } = {} | ||
) => { | ||
const processedChildren = | ||
typeof children === 'string' | ||
? [createTree('text', { value: children })] | ||
: (children ?? []); | ||
|
||
const attrs = Object.entries(attributes).map(([key, value]) => | ||
createTree('mdxJsxAttribute', { name: key, value: String(value) }) | ||
); | ||
|
||
return createTree(inline ? 'mdxJsxTextElement' : 'mdxJsxFlowElement', { | ||
name, | ||
attributes: attrs, | ||
children: processedChildren, | ||
}); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are here for testing the output, and will be removed before this is ready for review