Files
goose/documentation/plugins/markdown-export.cjs
T
Emma Youndtsmith 043e4a741e markdown export from URL (#5830)
* Add Markdown export of any doc page by appending `.md` to the URL
* New "View as Markdown" dropdown menu from the "copy page" button
2025-11-21 15:37:41 -06:00

62 lines
1.8 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const globby = require('globby');
module.exports = function markdownExportPlugin(context, options) {
const pluginOptions = {
enabled: true,
...options,
};
return {
name: 'markdown-export',
async postBuild({ outDir }) {
if (!pluginOptions.enabled) {
return;
}
console.log('[markdown-export] Starting markdown export...');
const docsDir = path.join(context.siteDir, 'docs');
const outputDir = path.join(outDir, 'docs');
// Get all markdown files
const files = await globby('**/*.{md,mdx}', { cwd: docsDir });
for (const file of files) {
const inputPath = path.join(docsDir, file);
const outputPath = path.join(outputDir, file.replace('.mdx', '.md'));
// Ensure output subdirectory exists
fs.mkdirSync(path.dirname(outputPath), { recursive: true });
const content = fs.readFileSync(inputPath, 'utf-8');
// Strip frontmatter and clean up
const cleaned = stripFrontmatter(content);
// Write the cleaned markdown alongside HTML files
fs.writeFileSync(outputPath, cleaned);
}
console.log(`[markdown-export] Successfully exported ${files.length} markdown files to ${outputDir}`);
},
};
};
function stripFrontmatter(content) {
// Remove YAML frontmatter (everything between --- at the start)
const withoutFrontmatter = content.replace(/^---\s*\n[\s\S]*?\n---\s*\n/, '');
// Clean up any remaining import statements (for .mdx files)
const withoutImports = withoutFrontmatter.replace(/^import .+$/gm, '');
// Remove excessive empty lines and trim
return withoutImports
.replace(/\n{3,}/g, '\n\n')
.trim();
}