Conversation
Turndown emits each block as one physical line regardless of width, so typing or pasting a long paragraph in the md viewer wrote one very long line to CM on every save. Adds a line-scoped reflow that wraps only the lines the user actually edited, behind a new opt-out preference mdViewerWrapEditedLines (default true) using EditorOptionHandlers .getMaxLineLength() as the width source. New module markdown-line-wrap.js (with 26 unit specs): - Prefix/suffix line-range diff so unchanged lines stay byte-identical. - Indent-aware continuation: bullets (- * +), ordered lists (N. / N)), and blockquotes drive the continuation indent on wrapped lines. - Inline atoms (image-only links like [ ](href), plain image links, inline links, inline code, inline HTML tags) are kept whole by tokenization. Single oversize tokens are left intact. - Conservative skip rules: fenced code, tables, ATX headings, setext underlines, link reference definitions, hr, HTML blocks, frontmatter. - Balanced packer: binary-searches for the smallest width that still produces the greedy line count, so the two physical lines come out e.g. 95+90 instead of 118+67 for a 186-char paragraph. Critical for the cursor-sync precision in the rendered viewer (see below). - Fast-path early exit when no changed line exceeds printWidth, so typical typing keystrokes never run the regex-heavy state scan. Per-source-line cursor sync in the markdown viewer (bridge.js + editor.js): - The custom paragraph renderer in bridge.js now wraps each source line of a multi-line paragraph in <span data-source-line="N">. Without these the whole <p> shared a single data-source-line and cursor sync always resolved to the block's first line, so moving the caret through a wrapped paragraph never updated the CM highlight. - editor.js _updateSourceLineAttrs keeps those spans in sync as the user edits. _refreshParagraphSourceSpans no-ops when the layout is unchanged, updates attributes in place when only the start line shifted, and rebuilds inner HTML (preserving caret by character offset) when the wrap line count actually changed. - Fixes a pre-existing _updateSourceLineAttrs filter bug: it was skipping any element with the cursor-sync-highlight class, but that class is added to real content blocks (<p>, <h1>) for highlighting purposes. Skipping them threw mdLineIdx off-by-one for every block after the highlighted one. Now we only skip the standalone overlay variants (cursor-sync-br-line, cursor-sync-code-line). Tests: - 26 unit specs in unit:markdown-line-wrap cover bullets, nested lists, ordered lists, blockquotes, fences, tables, headings, link refs, inline atoms, frontmatter, and the badge-row regression. - The existing 58-spec livepreview:Markdown Editor 1 suite still passes.
Hot path in _balancedPack: >>1 is faster than Math.floor for the midpoint of two non-negative ints, and the values here stay well under 2^31. Adds a local no-bitwise eslint disable with rationale.
|
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.



Turndown emits each block as one physical line regardless of width, so
typing or pasting a long paragraph in the md viewer wrote one very long
line to CM on every save. Adds a line-scoped reflow that wraps only the
lines the user actually edited, behind a new opt-out preference
mdViewerWrapEditedLines (default true) using EditorOptionHandlers
.getMaxLineLength() as the width source.
New module markdown-line-wrap.js (with 26 unit specs):
and blockquotes drive the continuation indent on wrapped lines.
image links, inline links, inline code, inline HTML tags) are kept
whole by tokenization. Single oversize tokens are left intact.
underlines, link reference definitions, hr, HTML blocks, frontmatter.
produces the greedy line count, so the two physical lines come out
e.g. 95+90 instead of 118+67 for a 186-char paragraph. Critical for
the cursor-sync precision in the rendered viewer (see below).
typical typing keystrokes never run the regex-heavy state scan.
Per-source-line cursor sync in the markdown viewer (bridge.js + editor.js):
of a multi-line paragraph in . Without
these the whole
shared a single data-source-line and cursor sync
always resolved to the block's first line, so moving the caret
through a wrapped paragraph never updated the CM highlight.
user edits. _refreshParagraphSourceSpans no-ops when the layout is
unchanged, updates attributes in place when only the start line
shifted, and rebuilds inner HTML (preserving caret by character
offset) when the wrap line count actually changed.
skipping any element with the cursor-sync-highlight class, but that
class is added to real content blocks (
,
) for highlighting
purposes. Skipping them threw mdLineIdx off-by-one for every block
after the highlighted one. Now we only skip the standalone overlay
variants (cursor-sync-br-line, cursor-sync-code-line).
Tests:
ordered lists, blockquotes, fences, tables, headings, link refs,
inline atoms, frontmatter, and the badge-row regression.