Skip to content

fix(site): table pipe rendering + theme-safe mermaid diagrams#245

Merged
rohitg00 merged 1 commit into
mainfrom
site-fix-tables-diagrams
Jun 2, 2026
Merged

fix(site): table pipe rendering + theme-safe mermaid diagrams#245
rohitg00 merged 1 commit into
mainfrom
site-fix-tables-diagrams

Conversation

@rohitg00
Copy link
Copy Markdown
Owner

@rohitg00 rohitg00 commented Jun 2, 2026

Fixes the two most-reported website rendering bugs at the source, in the shared lesson renderer, so they apply across the whole curriculum.

Tables — closes #193

Lesson tables shattered when a cell contained an escaped pipe (P(X\|Y)) or a pipe inside an inline-code span. Root cause: the renderer split rows on every |.

  • New escape/code-aware row splitter: ignores \| (and unescapes it to |) and pipes inside backtick spans.
  • Each data row is normalized to the header column count, so a stray pipe can't add ghost columns.
  • parseMd now splits on /\r?\n/ so a CRLF checkout doesn't leak a trailing \r.

Verified on phases/02-ml-fundamentals/14-naive-bayes: 4 tables, balanced columns, P(A, B | C) = P(A | C) * P(B | C) renders intact, zero console errors.

