Skip to content

feat(code-block): {diff} fence attribute for unified-diff blocks (code-block Task 2.3) - #3034

Merged
jung-thomas merged 2 commits into
DEVfrom
worktree-diff-attribute
Nov 6, 2026
Merged

jung-thomas merged 2 commits into
DEVfrom
worktree-diff-attribute

Conversation

@jung-thomas

Copy link
Copy Markdown
Contributor

What / Why

Implements the deferred Task 2.3 from the tutorial-code-block-enhancements openspec: the {diff} fence attribute for unified-diff-format code blocks.

Fences tagged {diff} now interpret their content as a standard unified-diff body — leading + = added line, - = removed line, (space) = context line. The gutter column is stripped before Chroma highlighting, and the added/removed row styling reuses the exact same diff-added/diff-removed CSS classes and data-diff DOM shape that [!code ++]/[!code --] markers already produce. Copy behavior is automatic: the existing copyDiffFinal path excludes removed lines and yields the final code.

Commits

  • de3171d63 feat(code-block): {diff} fence attribute for unified-diff blocks (task 2.3)
  • 8e9e5c1d2 fix(code-block): always strip {diff} gutter even for context-only fences

The fix commit addresses a spec defect found in final review: when a {diff} fence contains only context lines (no +/-), the original implementation left $hasDiff = false and fell through to the plain render branch, highlighting .Inner (with gutter spaces) instead of $cleanInner (stripped). Fix introduces $isDiffFence flag so the stripped-source render path fires whenever {diff} is present, regardless of whether add/remove lines exist.

How tested

  • Unit tests: 9/9 passing in scripts/parsers/__tests__/diff-markers.test.ts (5 pre-existing + 3 for Task 2.3 + 1 for the context-only fix)
  • Regression: fences without {diff} are byte-identical — gated by strict index $attrs "diff" attribute-presence check
  • Integration: Hugo template logic is not unit-testable (no render harness per project convention); template-level verification requires a Hugo build
  • Reviewed by: task reviewer (Sonnet) + final whole-branch reviewer (Opus)

Deploy scope

Frontend/Hugo only — changes are in:

  • hugo/layouts/_default/_markup/render-codeblock.html
  • scripts/parsers/__tests__/diff-markers.test.ts

No CAP/backend changes. No new npm packages. No TypeScript runtime changes.

Introduce $isDiffFence (true whenever the {diff} attribute is present) so
that context-only {diff} fences — no + or - lines — still take the
$cleanInner highlight path and emit data-diff="true".  Previously $hasDiff
stayed false for context-only blocks, falling through to the plain .Inner
branch which left the leading space gutter in the output.

Changes:
- hugo/layouts/_default/_markup/render-codeblock.html: add $isDiffFence,
  use `or $hasDiff $isDiffFence` for the render branch and data-diff/
  data-copy-raw attributes.
- scripts/parsers/__tests__/diff-markers.test.ts: new test verifying
  context-only {diff} DOM shape passes through copyDiffFinal correctly.
@jung-thomas
jung-thomas merged commit f38b72e into DEV Nov 6, 2026
4 of 5 checks passed
@jung-thomas
jung-thomas deleted the worktree-diff-attribute branch November 6, 2026 16: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.

1 participant