Skip to content

✨ feat(code): flag streaming code fences with data-streaming - #4

Open
Innei wants to merge 1 commit into
mainfrom
feat/streaming-fence-flag
Open

Innei wants to merge 1 commit into
mainfrom
feat/streaming-fence-flag

Conversation

@Innei

@Innei Innei commented Sep 26, 2026

Copy link
Copy Markdown
Member

Summary

  • Add rehypeStreamingFence: marks the trailing unclosed fenced code block's <pre> with data-streaming, so custom pre components can skip highlighting / diagram rendering until the fence closes.
  • Applied automatically by Streamdown, ahead of user rehype plugins.
  • Site CodeBlock reads the flag instead of the 180ms useSettled debounce; also stops briefly showing a stale highlight after a stream restart.

Why

Browser trace (production build, ~200 cps, 120-line TS fence): a pre component that re-highlights with shiki on every reveal commit spent 880ms in shiki vs 57ms total markdown parse β€” 43% of main-thread busy time, growing quadratically with fence length. Markdown re-parsing itself stays ~1% of wall time and is not the bottleneck.

Notes

  • Detection handles backtick/tilde fences, list indentation and blockquotes; an earlier fence implicitly closed by its container ending is not flagged.
  • A fence left unclosed at the very end of a stream stays flagged, since Streamdown has no end-of-stream signal.

Test plan

  • vitest run β€” 208 passed (13 new cases for the flag)
  • eslint + tsc for lib and site
  • Site Hero: code block stays plain while streaming, highlights once at full length after the fence closes

Mark the trailing unclosed fenced code block's <pre> with data-streaming so
custom pre components can defer highlighting until the fence closes. The
site's CodeBlock now reads the flag instead of a 180ms settle debounce.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
πŸ“ Code Review βœ… Completed 2026-09-26T06:32:33.422311Z 259ed06 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with πŸ‘€ while any review is running, comments if it has suggestions, and reacts with πŸ‘ once all reviews finish with no findings.

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying streamdown with Β Cloudflare Pages Β Cloudflare Pages

Latest commit: 259ed06
Status:Β βœ…Β  Deploy successful!
Preview URL: https://f562bffc.streamdown-6nu.pages.dev
Branch Preview URL: https://feat-streaming-fence-flag.streamdown-6nu.pages.dev

View logs

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

πŸ’‘ Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 259ed061ac

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with πŸ‘.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/fenceState.ts
return inFence ? lang : null;
};

const FENCE_OPEN_RE = /^[\t >]*(`{3,}|~{3,})/;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Reject indented code when detecting fence openers

When a trailing indented code block begins with four or more spaces followed by backticks, such as ```js\n const x = 1, Markdown parses it as an indented <pre>, not a fenced block. This regex nevertheless accepts unlimited leading whitespace, so the block receives data-streaming and consumers may wait indefinitely for a closing fence that does not apply. Restrict fence indentation to the Markdown-supported range rather than accepting arbitrary spaces or tabs.

Useful? React with πŸ‘Β / πŸ‘Ž.

Comment thread src/fenceState.ts
Comment on lines +48 to +49
const lastNewline = source.lastIndexOf('\n');
if (lastNewline === -1) return true;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Recognize carriage returns when locating the closing fence

For Markdown using CR-only line endings, such as `````js\rconst x = 1\r``` ``, the parser recognizes a complete fenced block, but lastIndexOf('\n') returns `-1` and this function unconditionally classifies it as unclosed. The resulting `

` remains marked `data-streaming`, preventing consumers such as the site highlighter from upgrading the completed block; handle `\r` line boundaries as well as `\n`.

Useful? React with πŸ‘Β / πŸ‘Ž.

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.

1 participant