Repository navigation
[lexical-mdast][lexical-website] Bug Fix: Write only the literals from the first one that would read back differently in another form - #9970
Conversation
…m the first one that would read back differently in another form ## Description Follow-ups from the automated review of meta#9965: - When one literal in a paragraph, heading or table cell would read back differently, every literal in it was written in resource or autolink form, so typing `y` right after the third of `see www.a.example, www.b.example and www.c.example` rewrote all three. Now only the literals from the first link that does not read back as written are written again. The container is then read once more, unless that was every literal in it, and if it still does not read back, every literal in it is written again as before. Links are matched in order, so a link that reads back can stand in for an earlier literal with the same text that does not: in `xwww.example.com and www.example.com` the second link is taken for the first, and writing the second one again changes nothing. - The markdown-mdast docs now list such a literal among the forms that are normalized. They also say that the check only covers the paragraph, heading or table cell the literal is written in, so a letter that a contributed `paragraph` handler appends after it is not checked, as on `main`. - The changeset says what this means for contributed handlers: inline handlers run again each time a container is written again, and a contributed `root` handler runs inside the package's own, with the state as `this`. ## Changeset An edit to the pending `mdast-literal-neighbours` changeset; no new one. ## Test plan ### Before `TZ=UTC pnpm vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastImportExport.test.ts` with `MdastExport.ts` from `main`: ``` × writes only the literals from the first that would read back differently × writes every literal from the first that would read back differently AssertionError: expected 'see [www.a.example](http://www.a.exam…' to be 'see www.a.example, www.b.example and …' // Object.is equality AssertionError: expected 'see [www.a.example](http://www.a.exam…' to be 'see www.a.example, [www.b.example](ht…' // Object.is equality Tests 2 failed | 188 passed (190) ``` ### After ``` $ TZ=UTC pnpm vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastImportExport.test.ts Tests 190 passed (190) $ pnpm run test-unit Test Files 1027 passed | 6 skipped (1033) Tests 40429 passed | 423 expected fail | 174 skipped (41026) ``` `pnpm run tsc`, `pnpm run tsc-test`, `pnpm run flow` (0 errors), `pnpm run prettier`, `pnpm exec eslint` on the changed files, `pnpm run build` and `pnpm run check-changesets` pass. Writing every literal again, or starting after the first one that changed, fails `writes only the literals from the first that would read back differently`; writing only the first one that changed, or starting from the last one that changed, fails `writes every literal from the first that would read back differently`; not reading the container back a second time fails `writes a literal in resource form when it would not read back bare: a second literal with the same text`; reading it back a second time when every literal was written again fails `reads a container back once when its first literal is written again`. Across 3,000 fuzzed paragraphs with two to four literals and random neighbours, each exported in both `exportPunctuation` modes, every one of the 1,640 exports that changed has fewer literals written in another form, and all 6,000 read back as imported, as on `main`.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
lexical
@lexical/a11y
@lexical/clipboard
@lexical/code
@lexical/code-core
@lexical/code-prism
@lexical/code-shiki
@lexical/compiler
@lexical/devtools-core
@lexical/dragon
@lexical/eslint-plugin
@lexical/extension
@lexical/fast-check
@lexical/file
@lexical/hashtag
@lexical/headless
@lexical/history
@lexical/html
@lexical/internal
@lexical/link
@lexical/list
@lexical/mark
@lexical/markdown
@lexical/mdast
@lexical/offset
@lexical/overflow
@lexical/plain-text
@lexical/react
@lexical/rich-text
@lexical/selection
@lexical/table
@lexical/tailwind
@lexical/text
@lexical/utils
@lexical/y
@lexical/yjs
commit: |
There was a problem hiding this comment.
Thanks, this looks good. Writing only the literals from the first one that breaks, and falling back to every literal for the xwww.example.com and www.example.com case, reads correctly. The two new tests fail on main and pass here. Two optional, non-blocking suggestions:
-
A literal kept bare after a code span, an entity or
!is respelled on the next save (packages/lexical-mdast/src/MdastExport.ts:299). The partial rewrite is accepted when every link reads back with the same text and URL. But the GFM transform also recovers a literal after a code span, a character reference,!or), and a link found that way has no source position, so$importLinkstores nomdastLinkStyle(handlers.ts:455-461) and the next export writes it in resource form. To reproduce: importwww.a.example and www.b.example, then add inline codexbefore the first link andyafter the second. This branch exports`x`www.a.example and [www.b.example](http://www.b.example)y, and exporting the reimported document gives`x`[www.a.example](http://www.a.example) and [www.b.example](http://www.b.example)y.mainwrites the resource form on the first export. A reader that follows the GFM spec may not link the bare form at all, since extended autolinks are only recognized at the start of a line, after whitespace, or after*,_,~or(.mainalready has this gap when nothing else in the container breaks; this change just reaches it more often. One option is to give a link with nopositiona key that never matches inparse(MdastExport.ts:1589), so such a literal is written in resource form as before. The docs pagepackages/lexical-website/docs/serialization/markdown-mdast.md("Round-trips are close, not exact") lists link style among what export preserves, which is the contract involved. -
Assert the round trip in the new tests (
packages/lexical-mdast/src/__tests__/unit/MdastImportExport.test.ts:1371and:1398). They check the first export only. The table-driven test above them also reimports the output and checks a stable second export (:1222-1227). Doing the same here, with the code-span case from point 1 added, would pin the round trip.
…form where only GFM's transform reads it back as a link ## Description From the automated review of this PR: a literal right after a code span or `!` was kept bare, because GFM's transform reads it back as the same link. But micromark starts no literal there, so the link that comes back has no source position, the import records no link style for it, and the next export writes it in resource form. With `` `x` `` before the first of `www.a.example and www.b.example` and `y` after the second, the first export was `` `x`www.a.example and [www.b.example](http://www.b.example)y `` and the second `` `x`[www.a.example](http://www.a.example) and … ``, where `main` wrote the resource form at once. `main` has the same gap when nothing else in the container breaks. The container check now tells such a link apart: a link read back with no position gets its own key, and its literal is written again on its own, whatever its text and URL read back as. From the first link with a position that does not read back, every literal is written again as before. When no literal is left bare, the container is not read a second time. Giving such a link a key that never matches would also fix it, but it then writes every literal after it again too: more literals than this in 1,262 of 6,000 fuzzed exports, two or more in 492. The two tests for literals beside stand-ins put `⸀` right before the literal, where the literal is now written in resource form either way, so they no longer caught a nested export that does not restore the outer one's stand-ins. They now put it in the literal's path, `(www.example.com/a⸀b)`. The tests for the first changed literal also read the output back, as the table-driven tests do. ## Test plan ### Before `TZ=UTC pnpm vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastImportExport.test.ts` with `MdastExport.ts` from the previous commit: ``` × writes a literal in resource form when it would not read back bare: `!` before × writes a literal after a code span in resource form: the next literal keeps its bare form × writes a literal after a code span in resource form: a letter after it × writes a literal after a code span in resource form: before another literal that does not read back × reads a container back once when it writes again a literal that only GFM's transform links × reads a container back once when it writes again a literal that only GFM's transform links, beside a link AssertionError: expected '!www.example.com' to be '\ !www.example…' to be 'so [a](http://a.example) \` after `.`, `)`, `&`, `<`, `、`, an ideographic space, `"`, `:` or `!`; each output exports the same again.
There was a problem hiding this comment.
This looks good to me. The partial rewrite is only kept when the paragraph, heading or table cell reads back with every link intact, and otherwise falls back to today's output, and keying a link that comes back with no source position separately removes the one-save respelling after a code span or !. The new tests fail against main's MdastExport.ts and pass here, and each of the four mechanisms (the position-less key, the second read, the from-the-first slice, the skipped re-read) is pinned by at least one of them.
One optional, non-blocking suggestion:
packages/lexical-website/docs/serialization/markdown-mdast.md:128-131and.changeset/mdast-literal-neighbours.md:9: the sentence "GFM starts a literal only at the start of a line or after whitespace,*,_,~or(" follows the GFM spec's general wording for extended autolinks, butmicromark-extension-gfm-autolink-literal, which this package parses with, applies that restriction towww.literals only (previousWww, which also allows[and]). Ahttps://literal starts after anything but an ASCII letter (previousProtocol), and an email literal after anything but/or an address character (previousEmail). So on this branch!https://example.com,`x`https://example.comand[email protected]keep their bare form, and typing.or!before such a link does not change how it is written. Scoping the sentence would make both the docs page and the changeset exact, for example: "GFM starts awww.literal only at the start of a line or after whitespace,*,_,~,(,[or], so after.,!, a code span and most other characters only its transform links one." This refines the wording suggested in the earlier review comment, which took it from the spec. No code change is needed, since the export reads each container back rather than relying on the rule.
…teral starts to www. literals ## Description From the automated review of this PR: the docs and the changeset said GFM starts a literal only at the start of a line or after whitespace, `*`, `_`, `~` or `(`. micromark, which `@lexical/mdast` parses with, applies that rule to `www.` literals only, and also lets one start after `[` or `]`; a `https://` literal starts after anything but an ASCII letter, and an email literal after anything but `/` or an address character. Both now state the rule for `www.` literals. No code changes. ## Test plan ### After ``` $ pnpm exec prettier --check packages/lexical-website/docs/serialization/markdown-mdast.md .changeset/mdast-literal-neighbours.md All matched files use Prettier code style! $ pnpm run check-changesets Changeset check passed: 197 pending changeset(s) valid. ``` Importing `see <link>` and typing one character right before the link: after `!`, a code span or `.`, `www.example.com` is written as `[www.example.com](http://www.example.com)`, while `https://example.com` and `[email protected]` stay bare after `!` and a code span; after `]` all three stay bare. `previousWww`, `previousProtocol` and `previousEmail` in `micromark-extension-gfm-autolink-literal` give the same rules.
There was a problem hiding this comment.
This looks good to me. Scoping the GFM rule to www. literals, with [ and ] added, in both markdown-mdast.md and the changeset now matches what micromark-extension-gfm-autolink-literal does (an exported [ is always escaped to \[, which opens no label, so a literal right after it stays bare). The export change itself is as in the last round: a partial rewrite is kept only when the paragraph, heading or table cell reads back with every link intact and none found only by the transform, and otherwise falls back to today's output. The new tests fail against main's MdastExport.ts and pass here, and each mechanism they cover (the position-less key, the second read, the from-the-first slice, the skipped re-read) is pinned by at least one of them.
One optional, non-blocking nit, not worth a round on its own:
packages/lexical-website/docs/serialization/markdown-mdast.md:130-131: "after.,!, a code span and most other characters only its transform links one". The transform (previousinmdast-util-gfm-autolink-literal) links only at the start of a text node or after whitespace or punctuation, so after a letter or digit neither it nor micromark links the literal:xwww.example.comand7www.example.comparse with no link at all, while.www.example.com,!www.example.comand`c`www.example.comcome back as transform-only links. The export writes the resource form either way, so only the explanation is off. If you touch the sentence again, for example: "so after other punctuation such as.or!, after other whitespace or after a code span only its transform links one, and after a letter or digit neither does." This refines the wording suggested in the earlier review comment.
… literal right after a letter or digit ## Description From the automated review of this PR: the docs said that after `.`, `!`, a code span "and most other characters" only GFM's transform links a `www.` literal. After a letter or digit neither micromark nor the transform links one, since `previous` in `mdast-util-gfm-autolink-literal` only links at the start of a text node or after whitespace or punctuation. The sentence now names punctuation, other whitespace and a code span, and says that after a letter or digit neither does. The changeset only mentions `.` and `!`, so it stays. No code changes. ## Test plan ### After ``` $ pnpm exec prettier --check packages/lexical-website/docs/serialization/markdown-mdast.md All matched files use Prettier code style! ``` Parsing with the `mdast-util-from-markdown`, `micromark-extension-gfm-autolink-literal` and `mdast-util-gfm-autolink-literal` versions this package uses: `xwww.example.com`, `7www.example.com` and `éwww.example.com` give no link; `.www.example.com`, `!www.example.com`, `` `c`www.example.com `` and the literal after an ideographic or no-break space give a link with no source position (the transform's); `a www.example.com` gives a link with a position (micromark's).
There was a problem hiding this comment.
This looks good to me. Thanks for working through the follow-ups.
- The narrower rewrite in
checkLiterals(packages/lexical-mdast/src/MdastExport.ts:276-327) holds up. Both acceptance checks (:293,:321) now need every link to come back with a source position and the same key, which is stricter than before. The'\0'key for a link read back with no position (:1628-1630) can never equal alinkKey, and when the container still does not read back, the fallback respells every literal asmaindoes. - The new cases in
MdastImportExport.test.tsfail againstmain's exporter and pass here.expectLiteralRoundTripchecks the reimported runs and a stable second export, which pins the round trip the partial rewrite relies on. - The
www.start rule inmarkdown-mdast.mdand in the changeset matchespreviousWwwinmicromark-extension-gfm-autolink-literalandpreviousinmdast-util-gfm-autolink-literal, and the earlier docs-wording threads are addressed.
No further changes requested from my side.
Description
Follow-ups from the automated review of #9965:
yright after the third ofsee www.a.example, www.b.example and www.c.examplerewrote all three. Now only the literals from the first one that breaks are written again, and the container is read once more (skipped if that was all of them). If it still breaks, every literal in it is written again, as before. That last step is there because links are matched in order: inxwww.example.com and www.example.comthe second link is taken for the first, so writing the second one again changes nothing.paragraphhandler appends after it is not checked, as onmain.roothandler runs inside the package's own, with the state asthis.From this PR's own automated review: a literal right after a code span or
!was kept bare, because GFM's transform reads it back as the same link. But micromark starts no literal there, so it came back with no source position and no link style, and the next export wrote it in resource form.mainhas the same gap when nothing else in the paragraph breaks. Now a link read back with no position gets its own key, and only its literal is written again.Changeset
An edit to the pending
mdast-literal-neighbourschangeset; no new one.Test plan
Before
TZ=UTC pnpm vitest run --project unit packages/lexical-mdast/src/__tests__/unit/MdastImportExport.test.tswithMdastExport.tsfrommain:After
pnpm run tsc,pnpm run tsc-test,pnpm run flow(0 errors),pnpm run prettier,pnpm exec eslinton the changed files,pnpm run buildandpnpm run check-changesetspass. Writing every literal again, or starting after the first one that changed, failswrites only the literals from the first that would read back differently; writing only the first one that changed, or starting from the last one that changed, failswrites every literal from the first that would read back differently; not reading the container back a second time failswrites a literal in resource form when it would not read back bare: a second literal with the same text; reading it back a second time when every literal was written again failsreads a container back once when it writes again a literal that a letter after it breaks. Counting a link read back with no position as reading back failswrites a literal after a code span in resource form: the next literal keeps its bare form, and writing every literal from it on fails the same test. Across 3,000 fuzzed paragraphs with two to four literals and random neighbours, each exported in bothexportPunctuationmodes, all 6,000 read back as imported, as onmain; micromark reads 1,472 more links in them than inmain's output, and none fewer.