Repository navigation
fix(markdoc): escape text that would be read back as a list, emphasis or tag - #1648
Open
ChangkeunJ wants to merge 1 commit into
Open
ChangkeunJ wants to merge 1 commit into
ChangkeunJ wants to merge 1 commit into
Conversation
… or tag
Markdoc's formatter only escapes a leading *, # or > in a text node and
*, _ and ~ directly inside strong, em and s. So a paragraph stored as
"2026\. A turning point" was saved back as "2026. A turning point" and
parsed as an ordered list the next time, "\- not a list" became a
bullet, "a \*literal\* word" became emphasis and "\{% braces %}" became
an unknown tag that stopped the entry from opening again.
Every format call in the package goes through src/markdoc.js, so wrap
format there: walk the AST first and escape {%, * and _ outside
strong/em/s, and a bullet or ordered list marker at the start of a line.
Headings, snake_case and a * or _ with spaces on both sides are left
alone. The original text is restored after formatting.
Fixes Thinkmill#1628
🦋 Changeset detectedLatest commit: f0d42af The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1628.
Markdoc's formatter only escapes a leading
*,#or>in a text node, plus*,_and~directly inside strong, em and s. So when the markdoc or document field saves, a paragraph stored as2026\. A turning pointcomes back as2026. A turning pointand is an ordered list on the next parse,\- not a listturns into a bullet,a \*literal\* wordturns into emphasis and\{% braces %}turns into an unknown tag that stops the entry from opening again (same symptom as #1428).Every
formatcall in the package already goes throughsrc/markdoc.js, so this wrapsformatthere. Before formatting it walks the AST and escapes the text that would otherwise be read back as syntax:{%everywhere,*and_outside strong, em and s (Markdoc already escapes those inside), and a-,+,1.or1)marker at the start of a line. Headings are skipped so## 1. Introdoesn't change, a_between two word characters is skipped sosnake_casedoesn't churn, and a*or_with spaces on both sides can't open or close emphasis so it's skipped too. The original text is put back after formatting, so the caller's AST isn't changed.One thing to be aware of: existing content with a bare
*or_next to a word will pick up a backslash on its next save. It renders the same, but it is a diff, so I kept the rule as narrow as I could. A literal backslash, backticks and~~outside an s node aren't covered; I left those out rather than guess at what the editor should do with them.The new test in
markdoc.test.tsxhas the cases from the issue and fails on main. The keystatic test suite, tsc, eslint and prettier pass with the change.