Skip to content

fix(markdown-feed): harden markdown pipeline and feed rendering - #745

Open
markd3ng wants to merge 2 commits into
CuteLeaf:masterfrom
markd3ng:ff/markdown-feed
Open

markd3ng wants to merge 2 commits into
CuteLeaf:masterfrom
markd3ng:ff/markdown-feed

Conversation

@markd3ng

@markd3ng markd3ng commented Nov 29, 2026 •

Copy link
Copy Markdown

Summary

Markdown and feed pipeline cleanup covering eight audit findings.

  • FF-18 – Feed whitespace collapsing now uses a low-collision sentinel, restores protected blocks with a bounded lookup, and keeps unmatched sentinel text verbatim instead of writing undefined into RSS/Atom output.
  • FF-34 – new-post now emits a quoted, escaped YAML title and no longer leaves a stray trailing comma that broke frontmatter parsing for titles containing : or quotes.
  • FF-38 – Reading time is computed after the Mermaid and PlantUML remark plugins have cleared their source text, so diagram sources no longer inflate word counts or JSON-LD wordCount.
  • FF-39 – Excerpt extraction skips paragraphs rendered as custom elements (wiki cards) and paragraphs containing images, so card descriptions no longer collapse to alt text or card labels.
  • FF-40 – Only block-level images are converted to <figure> elements; inline images keep their paragraph, and the deprecated <center> wrapper is replaced by a figure rule and simplified .image-grid figure styles.
  • FF-41 – mailto: addresses are base64-encoded as UTF-8, with the browser decoder reading atob bytes through TextDecoder, so non-Latin-1 addresses no longer break builds.
  • FF-42 – Mermaid fence languages are matched case-insensitively, matching the PlantUML plugin.
  • FF-43 – Mermaid container detection normalizes both array and string className forms with exact token matching.

Files

  • astro.config.mjs, scripts/new-post.js, src/utils/feed-utils.ts
  • src/plugins/remark-excerpt.js, src/plugins/rehype-figure.mjs, src/plugins/rehype-email-protection.mjs, src/plugins/remark-mermaid.js, src/plugins/rehype-mermaid.mjs
  • src/styles/markdown.css

Verification

  • corepack pnpm run check: 0 errors, 0 warnings, 0 hints.
  • corepack pnpm exec astro build --force: 40 pages built successfully (41 with a temporary fixture post, removed afterward).
  • corepack pnpm exec biome check on the nine changed files: no fixes applied.
  • Runtime assertions (throwaway harness, removed after use): 5/5 PASS for excerpt skipping, block-only figure conversion, UTF-8 mailto round-trip, uppercase Mermaid fences, and className normalization.
  • Feed fixture build: literal sentinel text survives RSS/Atom rendering with no undefined; inline images stay inside <p>, block/grid images become <figure>, uppercase Mermaid fences render, and no <center> wrappers remain.
  • new-post fixture: generated frontmatter with a title containing :, quotes and a backslash parses as the original title.
  • Reading-time plugin order check: fixture word count drops from 55 (old order) to 42 (new order).

Sourcery 总结

加固 Markdown 和信息流处理流水线,确保在处理受保护内容、生成的元数据、图表、图片、摘要和国际化电子邮件地址时都能生成可靠的输出。

错误修复:

  • 加固信息流渲染、Markdown 摘要、图片处理、Mermaid 检测、电子邮件保护以及生成的文章 frontmatter,避免产生格式错误或具有误导性的输出。
  • 确保阅读时间和结构化数据中的字数统计不包含 Mermaid 和 PlantUML 源代码。

功能增强:

  • 现代化图片渲染:仅将独立成块的图片转换为 figure,同时保留行内图片,并移除已弃用的居中包装标签。
Original summary in English

Sourcery 摘要

加固 Markdown 和 feed 流水线,以可靠地生成摘要、元数据、图片、图表、邮件链接和联合发布内容。

错误修复:

  • 加固 feed 渲染,确保受保护的空白字符和字面内容保持原样,同时避免生成格式错误的输出。
  • 为包含引号、冒号或反斜杠的文章标题生成有效的 frontmatter。
  • 从阅读时长和字数统计元数据中排除图表源代码。
  • 生成摘要时跳过包含图片或自定义元素的段落。
  • 保留行内图片,同时将符合条件的独立图片转换为 figure,且不使用已弃用的居中包装元素。
  • 支持受保护 mailto 链接中的 UTF-8 国际邮箱地址。
  • 无论语言大小写或 className 表示形式如何,都能识别 Mermaid 代码围栏和容器。

增强功能:

  • 更新围绕 figure 元素的 Markdown 图片样式,并简化图片网格的呈现方式。
