Skip to content

feat(MD013): keep two spaces after sentences when reflowing - #979

Merged
rvben merged 1 commit into
rvben:mainfrom
yzhou216:md013-reflow-sentence-double-space
Nov 9, 2026
Merged

rvben merged 1 commit into
rvben:mainfrom
yzhou216:md013-reflow-sentence-double-space

Conversation

@yzhou216

@yzhou216 yzhou216 commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds reflow-sentence-double-space to MD013 (default false). With it on, reflow keeps the two spaces some authors put after the end of a sentence instead of collapsing every gap to one.

Two spaces after a sentence mark its end apart from an abbreviation (e.g. this versus this. That). #294 added MD064's allow-sentence-double-space for this convention, so check accepts the spacing, but fmt with reflow = true still rejoins every gap with a single space. Any paragraph that gets rewrapped loses the spacing, and the only way to keep it is to leave reflow off.

Behavior

With reflow-sentence-double-space = true:

  • Same-line gaps: two or more spaces after the end of a sentence stay two spaces when the words end up on the same line. A sentence end is what MD064's allow-sentence-double-space accepts: the same is_after_sentence_ending check, with the built-in abbreviations plus MD013's abbreviations. So fmt keeps exactly the double spaces that check allows.
  • Joined line breaks: a line break that ends a sentence with ., ! or ? becomes two spaces when reflow joins the lines. The same splitter that sentence-per-line uses to keep source sentence breaks decides where a sentence ends. A break after an abbreviation such as e.g. joins with one space, and CJK sentence enders take no extra space.
  • Other gaps: everything else is still one space: mid-sentence double spaces, a double space after Dr., and so on.
  • sentence-pack: this mode separates the sentences it packs onto a line with two spaces after ., ! or ?.
  • Line ends: a line never ends in the spaces, which would make a hard break. The extra space counts toward the line width.
The first sentence of this paragraph runs on for quite a while.  The second sentence follows two spaces later, and the line goes well past eighty columns.

A short first sentence.
The second sentence is long enough that the whole paragraph needs to be reflowed to fit.

becomes

The first sentence of this paragraph runs on for quite a while.  The second
sentence follows two spaces later, and the line goes well past eighty columns.

A short first sentence.  The second sentence is long enough that the whole
paragraph needs to be reflowed to fit.

With the option off, which is the default, output is unchanged.

Implementation

  • Tokenizing: reflow_elements tokenizes text with the gap in front of each word (split_breakable_words_with_gaps). Each place that wrote a single space now writes gap_separator(...), which is one or two spaces. Its width goes into the overflow checks and the early-break paths (break_before_attached). Gaps in front of links, code spans and emphasis use the trailing gap of the preceding text element.
  • Joining: join_soft_break_lines_with_sentences now takes what a sentence-ending break becomes (SentenceJoin::Break for sentence-per-line, SentenceJoin::DoubleSpace for this option). The new join_double_spaced_sentence_lines is used by join_reflow_lines and by MD013's join_paragraph_lines.
  • Sharing MD064's check: is_after_sentence_ending_with_abbreviations in sentence_utils is now pub(crate), so reflow shares MD064's sentence-end check.

Tests

  • Unit tests for the join, the separator and the tokenizer in text_reflow.rs, plus a config parse test.
  • tests/regressions/md013_reflow_sentence_double_space_test.rs runs fmt end to end. It covers same-line gaps, joined breaks, abbreviations and mid-sentence gaps, the default-off behavior, a sentence end at the wrap point, a link after a sentence, and sentence-pack. Every case asserts that check with MD064's allow-sentence-double-space is clean on the output and that a second fmt pass changes nothing.
  • make test-dev passes (17,007 tests), as do the clippy step of make lint and make docs-check, on the pinned 1.96.0 toolchain.

Add `reflow-sentence-double-space` (default false). Two spaces after a
sentence mark its end apart from an abbreviation, and MD064's
`allow-sentence-double-space` already accepts them, but reflow rejoined
every gap with a single space, so rewrapping a paragraph lost the
spacing.

With the option on, two or more spaces after the end of a sentence, as
MD064 recognizes it, stay two spaces where the words share a line after
reflow. A line break that ends a sentence with `.`, `!` or `?`, as the
sentence-per-line splitter finds it, becomes two spaces where reflow
joins the lines, and sentence-pack separates the sentences it packs the
same way. Every other gap is still one space, and a line never ends in
the spaces, which would make a hard break.

Assisted-by: Claude Code (Claude Opus 5.5)
@yzhou216
yzhou216 force-pushed the md013-reflow-sentence-double-space branch from 0decfc3 to d793950 Compare November 2, 2026 09:35
@rvben
rvben merged commit c89b5e6 into rvben:main Nov 9, 2026
@yzhou216

yzhou216 commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for merging this so quickly. Would you mind tag the HEAD of main? I'd like to bump the version in Nixpkgs.

@rvben

rvben commented Nov 10, 2026

Copy link
Copy Markdown
Owner

@yzhou216 Thanks for this, and for the careful tests: asserting that check is clean and that a second fmt changes nothing is exactly the right bar. It's merged and will ship in the next release.

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