Skip to content

feat(core/diagrams): render Mermaid diagrams with Pele - #5269

Open
marcoscaceres wants to merge 38 commits into
speced:mainfrom
marcoscaceres:feat/diagrams
Open

marcoscaceres wants to merge 38 commits into
speced:mainfrom
marcoscaceres:feat/diagrams

Conversation

@marcoscaceres

@marcoscaceres marcoscaceres commented May 5, 2026 •

Copy link
Copy Markdown
Contributor

Draws a <pre class="mermaid"> as a card that flips: the diagram on the front and the text the author typed on the back. A library called Pele draws the diagram. Pele ships as its own file, respec-pele.js, and a page loads that file only when the page has a diagram. W3C has to serve respec-pele.js in the same folder as respec-w3c before diagrams work on w3.org. On 2026-11-25, www.w3.org answered 404 for Tools/respec/respec-pele.js. Until that file exists, a diagram stays as text and ReSpec shows a warning. One test draws a flowchart, a sequence, a class, a state and a pie diagram and checks that each gives an SVG. Claude wrote the changes in this pull request, and Gemini wrote the first version of the tests.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces first-class Mermaid diagram support to ReSpec by adding a new core/diagrams coordinator that detects pre.mermaid blocks, lazy-loads a Mermaid runtime bundle, renders diagrams to SVG, and provides a flip-card UI to view/copy the underlying source, plus linting/styling/test coverage updates.

Changes:

  • Added Mermaid rendering pipeline (runtime bundle + coordinator + renderer + runtime flip handler) and associated CSS for interactive diagrams.
  • Updated highlighting to exclude Mermaid source blocks and added a new linter rule for diagram placement.
  • Added integration tests and wired the new modules into the W3C profile + build/dependency graph.

Reviewed changes

Copilot reviewed 11 out of 15 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
worker/rollup.config.js Adds a separate Rollup output to build a dedicated Mermaid runtime bundle.
worker/respec-mermaid.js Implements the Mermaid runtime wrapper (initialize, render) and exposes it on self.
src/core/diagrams.js New coordinator that loads Mermaid, renders SVG, builds UI/error views, injects runtime, and emits warnings/errors.
src/core/diagrams/mermaid.js New “pure renderer” that delegates rendering to the injected runtime.
src/core/diagrams-runtime.js New exported runtime script that wires up flip-button behavior in exported documents.
src/styles/diagrams.css.js New styles for diagram flip-card UI, error presentation, reduced-motion, dark mode, and print.
src/core/highlight.js Excludes .mermaid blocks from syntax highlighting selection.
src/core/linter-rules/no-uncaptioned-diagram.js New linter rule to warn about Mermaid/Jake diagrams outside figures.
profiles/w3c.js Ensures core/diagrams and the new linter rule run in the W3C profile.
tests/spec/core/diagrams-spec.js Adds integration tests for Mermaid rendering, toolbar presence, errors, config, and highlighting behavior.
package.json Adds mermaid dependency.
pnpm-lock.yaml Locks Mermaid and transitive dependencies.
Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/core/diagrams.js Resolved
Comment thread src/core/diagrams.js Resolved
Comment thread src/core/diagrams.js Resolved
Comment thread src/core/diagrams.js Resolved
Comment thread src/core/diagrams.js Resolved
Comment thread tests/spec/core/diagrams-spec.js Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 14 out of 18 changed files in this pull request and generated 3 comments.

Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/core/diagrams.js Resolved
Comment thread profiles/w3c.js Resolved
Comment thread src/core/clipboard.js Resolved
Comment thread src/core/clipboard.js Resolved
Comment thread src/core/clipboard.js Resolved
Comment thread src/core/diagrams-runtime.js Resolved
Comment thread src/core/diagrams.js Resolved
Comment thread src/core/diagrams.js Resolved
Comment thread src/core/diagrams.js Resolved
Comment thread src/styles/diagrams.css.js Resolved
Comment thread worker/respec-mermaid.js Resolved
@marcoscaceres marcoscaceres added the AI Contains AI-generated logic; see AI_POLICY.md label Aug 5, 2026
@marcoscaceres
marcoscaceres force-pushed the feat/diagrams branch 2 times, most recently from 9b62003 to 4a0fa68 Compare August 19, 2026 12:45
@marcoscaceres
marcoscaceres requested a balanced review from Copilot August 19, 2026 23:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 13 out of 14 changed files in this pull request and generated 1 comment.

Files not reviewed (1)
  • pnpm-lock.yaml: Generated file
Suppressed comments (4)

