feat: add LLM-ready markdown export (append .md to any URL)#2403
feat: add LLM-ready markdown export (append .md to any URL)#2403
Conversation
Implements automatic generation of clean markdown files from MDX sources, making the docs easily consumable by LLMs and other tools. Users can now append .md to any documentation URL to get a clean markdown version. Changes: - Enhanced generate-llms.js to create individual .md files for each .mdx page - Integrated markdown generation into the build process via postbuild hook - Added cleanMdxContent function to strip JSX, imports, and MDX-specific syntax - Smart output directory selection (out/ for builds, public/ for dev) - Updated README with feature documentation Example usage: https://docs.celestia.org/learn/TIA/overview.md 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Summary of ChangesHello @jcstein, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the Celestia documentation by adding a mechanism to automatically generate LLM-friendly markdown files from the existing MDX source. This allows users and AI tools to easily consume the documentation in a simplified format. The changes include updates to the build process, a new content cleaning function, and adjustments to the documentation itself to reflect the new feature. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
🚀 Preview Deployment Your preview is ready: https://celestiaorg.github.io/docs-preview/pr-2403/ |
There was a problem hiding this comment.
Code Review
This pull request introduces a great feature for generating LLM-friendly markdown versions of documentation pages. The implementation is solid, with changes to the build script and process. I've provided a few suggestions in scripts/generate-llms.js to improve code quality and robustness: one to simplify the MDX cleaning logic, another to make path manipulation safer, and a third to reduce code duplication.
|
|
||
| // Handle page.mdx files - they should become index.md | ||
| if (outputPath.endsWith('/page.md')) { | ||
| outputPath = outputPath.replace('/page.md', '.md'); |
There was a problem hiding this comment.
Using String.prototype.replace() with a string argument only replaces the first occurrence. If a path contained /page.md earlier in the string (e.g., .../learn/page.md/overview/page.mdx), it could lead to an incorrect output path (.../learn.md/overview/page.md). It's safer to use a regular expression with an end-of-string anchor ($) to ensure you're only replacing the suffix.
outputPath = outputPath.replace(/\/page\.md$/, '.md');| function cleanMdxContent(content) { | ||
| // Remove frontmatter if present (between --- markers) | ||
| const frontmatterRegex = /^---\s*\n[\s\S]*?\n---\s*\n/; | ||
| const frontmatterMatch = content.match(frontmatterRegex); | ||
| let cleanedContent = content; | ||
| let frontmatter = ''; | ||
|
|
||
| if (frontmatterMatch) { | ||
| frontmatter = frontmatterMatch[0]; | ||
| cleanedContent = content.replace(frontmatterRegex, ''); | ||
| } | ||
|
|
||
| // Remove import statements | ||
| cleanedContent = cleanedContent.replace(/^import\s+.*?from\s+['"].*?['"];?\s*$/gm, ''); | ||
|
|
||
| // Remove export statements (except export default) | ||
| cleanedContent = cleanedContent.replace(/^export\s+(?!default).*?;?\s*$/gm, ''); | ||
|
|
||
| // Remove JSX/React components (custom components starting with capital letters) | ||
| cleanedContent = cleanedContent.replace(/<([A-Z][a-zA-Z0-9]*)\s*[^>]*\/>/g, ''); // Self-closing components | ||
| cleanedContent = cleanedContent.replace(/<([A-Z][a-zA-Z0-9]*)[^>]*>[\s\S]*?<\/\1>/g, ''); // Component blocks | ||
|
|
||
| // Remove MDX-specific syntax like {/* comments */} | ||
| cleanedContent = cleanedContent.replace(/\{\/\*[\s\S]*?\*\/\}/g, ''); | ||
|
|
||
| // Clean up extra whitespace | ||
| cleanedContent = cleanedContent.replace(/\n{3,}/g, '\n\n'); | ||
| cleanedContent = cleanedContent.trim(); | ||
|
|
||
| // Add frontmatter back if it existed | ||
| if (frontmatter) { | ||
| return frontmatter + cleanedContent; | ||
| } | ||
|
|
||
| return cleanedContent; | ||
| } |
There was a problem hiding this comment.
The current implementation of cleanMdxContent preserves the MDX frontmatter. While this might be intentional, frontmatter is not standard Markdown and could be confusing for LLMs. Additionally, the comment on line 10 ("Remove frontmatter...") is misleading as the code actually preserves it. For cleaner, more portable markdown, and simpler code, I suggest removing the frontmatter entirely.
function cleanMdxContent(content) {
// Remove frontmatter if present (between --- markers)
let cleanedContent = content.replace(/^---s*\n[\s\S]*?\n---s*\n/, '');
// Remove import statements
cleanedContent = cleanedContent.replace(/^import\s+.*?from\s+['"].*?['"];?\s*$/gm, '');
// Remove export statements (except export default)
cleanedContent = cleanedContent.replace(/^export\s+(?!default).*?;?\s*$/gm, '');
// Remove JSX/React components (custom components starting with capital letters)
cleanedContent = cleanedContent.replace(/<([A-Z][a-zA-Z0-9]*)\s*[^>]*\/>/g, ''); // Self-closing components
cleanedContent = cleanedContent.replace(/<([A-Z][a-zA-Z0-9]*)[^>]*>[\s\S]*?<\/\1>/g, ''); // Component blocks
// Remove MDX-specific syntax like {/* comments */}
cleanedContent = cleanedContent.replace(/{\/\*[\s\S]*?\*\/}/g, '');
// Clean up extra whitespace
cleanedContent = cleanedContent.replace(/\n{3,}/g, '\n\n');
return cleanedContent.trim();
}| console.log(`Found ${files.length} MDX files to convert`); | ||
|
|
||
| // Determine output directory - use 'out' if it exists (during build), otherwise 'public' (during dev) | ||
| const outputBase = await fs.access('out').then(() => 'out').catch(() => 'public'); |
There was a problem hiding this comment.
This logic to determine outputBase is duplicated in the main function at line 163. To adhere to the DRY (Don't Repeat Yourself) principle and improve maintainability, you could determine outputBase once in the main function and pass it as a parameter to generateMarkdownFiles. This would require changing the signature of generateMarkdownFiles to async (outputBase) and updating the call in main.
Summary
.mdto any documentation URL to get LLM-friendly markdownWhat's Changed
generate-llms.jsscript: Now generates individual.mdfiles for each.mdxpageyarn buildout/during build,public/during developmentHow It Works
Simply append
.mdto any documentation URL:https://docs.celestia.org/learn/TIA/overview→https://docs.celestia.org/learn/TIA/overview.mdhttps://docs.celestia.org/build/private-blockspace/quickstart→https://docs.celestia.org/build/private-blockspace/quickstart.mdBenefits
.mdto any existing URLTest Plan
yarn dev- markdown files are accessible via.mdURLsyarn build🤖 Generated with Claude Code