Engineering a Knowledge Hub: Integrating Dynamic Docs, Mermaid, and Math
A deep dive into integrating dynamic documentation features like Mermaid diagrams and LaTeX math into our projects dashboard, alongside a major content migration and key lessons learned from common development hurdles.
Every great project needs great documentation. But what makes documentation truly great? It's not just about content; it's about accessibility, clarity, and the ability to convey complex ideas effortlessly. Recently, our mission was to elevate our project documentation experience, turning a static collection of files into a dynamic, interactive knowledge hub directly within our dashboard.
This session was all about bringing our internal intelligence documentation to life, integrating powerful rendering capabilities, and streamlining content management.
The Core Mission: A Three-Pronged Attack
Our primary goal for this development session was ambitious yet clear:
- Implement a dedicated "Projects Docs" tab: A central place for all project-related documentation.
- Unleash the power of Mermaid and Math: Enable rendering of Mermaid diagrams and LaTeX math formulas directly within our Markdown.
- Migrate and structure our intelligence docs: Move 20 critical documentation sections into a clean, GitHub-backed
docs/folder, each as an individual Markdown file.
By the end of the session, all targets were hit. The dev server purred on port 3000, and our documentation was ready for its grand debut.
Diving Deep: The Implementation Journey
Let's break down the key architectural and code changes that made this possible.
Backend: Powering Docs with GitHub Integration
Our backend, built with tRPC, needed to become the bridge between our application and our GitHub repositories.
In src/server/trpc/routers/projects.ts:
- We integrated
fetchRepoTreefrom ourgithub-connectorto efficiently list files. - A new
docssub-router was introduced, housing two crucial procedures:docs.list({ projectId }): This procedure now fetches the entire GitHub repository tree for a given project, filtering specifically for.mdfiles within thedocs/directory. It returns a concise list of{ path, name }pairs, ready for display.docs.get({ projectId, filePath }): Given a project and a specific file path, this procedure retrieves the raw content of the Markdown file. It intelligently extracts the primary title (from the first#heading) and a summary (the first paragraph) for quick previews, using a robust[\s\S]+?regex to handle multiline content.
Frontend: A Smarter Markdown Renderer
The true magic happens in src/components/markdown-renderer.tsx, our centralized Markdown processing component. This file received a significant upgrade:
- Math Rendering: We integrated
remark-mathandrehype-katexto transform$inline$and$$block$$LaTeX syntax into beautiful, rendered math equations. This is critical for explaining complex algorithms and formulas. - Mermaid Diagrams: To visualize workflows and architectures, we built a dedicated
MermaidDiagramcomponent. It uses dynamic imports formermaidto keep our bundle size lean, applies a dark theme for consistency, and renders Mermaid code blocks as SVG viadangerouslySetInnerHTML. - Intelligent Code Blocks: Our
CodeBlockWrappernow inspects the language specified for code blocks. If it detectslanguage-mermaid, it gracefully delegates rendering to our newMermaidDiagramcomponent. - GitHub Link Rewriting: A clever
RepoLinkcomponent was added to automatically rewrite internal source code references (e.g.,src/server/ordocs/) into proper GitHub blob URLs. This means links within our Markdown now point directly to the relevant files in the GitHub repo, improving navigability for developers. - Flexibility & Performance: We added optional
githubOwnerandgithubRepoprops toMarkdownRendererPropsto enable the link rewriting, and judiciously useduseMemofor the components object to prevent unnecessary re-renders.
UI: The "Projects Docs" Tab Comes Alive
The user interface for our new documentation hub was integrated directly into the project dashboard:
- In
src/app/(dashboard)/dashboard/projects/[id]/page.tsx:- We added the
BookOpenicon fromlucide-reactfor a visually appealing "Docs" tab. - The new
DocsTabcomponent was developed, managing three distinct states: a grid view of all available documents, a summary card view when a document is selected, and the full document view for reading. - A new
<TabsTrigger value="docs">and<TabsContent value="docs">were slotted into the dashboard after the existing "Actions" tab, making the docs easily accessible. - The
DocsTabcomponent orchestrates data fetching using our new tRPC procedures:trpc.projects.docs.listfor the document list andtrpc.projects.docs.getfor individual document content.
- We added the
Dependencies
To support these new features, we introduced a few key packages:
mermaid: For diagramming.remark-math,rehype-katex,katex: For LaTeX math rendering.
The Great Documentation Migration
Beyond the technical features, a significant part of this session involved content management. We successfully migrated all 20 sections of our core intelligence documentation into the new docs/ structure. These sections, previously scattered or in monolithic files, are now individual, discoverable Markdown files:
- Foundation: Sections
01-11were meticulously split from larger/tmp/nyxcore-doc-part{1,2,3}.mdfiles. - Core Systems: New sections like
12: Auto-Fix Pipeline,13: Refactor Pipeline,14: Code Analysis,15: Action Points, and16: Discussion Servicedetail the sophisticated inner workings of our platform, from multi-phase pipelines to LLM-driven analysis and collaborative discussion features. - Infrastructure & Analytics: Sections
17: GitHub Connector,18: Analytics Dashboard,19: Injection Diagnostics, and20: Sidebar & Active Processescover critical components like our BYOK GitHub integration, comprehensive analytics, context measurement, and UI navigation.
This structured approach makes our documentation not only accessible but also maintainable and extensible.
Lessons Learned: Navigating Development Hurdles
No development sprint is without its challenges. Here are a few "gotchas" and our effective workarounds:
-
NPM Cache Permissions (
EACCES):- Problem: Running
npm installafter anpm cache clean --forcewould consistently fail withEACCESerrors due to root-owned files in the~/.npm/_cacache/directory. Attemptingsudo chownin a non-interactive shell is a risky proposition. - Solution: We adopted a reliable workaround:
npm install --cache /tmp/npm-cache-nyxcore. This directs npm to use a temporary, user-owned cache directory, avoiding system-wide permission conflicts altogether. It's safe to delete after the install.
- Problem: Running
-
TypeScript and the
/sRegex Flag:- Problem: We initially tried to use the
/s(dotAll) regex flag in TypeScript (e.g.,/.+?/s) for multiline matching when extracting document summaries. This resulted in aTS1501error, indicating that the/sflag requires--target es2018or later, which wasn't our current configuration. - Solution: The classic workaround came to the rescue: replacing
.with[\s\S]. Our regex for extracting the first paragraph became/^([\s\S]+?)(?:\n\n|\n#|$)/, effectively matching any character, including newlines, without needing the/sflag.
- Problem: We initially tried to use the
-
LLM Context Window Limitations:
- Problem: When attempting to use a single large AI agent to merge three existing documentation parts and write nine new sections, we hit the dreaded "Prompt is too long" error. The sheer volume of source files exhausted the LLM's context window.
- Solution: We pivoted to a more modular approach: parallelizing agents with narrower scopes. We used separate agents for sections 12-16 versus 17-20 and even employed a dedicated Bash agent for the initial file splitting. This distributed the cognitive load, allowing the LLMs to operate within their context limits effectively.
What's Next?
With the core implementation complete, our immediate next steps involve thorough verification and minor enhancements:
- End-to-end Verification: Confirm the Docs tab functions perfectly on a live project with linked GitHub repos containing
/docs/*.mdfiles. - Mermaid Theme Check: Ensure Mermaid SVG renders correctly in dark theme, looking out for any Flash of Unstyled Content (FOUC) or double-initialization issues.
- Index Document: Consider adding a
docs/00-index.mdfile to serve as a README, listing all 20 sections with internal links for better navigation. - Existing Callsites: Verify that existing
MarkdownRenderercallsites (those not passinggithubOwner/githubRepo) continue to function correctly, as these props are optional.
This session marked a significant leap forward in our project's documentation capabilities. By integrating dynamic rendering, smart content management, and learning from our challenges, we've built a robust and engaging knowledge hub that will serve our team well into the future.