Files
medusa-store/www/apps/ui/src/lib/rehype-component.ts
Shahed Nasser 245e5c9a69 docs: generate documentation for UI components (#5849)
* added tool to generate spec files for React components

* use typedoc for missing descriptions and types

* improvements and fixes

* improvements

* added doc comments for half of the components

* add custom resolver + more doc comments

* added all tsdocs

* general improvements

* add specs to UI docs

* added github action

* remove unnecessary api route

* Added readme for react-docs-generator

* remove comment

* Update packages/design-system/ui/src/components/currency-input/currency-input.tsx

Co-authored-by: Kasper Fabricius Kristensen <45367945+kasperkristensen@users.noreply.github.com>

* remove description of aria fields + add generate script

---------

Co-authored-by: Kasper Fabricius Kristensen <45367945+kasperkristensen@users.noreply.github.com>
2023-12-13 16:02:41 +02:00

81 lines
2.3 KiB
TypeScript

import fs from "fs"
import path from "path"
import { u } from "unist-builder"
import { visit } from "unist-util-visit"
import { ExampleRegistry } from "../registries/example-registry"
import { UnistNode, UnistTree } from "../types/unist"
import { Documentation } from "react-docgen"
export function rehypeComponent() {
return async (tree: UnistTree) => {
visit(tree, (node: UnistNode) => {
if (node.name === "ComponentExample") {
const name = getNodeAttributeByName(node, "name")?.value as string
if (!name) {
return null
}
try {
const component = ExampleRegistry[name]
const src = component.file
const filePath = path.join(process.cwd(), src)
let source = fs.readFileSync(filePath, "utf8")
source = source.replaceAll("export default", "export")
// Trim newline at the end of file. It's correct, but it makes source display look off
if (source.endsWith("\n")) {
source = source.substring(0, source.length - 1)
}
node.children?.push(
u("element", {
tagName: "span",
properties: {
__src__: src,
code: source,
},
})
)
} catch (error) {
console.error(error)
}
} else if (node.name === "ComponentReference") {
const mainComponent = getNodeAttributeByName(node, "mainComponent")
?.value as string
if (!mainComponent) {
return null
}
const mainSpecsDir = path.join(process.cwd(), "src/specs")
const componentSpecsDir = path.join(mainSpecsDir, mainComponent)
const specs: Documentation[] = []
const specFiles = fs.readdirSync(componentSpecsDir)
specFiles.map((specFileName) => {
// read spec file
const specFile = fs.readFileSync(
path.join(componentSpecsDir, specFileName),
"utf-8"
)
specs.push(JSON.parse(specFile) as Documentation)
})
node.attributes?.push({
name: "specsSrc",
value: JSON.stringify(specs),
type: "mdxJsxAttribute",
})
}
})
}
}
function getNodeAttributeByName(node: UnistNode, name: string) {
return node.attributes?.find((attribute) => attribute.name === name)
}