Original summary in English

Summary by Sourcery

Harden the Markdown and feed pipelines to produce reliable excerpts, metadata, images, diagrams, email links, and syndication output.

Bug Fixes:

  • Harden feed rendering so protected whitespace and literal content remain intact without producing malformed output.
  • Generate valid frontmatter for post titles containing quotes, colons, or backslashes.
  • Exclude diagram source from reading-time and word-count metadata.
  • Skip image- and custom-element paragraphs when generating excerpts.
  • Preserve inline images while converting eligible standalone images to figures without deprecated center wrappers.
  • Support UTF-8 international email addresses in protected mailto links.
  • Recognize Mermaid fences and containers regardless of language casing or className representation.

Enhancements:

  • Modernize markdown image styling around figure elements and simplify image-grid presentation.

- FF-18: use low-collision feed sentinels and preserve unmatched sentinels instead of emitting undefined
- FF-34: quote and escape new-post titles; drop stray YAML comma
- FF-38: compute reading time after diagram plugins clear source text
- FF-39: skip wiki-card and image paragraphs when deriving excerpts
- FF-40: convert only block images to figures, replacing deprecated center wrapper with CSS
- FF-41: base64-encode mailto addresses as UTF-8 and decode bytes in the browser
- FF-42: match Mermaid fence language case-insensitively
- FF-43: normalize mermaid-container className array/string forms
@netlify

netlify Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

👷 Deploy request for demo-firefly pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit cdac126

@sourcery-ai

sourcery-ai Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

审阅者指南

实现

折叠 feed 中的空白字符,但不要替换作者提供的类似哨兵值的文本,也不要破坏受保护的代码块。

  • src/utils/feed-utils.ts L64-71

生成摘要时,排除自定义渲染的段落和包含图片的段落。

  • src/plugins/remark-excerpt.js L3, L10-18

仅将独立的块级图片转换为 figure,并对受保护的电子邮件链接进行 UTF-8 安全的编码和解码。

  • src/plugins/rehype-email-protection.mjs L18, L50-52
  • src/plugins/rehype-figure.mjs L11-65, L74-78
  • src/utils/email-utils.ts L16

匹配 Mermaid 围栏语言时忽略大小写,并检测各种受支持 className 表示形式中的 Mermaid 容器。

  • src/plugins/rehype-mermaid.mjs L87-96
  • src/plugins/remark-mermaid.js L6

为生成的 YAML 标题加引号并进行转义,以便特殊字符不会改变文章的原始标题。

  • scripts/new-post.js L83

UI

直接为 figure 和图片网格 figure 添加样式,替代已移除的居中包装器样式。

  • src/styles/markdown.css L143-160

配置

在计算阅读时间之前运行 Mermaid 和 PlantUML 转换,以便图表源代码不会计入字数。

  • astro.config.mjs L298-299, L307(已移除的行)

提示和命令

与 Sourcery 互动

  • 触发新的审阅: 在拉取请求中评论 @sourcery-ai review。
  • 继续讨论: 直接回复 Sourcery 的审阅评论。
  • 根据审阅评论创建 GitHub issue: 回复审阅评论,请 Sourcery 根据该评论创建 issue。你也可以回复审阅评论并发送 @sourcery-ai issue,以根据该评论创建 issue。
  • 生成拉取请求标题: 在拉取请求标题的任意位置写入 @sourcery-ai,即可随时生成标题。你也可以在拉取请求中评论 @sourcery-ai title,以随时(重新)生成标题。
  • 生成拉取请求摘要: 在拉取请求正文中你希望摘要出现的位置写入 @sourcery-ai summary,即可随时生成 PR 摘要。你也可以在拉取请求中评论 @sourcery-ai summary,以随时(重新)生成摘要。
  • 生成审阅者指南: 在拉取请求中评论 @sourcery-ai guide,即可随时(重新)生成审阅者指南。
  • 解决所有 Sourcery 评论: 在拉取请求中评论 @sourcery-ai resolve,即可解决所有 Sourcery 评论。如果你已处理所有评论且不想再看到它们,这会很有用。
  • 忽略所有 Sourcery 审阅: 在拉取请求中评论 @sourcery-ai dismiss,即可忽略所有现有的 Sourcery 审阅。如果你想从头开始进行新的审阅,这尤其有用——别忘了评论 @sourcery-ai review 来触发新的审阅!

自定义使用体验

访问你的控制面板以:

  • 启用或停用审阅功能,例如 Sourcery 生成的拉取请求摘要、审阅者指南等。
  • 更改审阅语言。
  • 添加、移除或编辑自定义审阅说明。
  • 调整其他审阅设置。

