Repository navigation
feat(MD013): keep two spaces after sentences when reflowing - #979
Merged
Merged
Conversation
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
force-pushed
the
md013-reflow-sentence-double-space
branch
from
November 2, 2026 09:35
0decfc3 to
d793950
Compare
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. |
Owner
|
@yzhou216 Thanks for this, and for the careful tests: asserting that |
4 of 13 tasks
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.
Summary
Adds
reflow-sentence-double-spaceto MD013 (defaultfalse). 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. thisversusthis. That). #294 added MD064'sallow-sentence-double-spacefor this convention, socheckaccepts the spacing, butfmtwithreflow = truestill 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:allow-sentence-double-spaceaccepts: the sameis_after_sentence_endingcheck, with the built-in abbreviations plus MD013'sabbreviations. Sofmtkeeps exactly the double spaces thatcheckallows..,!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 ase.g.joins with one space, and CJK sentence enders take no extra space.Dr., and so on..,!or?.becomes
With the option off, which is the default, output is unchanged.
Implementation
reflow_elementstokenizes text with the gap in front of each word (split_breakable_words_with_gaps). Each place that wrote a single space now writesgap_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.join_soft_break_lines_with_sentencesnow takes what a sentence-ending break becomes (SentenceJoin::Breakfor sentence-per-line,SentenceJoin::DoubleSpacefor this option). The newjoin_double_spaced_sentence_linesis used byjoin_reflow_linesand by MD013'sjoin_paragraph_lines.is_after_sentence_ending_with_abbreviationsinsentence_utilsis nowpub(crate), so reflow shares MD064's sentence-end check.Tests
text_reflow.rs, plus a config parse test.tests/regressions/md013_reflow_sentence_double_space_test.rsrunsfmtend 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 thatcheckwith MD064'sallow-sentence-double-spaceis clean on the output and that a secondfmtpass changes nothing.make test-devpasses (17,007 tests), as do the clippy step ofmake lintandmake docs-check, on the pinned 1.96.0 toolchain.