src/styles/diagrams.css.js:104

  • visibility: hidden makes the toolbar buttons unfocusable, so keyboard users cannot focus a button to trigger the :focus-within rule that reveals the toolbar. Keep the toolbar visible to the accessibility/focus model (opacity can still hide it visually); then tabbing to a button will reveal it.
  visibility: hidden;

src/core/diagrams.js:302

  • The new public respecConfig.mermaid.theme option is deliberately omitted from Conf and hidden with a type suppression. This leaves the supported option undiscoverable to typed consumers and lets invalid config shapes bypass checking. Add a mermaid configuration type (including Mermaid's supported theme values) to src/type-helper.d.ts and remove this suppression.
  // @ts-expect-error -- mermaid config not in Conf type yet
  const theme = conf.mermaid?.theme || "neutral";

profiles/w3c.js:30

  • The feature is registered only in the W3C profile, although the PR exposes it as general Mermaid support and the generic core/diagrams module has no W3C-specific dependency. The AOM, DINI, and Geonovum profiles also include core/figures but will leave identical <pre class="mermaid"> blocks untouched. Register the module in the other profiles as well, or explicitly scope the feature to W3C.
  import("../src/core/diagrams.js"),

src/styles/diagrams.css.js:322

  • Printing a currently flipped card still applies rotateY(180deg) from .diagram-container--flipped .diagram-flip, while the back face is hidden below. Error cards start flipped, so they print with no visible face; user-flipped valid diagrams do as well. Override the transform in print mode, not just transform-style.
  .diagram-flip {
    transform-style: flat;
  }

Comment thread worker/respec-mermaid.js Resolved
@marcoscaceres
marcoscaceres marked this pull request as ready for review November 10, 2026 04:29
@sidvishnoi
sidvishnoi self-requested a review November 10, 2026 19:35
@marcoscaceres
marcoscaceres marked this pull request as draft November 21, 2026 02:39
@marcoscaceres marcoscaceres changed the title feat: add mermaid diagram rendering support feat(core/diagrams): render Mermaid diagrams with Pele Nov 21, 2026
@marcoscaceres
marcoscaceres marked this pull request as ready for review November 21, 2026 02:48
@marcoscaceres
marcoscaceres force-pushed the feat/diagrams branch 5 times, most recently from ba5c1a4 to 766d22f Compare November 24, 2026 03:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Diagram replacement loses export-removal classes, causing omitted content to be published, and sanitized SVG anchors can be misinterpreted as definition links.

2 open issues
1 resolved since last review
1 file not reviewed
  • pnpm-lock.yaml: Generated file

Review effort: Balanced

Comment thread src/core/diagrams.js Resolved
Comment thread src/core/diagrams.js Resolved
Comment thread src/core/diagrams-runtime.js Resolved
Comment thread src/core/highlight.js Resolved
Comment thread src/styles/diagrams.css.js Resolved
Comment thread src/core/diagrams.js
const card = html`<div
class="diagram-container"
role="group"
data-diagram-source="${source}"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why not copy textContent from pre below?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

An error card's pre also holds the line numbers, and can hold the error message, so its textContent is not the source. Both cards store the source in data-diagram-source, so the copy button reads the same place for either.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we skip the error text/line numbers (selector) from source to avoid duplicating in data-diagram-source?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

function getDiagramSource(preElement) {
  return Array.from(preElement.querySelectorAll('.dg-code:not(.dg-code--hint)'))
    .map(el => el.textContent)
    .join('\n');
}

Comment thread src/core/diagrams.js Resolved
Comment thread src/core/diagrams.js Outdated
}
}
card = buildDiagramBlock(source, svgs, caption?.id);
} catch (error) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe we should do following only for PeleError.

Comment thread src/core/diagrams.js Outdated
const front = `.diagram-face--front .${prefix}`;
// Pele draws labels at 14 to 16px, so three quarters keeps them at 10px
// or more.
switchRule = `@container (max-width: ${Math.floor(desktop.width * 0.75)}px) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why container query over a media query here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

And can we have a single container query instead of per-diagram?

Comment thread src/core/diagrams.js Resolved
Comment thread src/core/diagrams.js
Comment on lines +108 to +109
await loadScript(url);
if (self.pele) return true;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we use ESM for this instead of defining global pele?

Comment thread src/core/diagrams.js Outdated
* @param {HTMLElement} pre
* @returns {string} the rule that swaps its wide and phone layouts, or ""
*/
function drawDiagram(pre) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should this be done in a worker similar to respec-highlight?

Comment thread src/core/diagrams.js Outdated
Comment on lines +414 to +417
for (const { name, value } of pre.attributes) {
if (card.hasAttribute(name)) continue;
card.setAttribute(name, value);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Check if we've something in utils for this.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI Contains AI-generated logic; see AI_POLICY.md

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants