Skip to content

Escape a paragraph line that would reparse as a table delimiter row - #19877

Open
Kjubikstronk wants to merge 2 commits into
prettier:mainfrom
Kjubikstronk:md-table
Open

Escape a paragraph line that would reparse as a table delimiter row#19877
Kjubikstronk wants to merge 2 commits into
prettier:mainfrom
Kjubikstronk:md-table

Conversation

@Kjubikstronk

Copy link
Copy Markdown
Contributor

Fixes #19847.

Description

A GFM delimiter row indented by four or more spaces is not a table, but after formatting it becomes one, so the second run produces different output:

<!-- Input -->
| x | y |
    |---|---|

<!-- Prettier stable -->
| x | y |
|---|---|

<!-- Prettier stable, second run -->
| x   | y   |
| --- | --- |

<!-- Prettier main -->
| x | y |
\|---|---|

Cause

remark only accepts a delimiter row indented up to three spaces, so at four the two lines are a plain paragraph and arrive as a single text node with the indentation already stripped. Printing a paragraph drops leading whitespace on continuation lines, which is insignificant everywhere else, but here it turns the text into a valid table.

Change

Escaping the delimiter row so it cannot be re-parsed, which is what the same file already does for the analogous case one line up:

// escape indented pseudo setext header, e.g. `Previous line↡␣␣␣␣===`
return `\\${text}`;

The issue asks for the table to be printed on the first run instead. That would change what the document renders as β€” the input is a paragraph, and a table is not the same output β€” so this keeps it a paragraph, consistent with the setext case.

The line is reconstructed from the sentence's sibling nodes rather than from options.originalText. A delimiter row is only |, -, : and spaces, so it is always contained in one sentence, and reading it from the AST means blockquote and list markers never enter the picture.

One thing I left out deliberately: GFM only forms a table when the header row and the delimiter row have the same number of cells, and this does not check that, so a delimiter row under a header with a different cell count is escaped although no table would have formed. Counting the header's cells means reconstructing a line that can span several nodes once it contains emphasis or code spans. No existing snapshot changed without it β€” happy to add it if you would rather have the exact rule.

Tests

tests/format/markdown/paragraph/issue-19847.md, which runs under all three proseWrap modes and covers the compact, spaced, aligned and no-outer-pipe delimiter shapes, a single-cell row, blockquote and list item nesting, and a trailing paragraph line. Two cases in it should not change: a three-space indent is a real table and still prints as one, and a delimiter row alone after a blank line is an indented code block.

tests/format/{markdown,mdx}: 1503 snapshots, none of the existing ones changed. Full suite is unchanged against a stashed tree β€” same four failing suites either way, patterns-dirs.js and three jsx suites that fail on main here too.

I also checked idempotency and meaning over 576 generated combinations of header shape, delimiter shape, indent width and container, comparing the parsed tree before and after formatting: no instability and no case where the escape changed what the document parses to.

Checklist

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory).
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.
  • I did not use AI to generate this PR.
  • (If the above is not checked) I have reviewed the AI-generated content before submitting.

This PR was written with AI assistance, and reviewed before submitting.

@netlify

netlify Bot commented Aug 17, 2026

Copy link
Copy Markdown

βœ… Deploy Preview for prettier ready!

Built without sensitive environment variables

Name Link
πŸ”¨ Latest commit 4c54e93
πŸ” Latest deploy log https://app.netlify.com/projects/prettier/deploys/6a8362b7fd50d00007f0b7bc
😎 Deploy Preview https://deploy-preview-19877--prettier.netlify.app
πŸ“± Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
πŸ€– Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Aug 17, 2026

Copy link
Copy Markdown

βœ… Deploy Preview for prettier ready!

Built without sensitive environment variables

Name Link
πŸ”¨ Latest commit b37d54a
πŸ” Latest deploy log https://app.netlify.com/projects/prettier/deploys/6a8362e478b0e80008f555c6
😎 Deploy Preview https://deploy-preview-19877--prettier.netlify.app
πŸ“± Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
πŸ€– Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

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.

markdown: row indented GFM table rendering idempotency

1 participant