Diagrams — closes #233

  • The block-beta protocol-landscape diagram in phases/16-multi-agent-and-swarms/03-communication-protocols did not render (showed "Diagram could not be rendered"). Replaced with a standard flowchart. Also converted the A2A lifecycle stateDiagram note from literal \n to block-note syntax.
  • Hard-coded node fills broke contrast: light pastels vanish on the dark default theme, and dark navies (#1a1a2e, used 190× across lessons) vanish in light mode. Added a hue-preserving HSL transform to the mermaid renderer — in dark mode light fills are darkened, in light mode dark fills are lightened. Color-coding survives; labels stay legible in both themes. Verified end-to-end: pastels render as deep same-hue fills in dark mode, navies render as pale same-hue fills in light mode.

Supersedes / relates to

Tables (closes #193):
- Parse table rows with an escape/code-aware splitter so escaped pipes
  (P(X\|Y)) and pipes inside inline-code spans no longer shatter cells
- Normalize each row to the header column count
- Split markdown on /\r?\n/ so CRLF docs do not leak a trailing \r

Diagrams (closes #233):
- Replace the non-rendering block-beta protocol landscape in
  phase 16/03 with a standard flowchart, and convert the stateDiagram
  note from literal \n to block-note syntax
- Add a hue-preserving HSL fill transform to the mermaid renderer:
  in dark mode light fills are darkened, in light mode dark fills are
  lightened, so hard-coded node colors stay legible in both themes
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 2, 2026

Review Change Stack

📝 Walkthrough

Walkthrough

The PR updates communication protocol documentation diagrams and enhances the in-browser markdown parser for robustness. Changes include refactoring Mermaid diagram syntax, supporting Windows newlines in markdown, improving table cell parsing to handle escaped pipes and inline code, and adding theme-aware color preprocessing for Mermaid diagrams.

Changes

Markdown Parser and Diagram Enhancements

Layer / File(s) Summary
Protocol Documentation Diagrams
phases/16-multi-agent-and-swarms/03-communication-protocols/docs/en.md
"Protocol Landscape" diagram converted from block-beta columns to flowchart TD layout, and "Task Lifecycle" completed state note expanded into multi-line format.
Markdown Table and Newline Parsing
site/lesson.html
parseMd function now handles Windows CRLF newlines via \r?\n regex, table rows are recognized with leading/trailing whitespace tolerance, new splitTableRow() helper correctly tokenizes cells while respecting escaped pipes (|) and inline code spans, and table generation iterates on header column count to prevent misalignment.
Mermaid Theme-Aware Color Preprocessing
site/lesson.html
Added hexToHsl(), hslToHex(), and mermaidPreprocess() utilities to dynamically rewrite Mermaid fill: colors by adjusting lightness based on light/dark page theme; integrated preprocessing into rerenderMermaidBlocks() and rerenderMermaidModal() rendering paths.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • rohitg00/ai-engineering-from-scratch#138: Adds Windows CRLF newline handling to the site's markdown parsing in a separate build script, addressing the same cross-platform newline compatibility concern in this PR's parseMd function.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the two main changes: table pipe rendering fixes and theme-safe mermaid diagrams, both of which are the primary focuses of the changeset.
Description check ✅ Passed The description is detailed and directly related to the changeset, covering both bug fixes with root causes, implementation approaches, verification steps, and related issues.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch site-fix-tables-diagrams

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
phases/16-multi-agent-and-swarms/03-communication-protocols/docs/en.md (1)

1-10: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Add required docs/en.md frontmatter block.

This file is missing the required YAML frontmatter (Title, one-line hook, Type, Languages, Prerequisites, Time estimate, and 4–6 Learning Objectives bullets). Please add it at the top before the H1.

Suggested patch
+---
+Title: Communication Protocols
+Hook: Learn how MCP, A2A, ACP, and ANP fit together to build robust multi-agent systems.
+Type: Build
+Languages: TypeScript
+Prerequisites: Phase 14 (Agent Engineering), Lesson 16.01 (Why Multi-Agent)
+Time estimate: 120 minutes
+Learning Objectives:
+  - Implement MCP tool discovery and invocation for external tool use.
+  - Build A2A Agent Card and task endpoints for agent delegation.
+  - Compare MCP, A2A, ACP, and ANP by problem scope and tradeoffs.
+  - Compose multiple protocols in one end-to-end system design.
+  - Apply protocol selection heuristics for production agent architectures.
+---
+
 # Communication Protocols

As per coding guidelines, "**/docs/en.md: Include frontmatter in docs/en.md with fields: Title, one-line hook, Type (Learn|Build|Reference), Languages (matching main.* files), Prerequisites, Time estimate, and 4-6 Learning Objectives bullet points."

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@phases/16-multi-agent-and-swarms/03-communication-protocols/docs/en.md`
around lines 1 - 10, Add a YAML frontmatter block at the very top of docs/en.md
(before the "# Communication Protocols" H1) containing Title, a one-line hook,
Type (Learn|Build|Reference), Languages (matching the repository's main.*
files), Prerequisites, Time estimate, and a Learning Objectives list of 4–6
bullet points; ensure the field names are exactly Title, one-line hook, Type,
Languages, Prerequisites, Time estimate, and Learning Objectives so the site
parser recognizes them and that the Learning Objectives describe concrete
outcomes related to communication protocols and multi-agent coordination.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@site/lesson.html`:
- Around line 2363-2392: splitTableRow currently unescapes backslash-escaped
pipes before checking inCode, so sequences like `\|` inside backtick spans
become `|`; change the logic so the handler for a backslash followed by '|' only
consumes the escape and appends '|' when not inside a code span (i.e., check
inCode before treating '\\' + '|' as an escape), and when inCode append the raw
backslash and '|' to cur; adjust the condition around the backslash handling in
function splitTableRow (variables: inCode, cur, ch) accordingly so escaped pipes
are preserved inside inline code.

---

Outside diff comments:
In `@phases/16-multi-agent-and-swarms/03-communication-protocols/docs/en.md`:
- Around line 1-10: Add a YAML frontmatter block at the very top of docs/en.md
(before the "# Communication Protocols" H1) containing Title, a one-line hook,
Type (Learn|Build|Reference), Languages (matching the repository's main.*
files), Prerequisites, Time estimate, and a Learning Objectives list of 4–6
bullet points; ensure the field names are exactly Title, one-line hook, Type,
Languages, Prerequisites, Time estimate, and Learning Objectives so the site
parser recognizes them and that the Learning Objectives describe concrete
outcomes related to communication protocols and multi-agent coordination.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 4238c9dd-be59-4187-9096-9244144ff979

📥 Commits

Reviewing files that changed from the base of the PR and between e4f2f8b and d571df8.

📒 Files selected for processing (2)
  • phases/16-multi-agent-and-swarms/03-communication-protocols/docs/en.md
  • site/lesson.html

Comment thread site/lesson.html
Comment on lines +2363 to +2392
function splitTableRow(line) {
// Split a markdown table row on cell delimiters while respecting
// escaped pipes (P(X\|Y)) and pipes inside inline-code spans (`a | b`).
var cells = [];
var cur = '';
var inCode = false;
for (var i = 0; i < line.length; i++) {
var ch = line.charAt(i);
if (ch === '\\' && line.charAt(i + 1) === '|') {
cur += '|';
i++;
continue;
}
if (ch === '`') {
inCode = !inCode;
cur += ch;
continue;
}
if (ch === '|' && !inCode) {
cells.push(cur);
cur = '';
continue;
}
cur += ch;
}
cells.push(cur);
if (cells.length && cells[0].trim() === '') cells.shift();
if (cells.length && cells[cells.length - 1].trim() === '') cells.pop();
return cells.map(function (c) { return c.trim(); });
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Preserve \| inside inline code spans.

splitTableRow() unescapes \| before it checks whether the parser is currently inside backticks, so a cell like `\|` is rendered as `|`. That still mutates valid Markdown content in the exact case this helper is supposed to protect.

Suggested fix
       for (var i = 0; i < line.length; i++) {
         var ch = line.charAt(i);
-        if (ch === '\\' && line.charAt(i + 1) === '|') {
-          cur += '|';
-          i++;
-          continue;
-        }
         if (ch === '`') {
           inCode = !inCode;
           cur += ch;
           continue;
         }
+        if (!inCode && ch === '\\' && line.charAt(i + 1) === '|') {
+          cur += '|';
+          i++;
+          continue;
+        }
         if (ch === '|' && !inCode) {
           cells.push(cur);
           cur = '';
           continue;
         }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
function splitTableRow(line) {
// Split a markdown table row on cell delimiters while respecting
// escaped pipes (P(X\|Y)) and pipes inside inline-code spans (`a | b`).
var cells = [];
var cur = '';
var inCode = false;
for (var i = 0; i < line.length; i++) {
var ch = line.charAt(i);
if (ch === '\\' && line.charAt(i + 1) === '|') {
cur += '|';
i++;
continue;
}
if (ch === '`') {
inCode = !inCode;
cur += ch;
continue;
}
if (ch === '|' && !inCode) {
cells.push(cur);
cur = '';
continue;
}
cur += ch;
}
cells.push(cur);
if (cells.length && cells[0].trim() === '') cells.shift();
if (cells.length && cells[cells.length - 1].trim() === '') cells.pop();
return cells.map(function (c) { return c.trim(); });
}
function splitTableRow(line) {
// Split a markdown table row on cell delimiters while respecting
// escaped pipes (P(X\|Y)) and pipes inside inline-code spans (`a | b`).
var cells = [];
var cur = '';
var inCode = false;
for (var i = 0; i < line.length; i++) {
var ch = line.charAt(i);
if (ch === '`') {
inCode = !inCode;
cur += ch;
continue;
}
if (!inCode && ch === '\\' && line.charAt(i + 1) === '|') {
cur += '|';
i++;
continue;
}
if (ch === '|' && !inCode) {
cells.push(cur);
cur = '';
continue;
}
cur += ch;
}
cells.push(cur);
if (cells.length && cells[0].trim() === '') cells.shift();
if (cells.length && cells[cells.length - 1].trim() === '') cells.pop();
return cells.map(function (c) { return c.trim(); });
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@site/lesson.html` around lines 2363 - 2392, splitTableRow currently unescapes
backslash-escaped pipes before checking inCode, so sequences like `\|` inside
backtick spans become `|`; change the logic so the handler for a backslash
followed by '|' only consumes the escape and appends '|' when not inside a code
span (i.e., check inCode before treating '\\' + '|' as an escape), and when
inCode append the raw backslash and '|' to cur; adjust the condition around the
backslash handling in function splitTableRow (variables: inCode, cur, ch)
accordingly so escaped pipes are preserved inside inline code.

@rohitg00 rohitg00 merged commit a5285f6 into main Jun 2, 2026
6 checks passed
@rohitg00 rohitg00 deleted the site-fix-tables-diagrams branch June 2, 2026 23:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] Not rendered diagram under phase 16 communication protocols [bug] Tables messed up

1 participant