1717import fs from "fs" ;
1818import path from "path" ;
1919import { fileURLToPath } from "url" ;
20+ import { expandMdxBody } from "./mdx-markdown-expanders.mjs" ;
21+ import { markdownFromBuiltHtml } from "./html-to-markdown.mjs" ;
2022
2123const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
2224const rootDir = path . resolve ( __dirname , ".." ) ;
@@ -118,36 +120,15 @@ function indexSources() {
118120}
119121
120122/**
121- * Strip MDX import/export statements and a small set of common JSX-only blocks
122- * (e.g. <Tabs>...<TabItem>) so the body reads as plain markdown for LLMs.
123- * We deliberately preserve fenced code blocks, tables, lists, and prose.
123+ * Turn MDX source into agent-facing markdown: expand data-driven React
124+ * components, inline partials, and strip remaining JSX.
124125 */
125- function cleanMdxBody ( body ) {
126- // Drop everything that looks like an ESM import/export at the top level.
127- let cleaned = body
128- . split ( / \r ? \n / )
129- . filter ( ( line ) => {
130- const trimmed = line . trim ( ) ;
131- if ( trimmed . startsWith ( "import " ) && / f r o m \s + [ ' " ] / . test ( trimmed ) )
132- return false ;
133- if (
134- trimmed . startsWith ( "export " ) &&
135- ! trimmed . startsWith ( "export const meta" )
136- ) {
137- // strip simple `export const Foo = ...;` lines used by MDX
138- return false ;
139- }
140- return true ;
141- } )
142- . join ( "\n" ) ;
143-
144- // Strip self-closing JSX components like `<Tabs />`, `<DocCardList />`.
145- cleaned = cleaned . replace ( / ^ [ \t ] * < [ A - Z ] [ A - Z a - z 0 - 9 ] * [ ^ > ] * \/ > [ \t ] * $ / gm, "" ) ;
146-
147- // Collapse 3+ blank lines into 2.
148- cleaned = cleaned . replace ( / \n { 3 , } / g, "\n\n" ) ;
149-
150- return cleaned . trimStart ( ) ;
126+ function cleanMdxBody ( body , sourceRelPath ) {
127+ return expandMdxBody ( body , {
128+ rootDir,
129+ docsDir,
130+ sourcePath : sourceRelPath ,
131+ } ) ;
151132}
152133
153134/**
@@ -157,9 +138,16 @@ function cleanMdxBody(body) {
157138function buildMarkdownForDoc ( docId , route , sourcePath , frontmatter ) {
158139 if ( ! sourcePath || ! fs . existsSync ( sourcePath ) ) return null ;
159140
160- const raw = fs . readFileSync ( sourcePath , "utf8" ) ;
161- const { body } = parseFrontmatter ( raw ) ;
162- const cleaned = cleanMdxBody ( body ) ;
141+ const htmlBody = markdownFromBuiltHtml ( buildDir , route ) ;
142+ let cleaned ;
143+ if ( htmlBody ) {
144+ cleaned = htmlBody ;
145+ } else {
146+ const raw = fs . readFileSync ( sourcePath , "utf8" ) ;
147+ const { body } = parseFrontmatter ( raw ) ;
148+ const sourceRelPath = path . relative ( docsDir , sourcePath ) ;
149+ cleaned = cleanMdxBody ( body , sourceRelPath ) ;
150+ }
163151
164152 const title =
165153 ( typeof frontmatter . title === "string" && frontmatter . title . trim ( ) ) ||
0 commit comments