Skip to content

fix: don't de-indent HTML inside fenced code blocks - #130

Open
reidransom wants to merge 1 commit into
osteele:mainfrom
reidransom:fix-deindent-fenced-code-blocks
Open

fix: don't de-indent HTML inside fenced code blocks#130
reidransom wants to merge 1 commit into
osteele:mainfrom
reidransom:fix-deindent-fenced-code-blocks

Conversation

@reidransom

Copy link
Copy Markdown

Hello- love this repo, I'm using it on all my websites. This is my first PR. Let me know if I can make any changes or missed any guidelines. Thank you!

deIndentHTMLBlocks operates on raw markdown lines before Goldmark parses them, so a line like

    inside a ``` fence started an "HTML block" and the following indented code-sample lines lost their leading spaces in the rendered
     output.

    Track fence state (``` and ~~~) and leave fenced content untouched.

@osteele

osteele commented Aug 12, 2026

Copy link
Copy Markdown
Owner

Thanks for the clear fix and tests. I found one blocking fence-matching edge case: inCodeFence = !inCodeFence treats every matching fence line as the closing delimiter. A four-backtick fence may legitimately contain a triple-backtick line, and a backtick fence may contain ~~~; both currently end fence tracking early and allow HTML indentation to be stripped.

For example, the outer fence here uses four backticks, so the triple-backtick lines are content:

````html
```
<ul>
    <li>item</li>
</ul>
```
````

The current implementation toggles fence tracking off at the first triple-backtick line and changes <li>item</li> to <li>item</li>.

Please track the opening delimiter character and length, and only close on the same character with at least that length. Regression tests for longer outer fences and mixed delimiters would cover this.

@osteele osteele left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks for the clear fix and tests. Re-filing my earlier comment as a formal review so the status is visible from your side — the substance is unchanged.

inCodeFence = !inCodeFence treats every matching fence line as the closing delimiter. A four-backtick fence may legitimately contain a triple-backtick line, and a backtick fence may contain ~~~; both currently end fence tracking early and allow HTML indentation to be stripped.

Please track the opening delimiter's character and length, and only close on the same character with at least that length.

I ran the two cases against this branch's deIndentHTMLBlocks to confirm they reproduce:

FAIL  four-backtick outer fence containing ``` lines
FAIL  backtick fence containing ~~~ line
PASS  plain triple-backtick fence (this PR's own test)

In both failing cases <li>item</li> loses its indentation. Here are the two cases in the style of the existing table, ready to drop into TestDeIndentHTMLBlocks:

{
    name:  "longer outer fence containing shorter fence lines",
    input: "````html\n```\n<ul>\n    <li>item</li>\n</ul>\n```\n````\n",
    check: func(t *testing.T, result string) {
        require.Contains(t, result, "    <li>item</li>")
    },
},
{
    name:  "backtick fence containing tilde fence lines",
    input: "```\n~~~\n<ul>\n    <li>item</li>\n</ul>\n~~~\n```\n",
    check: func(t *testing.T, result string) {
        require.Contains(t, result, "    <li>item</li>")
    },
},

One other thing to pick up when you push: the branch now conflicts with main, but only in CHANGELOG.md, where both sides added entries under ## [Unreleased]. The code changes still merge cleanly, so it should be a quick rebase.

The rest of the change is good — the approach is right and the existing tests cover the common case well. Thanks for the contribution, and sorry for the slow signal on this one; my earlier note was a plain comment, so GitHub never showed you that it was blocking.

@reidransom
reidransom force-pushed the fix-deindent-fenced-code-blocks branch from 5ce090c to 0f44f0d Compare August 16, 2026 13:30
deIndentHTMLBlocks operates on raw markdown lines before Goldmark parses
them, so a line like <ul> inside a ``` fence started an "HTML block"
and the following indented code-sample lines lost their leading spaces
in the rendered <pre> output.

Track fence state (``` and ~~~) and leave fenced content untouched.
@reidransom
reidransom force-pushed the fix-deindent-fenced-code-blocks branch from 0f44f0d to be714ee Compare August 16, 2026 14:11
@reidransom

Copy link
Copy Markdown
Author

Thank you for the detailed feedback and test cases. Made the following updates,

  1. Track opening fence marker and length
  2. Close only on the same marker
  3. Require closing-fence trailing content to be whitespace only. Per CommonMark (https://spec.commonmark.org/0.31.2/#fenced-code-blocks), closing fences may be followed only by spaces or tabs.

Thank you!

@reidransom
reidransom requested a review from osteele August 16, 2026 14:19
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.

2 participants