// Copies the docs-site pages into docs/ as plain markdown, so an agent can read
// them from node_modules/executable-stories-formatters/docs without a network.
// Runs on prepack. MDX components outside code fences are flattened to markdown
// or dropped; code fences pass through untouched.
import { mkdir, readdir, readFile, rm, writeFile } from 'node:fs/promises';
import { dirname, join, relative } from 'node:path';
import process from 'node:process';
import { fileURLToPath } from 'node:url';
const PKG = join(dirname(fileURLToPath(import.meta.url)), '..');
const SRC = join(PKG, '../../apps/docs-site/src/content/docs');
const OUT = join(PKG, 'docs');
const attr = (tag, name) => new RegExp(`${name}="([^"]*)"`).exec(tag)?.[1];
export function flatten(mdx) {
// A fence closes on a line of the same backtick run, so ```` can hold ```.
const fence = /^[ \t]*(`{3,})[^\n]*\n[\s\S]*?^[ \t]*\1[ \t]*$/gm;
let out = '';
let last = 0;
for (const m of mdx.matchAll(fence)) {
out += flattenProse(mdx.slice(last, m.index)) + m[0];
last = m.index + m[0].length;
}
return out + flattenProse(mdx.slice(last));
}
function flattenProse(text) {
return text
.replace(/^import .+ from .+;?[ \t]*$/gm, '')
.replace(/\{\/\*[\s\S]*?\*\/\}/g, '')
.replace(/^<(script|style)\b[\s\S]*?(^\/>|<\/\1>)[ \t]*$/gm, '')
.replace(//g, (tag) => `- [${attr(tag, 'title')}](${attr(tag, 'href')})`)
.replace(/^[ \t]*]*>/gm, (tag) => `**${attr(tag, 'label')}**\n`)
.replace(/^[ \t]*