Week 23 • Day 2Week 23
Day 2:Automated Markdown-to-Web Content Engine
Battle-tested daily lesson note • 100% runnable code, interview Q&A, test suite & interactive quiz
Day 2: Automated Markdown-to-Web Content Engine
1. Technical Requirements for AI Course Content
Rendering technical AI engineering documentation requires far more than basic Markdown (markdown -> html):
- KaTeX LaTeX Math Equations:
- Inline math:
$W_0 \in \mathbb{R}^{d \times k}$ - Display equations:
- Inline math:
- Mermaid Architectural Diagrams:
- Fenced code blocks (
mermaid ...) must be intercepted and wrapped in<div class="mermaid">for client-side SVG rendering.
- Fenced code blocks (
- GitHub-Style Alerts:
> [!NOTE],> [!TIP],> [!WARNING],> [!CAUTION]must render into styled alert callout boxes.
- Interactive Code Fences:
- Syntax-highlighted code blocks with language tags (
python,bash,json) and "Copy Code" pills.
- Syntax-highlighted code blocks with language tags (
- Dynamic Table of Contents (TOC):
- Extracted hierarchical headings (
h2,h3) with auto-generated anchor links for in-page navigation.
- Extracted hierarchical headings (
Architecture Flow Diagram
Raw Markdown File (README.md)->PARSER[Markdown Content Engine
TOC[TOC Extractor (H2/H3 Anchor Map)
ALERTS[Alert Callout Transformer (> [!NOTE])
MATH[LaTeX Formula Formatter ($...$ / $$...$$)
MERMAID[Mermaid Diagram Interceptor
CODE[Syntax-Highlighted Code Blocks
& ALERTS & MATH & MERMAID & CODE->HTML[Clean Accessible HTML AST
2. Security: XSS Sanitization & Safe Rendering
When rendering user-facing content from markdown, raw <script> tags, malicious javascript: URI links, or onload handlers must be neutralized.
A robust engine sanitizes all incoming HTML while preserving valid SVG diagrams, code fences, and LaTeX math spans.
In Simple English: Rendering Math (KaTeX) + Blocking Security XSS Attacks
In Web Markdown Engines, raw text passes through a clean pipeline:
- Parse Markdown headings/tables (
remark) -> parse$...$math formulas (remark-math->rehype-katex) -> sanitize HTML tags (XSS Defense)! - Why Sanitization Matters: If you render user-submitted Markdown or AI outputs via raw
dangerouslySetInnerHTML, an attacker can inject<script>stealCookies()</script>! Always sanitize or compile cleanly via safe React AST nodes (react-markdown).