Skip to content

fix(muya): preserve tight mixed bullet and task lists - #5254

Closed
DePasqualeOrg wants to merge 1 commit into
marktext:developfrom
DePasqualeOrg:codex/mixed-task-list-tightness
Closed

DePasqualeOrg wants to merge 1 commit into
marktext:developfrom
DePasqualeOrg:codex/mixed-task-list-tightness

Conversation

@DePasqualeOrg

@DePasqualeOrg DePasqualeOrg commented Sep 6, 2026 •

Copy link
Copy Markdown

This PR was developed with Codex using GPT-6 Astra and went through several rounds of automated review. It resolves a real issue that I encountered while using this library.

Summary

Muya stores adjacent bullet and task runs as separate blocks. This PR prevents serialization from inserting a blank line between tight runs while retaining authored loose spacing.

This input now round-trips unchanged:

- A bullet
- [ ] A task

Type of change

  • Bug fix (non-breaking, fixes an issue).
  • New feature (non-breaking, adds functionality).
  • Breaking change (causes existing functionality to change).
  • Documentation update.

Test plan

  • New tests added.
  • Manually tested on: Not performed.

From packages/muya, pnpm test --maxWorkers=2 passes 1,468 tests and pnpm test:spec --maxWorkers=2 passes 1,347 checks. pnpm lint:types, pnpm lint and pnpm check-circular pass, with eight existing lint warnings. Four tight-list regressions fail without the fix; loose-list controls pass.


By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.


Edit: Closes #5257.

@DePasqualeOrg
DePasqualeOrg marked this pull request as ready for review September 6, 2026 17:24
@Jocs

Jocs commented Sep 7, 2026

Copy link
Copy Markdown
Member

We require every fix to be associated with an issue first. I’ll close this PR for now. Please open a new issue to describe the problem, and then work on the fix based on that issue. Thanks for your understanding!

@DePasqualeOrg

DePasqualeOrg commented Sep 7, 2026 •

Copy link
Copy Markdown
Author

I opened #5257 with a description of the problem and reproduction steps.

@Jocs, please reopen this PR. I have a number of fixes that I think would be valuable to contribute here, starting with the two PRs that I opened today.

@Jocs Jocs reopened this Sep 7, 2026
@Jocs

Jocs commented Sep 7, 2026

Copy link
Copy Markdown
Member

I opened #5257 with a description of the problem and reproduction steps.

@Jocs, please reopen this PR. I have a number of fixes that I think would be valuable to contribute here, starting with the two PRs that I opened today.

You need link the related issue in the PR.

@Jocs

Jocs commented Sep 7, 2026

Copy link
Copy Markdown
Member

This might be the expected behavior, because MarkText treats bullet lists and task lists as separate types of lists. A tight list only applies within a single list; when there are two separate lists, a blank line is added between them.

@Jocs Jocs closed this Sep 7, 2026
@DePasqualeOrg

Copy link
Copy Markdown
Author

@Jocs, the change in this PR appears to be correct, based on Muya’s parser and serializer and the GFM spec. The following analysis includes the relevant references. I used Claude Code to produce it, so I’m not claiming it as my own, but please reopen this PR if you agree with the reasoning.


Muya does store these as two blocks, so the description of the internal model is accurate. What a blank line does not do is encode that boundary, for Muya's own parser or for a GFM parser.

Here is MarkdownToState output for each form. That parser passes the token's loose flag straight into each list's meta, and the fix is four lines in stateToMarkdown.ts that leave it untouched, so these are unchanged upstream results:

Input Parse
- A bullet\n- [ ] A task\n bullet-list {loose: false} + task-list {loose: false}
- A bullet\n\n- [ ] A task\n bullet-list {loose: true} + task-list {loose: true}
- A bullet\n\n<!-- -->\n\n- [ ] A task\n bullet-list {loose: false} + html-block + task-list {loose: false}
- one\n\n- two\n one bullet-list {loose: true} with two items

The first two rows are the problem. The parser records the authored list as tight, unchanged upstream serializes a blank line, and the same parser reads that back as loose. An unmodified round trip changes the document's spacing semantics inside Muya, independently of any question about spec conformance.

The serializer already treats a real list boundary as needing no separator: when the previous list used a different marker, it suppresses the blank line because the marker change by itself starts a new list. A blank line is therefore not the mechanism upstream uses to separate lists. The third row shows the mechanism that does work, and Muya already round-trips it with both lists tight, so two genuinely separate lists stay expressible and this PR does not affect them. The fourth row shows the same-marker rule already in effect: a blank line between plain bullet items keeps one list and marks it loose. The bullet-list/task-list split is a distinction in Muya's block tree rather than a Markdown list boundary.

The GFM specification (version 0.29-gfm) agrees on each point. Two items are of the same type when their markers use the same bullet character, so both belong to one list. A blank line between them makes that list loose rather than splitting it, and the same definition notes that paragraphs in a loose list are wrapped in <p> tags. Separating consecutive lists of the same type takes a blank HTML comment (example 288). A task list item is defined as a list item whose first block begins with a task list item marker, so the extension qualifies an item rather than introducing a container, and it cannot change which items share a list. GitHub's cmark-gfm follows this: the authored input renders as one tight <ul class="contains-task-list">, and the blank-line form renders as that same single <ul> with both items wrapped in <p>, which is a visible rendering change rather than a whitespace detail.

The fix only stops a blank line that was not authored from being inserted. Authored loose spacing is preserved, with regressions covering both orderings.

@Jocs

Jocs commented Sep 8, 2026

Copy link
Copy Markdown
Member

I double-checked this. GFM only defines task list items, which means they can be mixed with regular bullet list items. According to the specification, they should still belong to the same list.

MarkText uses marked as its Markdown parser, and marked also parses them as a single list. However, when MarkText converts the parsed result(tokens) into editor states(JSONStates), it splits them into two separate lists (which may be an editor bug? but the cost of fixing it is quite high).

This is also why, during the state-to-Markdown serialization process, an empty line needs to be inserted after the list.

@DePasqualeOrg

Copy link
Copy Markdown
Author

For the record, Claude Code doesn't agree. It's not ideal for me to proxy things from Claude Code in this discussion, so if this isn't working, I'll stop trying to submit improvements to Muya (of which I already have about a dozen more that are verified against the spec and other sources).


compatibleTaskList is what divides one marked list into bullet and task lists, and it starts a new list whenever listItemType !== cache.listType. That condition reads item types only – not loose, not blank lines. So - A bullet\n- [ ] A task\n, with no blank line, already parses to bullet-list + task-list. The split is re-derived from the markers on every parse; the blank line is not what preserves it.

What the blank line does change is looseness. Each split list copies loose from the parent marked token, and marked sets that when a blank line separates items, so serializing one turns a tight list loose on the next read. GitHub renders the difference as <p>-wrapped items, not just whitespace.

The serializer already treats a blank line as something other than a list separator – the line immediately above this fix suppresses it when the marker differs, since the marker change alone starts a new list:

if (lastListBullet && lastListBullet !== bulletMarkerOrDelimiter)
    insertNewLine = false;

The fix adds the same suppression for the tight case, in four lines. The tokens-to-state conversion is untouched, so the cost you were weighing against does not apply.

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.

[Bug] Muya inserts blank lines between adjacent bullet and task items

2 participants