获取帮助

Original review guide in English

Reviewer's Guide

Implementation

Collapse feed whitespace without replacing author-provided sentinel-like text or corrupting protected code blocks.

  • src/utils/feed-utils.ts L64-71

Exclude custom-rendered and image-containing paragraphs when deriving excerpts.

  • src/plugins/remark-excerpt.js L3, L10-18

Convert only standalone block images to figures, and apply UTF-8-safe encoding and decoding to protected email links.

  • src/plugins/rehype-email-protection.mjs L18, L50-52
  • src/plugins/rehype-figure.mjs L11-65, L74-78
  • src/utils/email-utils.ts L16

Match Mermaid fence languages without regard to case and detect Mermaid containers across supported className representations.

  • src/plugins/rehype-mermaid.mjs L87-96
  • src/plugins/remark-mermaid.js L6

Quote and escape generated YAML titles so special characters preserve the original post title.

  • scripts/new-post.js L83

UI

Style figures and image-grid figures directly, replacing the removed center-wrapper styling.

  • src/styles/markdown.css L143-160

Configuration

Run Mermaid and PlantUML transformations before reading-time calculation so diagram source is excluded from word counts.

  • astro.config.mjs L298-299, L307 (removed lines)

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai 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.

你好——我发现了 4 个问题

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/utils/feed-utils.ts" line_range="65" />
<code_context>
  */
 function collapseFeedWhitespace(html: string): string {
 	const protectedBlocks: string[] = [];
+	const sentinel = (index: number) => `@@FIREFLY_PRE_BLOCK_9d4f2a_${index}@@`;
 	const masked = html.replace(
 		/<pre[\s\S]*?<\/pre>|<code[\s\S]*?<\/code>/gi,
</code_context>
<issue_to_address>
**Literal feed text is replaced**

When rendered feed text contains a sentinel matching an index in the protected-block list, `collapseFeedWhitespace` restores every matching sentinel as a protected block, including sentinels that came from authored text. RSS and Atom content therefore show the block instead of the literal text.

Restore only placeholders inserted by the masking step, using placeholders that cannot collide with authored text.

Also at `src/utils/feed-utils.ts:76-80`.
</issue_to_address>

### Comment 2
<location path="src/plugins/rehype-figure.mjs" line_range="39-41" />
<code_context>
+					imgNode = child;
+				}
+			} else if (
+				node.tagName === "img" &&
+				parent.tagName !== "p" &&
+				parent.tagName !== "figure"
+			) {
+				imgNode = node;
</code_context>
<issue_to_address>
**Inline images break paragraph layout**

When a paragraph contains an image wrapped in a link or another inline element, `rehypeFigure` treats the nested image as block-level and replaces it with a `<figure>` inside its inline parent. This creates invalid flow-content nesting, breaks the linked image’s layout, and can add an unintended caption.

Only convert images to figures when they are block images, and leave images nested in inline elements unchanged.
</issue_to_address>

### Comment 3
<location path="src/plugins/rehype-email-protection.mjs" line_range="50-51" />
<code_context>
 		}
 		return `
-      const decodedEmail = atob(encodedEmail);
+      const decodedEmail = new TextDecoder().decode(
+        Uint8Array.from(atob(encodedEmail), (c) => c.charCodeAt(0)),
+      );
     `;
</code_context>
<issue_to_address>
**Non-ASCII mailto addresses are corrupted**

When a profile or banner mailto address contains non-ASCII characters, `MAILTO_ONCLICK_SCRIPT` decodes the base64 bytes with `atob` as Latin-1; the `TextDecoder` change in the rehype plugin does not affect these links. The browser opens a corrupted mailto address.

Update `MAILTO_ONCLICK_SCRIPT` to decode the base64 bytes as UTF-8.
</issue_to_address>

### Comment 4
<location path="astro.config.mjs" line_range="306" />
<code_context>
 					? [remarkAdmonitionToBlockquoteCallout]
 					: []),
 				remarkMath,
-				remarkReadingTime,
 				remarkWikiLink,
 				remarkImageGrid,
</code_context>
<issue_to_address>
**Wiki cards inflate reading counts**

When an article contains a standalone wiki link that resolves to a card, `remarkReadingTime` now runs after `remarkWikiLink` replaces standalone wiki-link paragraphs with cards, so `toString(tree)` counts the card title, description, and tags instead of just the source link; the displayed word count and JSON-LD `wordCount` are inflated.

Run reading-time calculation after the diagram plugins but before other plugins that replace or add text content.
</issue_to_address>

Sourcery 评估

需要人工审核。 有 4 个问题需要先处理;如果标题转义有误,新生成的 Markdown 文件在脚本回滚后仍可能保留格式错误的标题;这些文件数量有限,可以手动编辑或重新生成。其他更改主要影响渲染,可以通过回滚流水线更改来修正。

阻塞性问题:src/utils/feed-utils.ts:65、src/plugins/rehype-figure.mjs:41、src/plugins/rehype-email-protection.mjs:51、astro.config.mjs:306


Sourcery 对开源项目免费——如果您喜欢我们的审查意见,欢迎分享 ✨
Original comment in English

Hey - I've found 4 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/utils/feed-utils.ts" line_range="65" />
<code_context>
  */
 function collapseFeedWhitespace(html: string): string {
 	const protectedBlocks: string[] = [];
+	const sentinel = (index: number) => `@@FIREFLY_PRE_BLOCK_9d4f2a_${index}@@`;
 	const masked = html.replace(
 		/<pre[\s\S]*?<\/pre>|<code[\s\S]*?<\/code>/gi,
</code_context>
<issue_to_address>
**Literal feed text is replaced**

When rendered feed text contains a sentinel matching an index in the protected-block list, `collapseFeedWhitespace` restores every matching sentinel as a protected block, including sentinels that came from authored text. RSS and Atom content therefore show the block instead of the literal text.

Restore only placeholders inserted by the masking step, using placeholders that cannot collide with authored text.

Also at `src/utils/feed-utils.ts:76-80`.
</issue_to_address>

### Comment 2
<location path="src/plugins/rehype-figure.mjs" line_range="39-41" />
<code_context>
+					imgNode = child;
+				}
+			} else if (
+				node.tagName === "img" &&
+				parent.tagName !== "p" &&
+				parent.tagName !== "figure"
+			) {
+				imgNode = node;
</code_context>
<issue_to_address>
**Inline images break paragraph layout**

When a paragraph contains an image wrapped in a link or another inline element, `rehypeFigure` treats the nested image as block-level and replaces it with a `<figure>` inside its inline parent. This creates invalid flow-content nesting, breaks the linked image’s layout, and can add an unintended caption.

Only convert images to figures when they are block images, and leave images nested in inline elements unchanged.
</issue_to_address>

### Comment 3
<location path="src/plugins/rehype-email-protection.mjs" line_range="50-51" />
<code_context>
 		}
 		return `
-      const decodedEmail = atob(encodedEmail);
+      const decodedEmail = new TextDecoder().decode(
+        Uint8Array.from(atob(encodedEmail), (c) => c.charCodeAt(0)),
+      );
     `;
</code_context>
<issue_to_address>
**Non-ASCII mailto addresses are corrupted**

When a profile or banner mailto address contains non-ASCII characters, `MAILTO_ONCLICK_SCRIPT` decodes the base64 bytes with `atob` as Latin-1; the `TextDecoder` change in the rehype plugin does not affect these links. The browser opens a corrupted mailto address.

Update `MAILTO_ONCLICK_SCRIPT` to decode the base64 bytes as UTF-8.
</issue_to_address>

### Comment 4
<location path="astro.config.mjs" line_range="306" />
<code_context>
 					? [remarkAdmonitionToBlockquoteCallout]
 					: []),
 				remarkMath,
-				remarkReadingTime,
 				remarkWikiLink,
 				remarkImageGrid,
</code_context>
<issue_to_address>
**Wiki cards inflate reading counts**

When an article contains a standalone wiki link that resolves to a card, `remarkReadingTime` now runs after `remarkWikiLink` replaces standalone wiki-link paragraphs with cards, so `toString(tree)` counts the card title, description, and tags instead of just the source link; the displayed word count and JSON-LD `wordCount` are inflated.

Run reading-time calculation after the diagram plugins but before other plugins that replace or add text content.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 4 findings to address first, and if title escaping is wrong, newly generated Markdown files can retain a malformed title after the script is reverted; those files are bounded and can be edited or regenerated. The other changes primarily affect rendering and can be corrected by reverting the pipeline changes.

Blocking findings: src/utils/feed-utils.ts:65, src/plugins/rehype-figure.mjs:41, src/plugins/rehype-email-protection.mjs:51, astro.config.mjs:306


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread src/utils/feed-utils.ts Resolved
Comment thread src/plugins/rehype-figure.mjs Resolved
Comment thread src/plugins/rehype-email-protection.mjs Resolved
Comment thread astro.config.mjs Resolved
- Collapse feed whitespace by splitting protected blocks instead of using collision-prone sentinels.
- Skip inline image wrappers when creating figures.
- Decode UTF-8 mailto links in inline onclick handlers.
- Count reading time before wiki-card expansion but after diagram transforms.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant