Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Leafdown uses lightweight [Keep a Changelog](https://keepachangelog.com/en/1.1.0

### Fixed

- Read a typed `*` or `_` run the way Markdown reads the same characters in a file, so `***text*` gives two literal asterisks before italic text, `_**text**` a literal underscore before bold text, and `_**text**_` italic bold, instead of leaving every marker as text that saved with backslashes and reopened without the formatting. A run whose closing marker is shorter than its opening one is read once the caret leaves it, because another marker typed there would spell something else.
- Pair a `*`, `_`, or `~` typed against bold, italic, or strikethrough with the matching literal marker already on the other side of it, so closing `_**text**` with a `_` gives italic bold and saves `_**text**_`, instead of leaving both markers as text that saved as `\_**text**\_` and reopened without the italic. A marker a file keeps literal by escaping it stays literal.
- Leave a `*`, `_`, or `~` bare on save wherever nothing else on its line could pair with it, so text such as `[a](b)*` keeps its marker as written instead of collecting a backslash merely because a link, an image, or a bold span shares the line with it. A marker that could still pair, including one that could pair with the markers of a span beside it, keeps its backslash.
- Keep the address of a URL or email address written on its own when a `*`, `_`, or `~` follows it, so text such as `https://example.com*` keeps its link pointing where it did. The backslash the file writes to keep that marker literal was being read back as part of the address, which gained another backslash every time the document was opened and saved.
Expand Down
2 changes: 2 additions & 0 deletions docs/decisions.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@
- The preset's strikethrough delimiter run is not preserved. Its strikethrough mark carries no marker attribute, unlike emphasis and strong, so a single-tilde run parses and serializes back as a double-tilde run. This is left as a known normalization under [Preserve Markdown semantics over exact formatting](#preserve-markdown-semantics-over-exact-formatting) rather than overridden as the autolink form was, because both runs mean the same thing to a GFM reader. Preserving the authored run would require carrying the marker on the mark.
- The preset's strikethrough input rule is overridden. Its `(~{1,2})` backtracks to a one-tilde delimiter run when no two-tilde closing run exists yet, and its content group does not exclude the marker, so typing `~~text~~` created a mark over `~text` on the seventh keystroke and left a surplus tilde on each side that saved as an escaped character. Leafdown carries its own rule, decided in [issue #233](https://github.com/Azganoth/leafdown/issues/233), which excludes the marker from the content and anchors the match at the caret so a run stays literal text until the author closes it. This is the only input rule Leafdown owns; every other preset rule either anchors at the caret or excludes its own marker, and none of them can match a run this way.
- The replacement rule keeps the preset's leading word, colon, and slash guard, so a tilde run that touches one of those does not become a strikethrough as it is typed. `lead~~text~~` and `1~2~3` parse as strikethrough when a file holds them but stay literal text when typed, which is a real disagreement, examined in [issue #282](https://github.com/Azganoth/leafdown/issues/282) and left as it is. The guard does two jobs: it holds the word boundary, and it stops a one-tilde run from opening inside an unclosed two-tilde one. Removing it fixes the first case and breaks `~~a~b~~`, which types as a struck `b` between literal tildes, because an input rule reads only the text before the caret and cannot know another tilde is coming. Separating the two jobs means matching delimiter runs directly rather than through `markRule`, which reads one content group and cannot express the alternation. Leafdown prefers the conservative failure: literal text the author can see and correct, over a construct silently built around the wrong delimiters.
- A `*` or `_` run the preset input rules decline is paired by Leafdown instead, decided in [issue #232](https://github.com/Azganoth/leafdown/issues/232). Those rules read one content group between delimiters of equal length, so a run closed against an unequal or differently spelled one matched nothing and stayed literal: `***text*`, `_**text*`, `_**text**`, and `*__text_` kept every marker as text, and `_**text**_` closed an italic span over the literal asterisks its bold span should have spelled. The pairing that already answers a delimiter typed beside a mark now answers one typed beside literal text as well, over as many delimiters as the shorter run spells. It acts only where the preset declined, because a rule that matches consumes the typed character before the plugin sees it, which is why the preset's guards are reproduced rather than replaced and `lead**text**` still types literal. The underscore is the one exception: `\w` counts it as a word character where CommonMark counts it as punctuation, and `_**text**` cannot be read otherwise.
- The pairing waits where the closing run is shorter than the run it would pair with. An input rule reads only the text before the caret and cannot know another delimiter is coming, and here the continuations are each other: firing on the keystroke serves `***text*` at the cost of `***text**`, and refusing serves the second at the cost of the first. Reading the pair once the caret leaves the run it closed serves both, and is how typed link source already reaches the object it spells. A closing run that could equally open a construct is refused outright rather than deferred, on the conservatism above: `**a*b` leaves its delimiters as text the author can see and correct, rather than pairing a run they may still be typing into. A tilde is left out of this pass entirely, because a strikethrough is spelled only by runs of equal length.
- Develop custom UI components only when required by the product specification.

### Do not use Crepe
Expand Down
1 change: 1 addition & 0 deletions docs/specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ The editor is a unified hybrid Markdown surface. Behavior is governed by renderi
- Link and autolink projection exposes their source directly in the document; links preserve their label, target, optional title, and compatible uniform outer inline formatting. An autolink keeps the form it was authored in, bare or angle-bracket, when it is projected and when it is saved. A link remains one semantic projection owner. A caret or contained text selection anywhere in a supported label projects the complete link source, including labels with nested strong, emphasis, strikethrough, inline-code formatting, semantic soft line endings, an image, a footnote reference, or a preserved character reference. An image or footnote reference in a projected label becomes its own Markdown source and returns as its object when the label commits. Soft line endings remain one logical label; indentation follows Leafdown's canonical serialization. Valid edits rehydrate one link over the complete rich label; invalid or incomplete edits become the literal text the source spells, where a backslash escapes the character it precedes and is otherwise kept as text. Mixed-format and multiline labels do not fall back to fragmented projections for their nested content.
- Text written in the current editing session that is exactly the source of one inline link, image, autolink literal, or URI autolink becomes that object when the caret leaves it, including when a line break ends the line it sits on, so typing and plain-text paste of the same characters reach the same document. The caret has not left while only source characters separate them, which keeps a bare URL whole as it is typed. Backslash-escaped source, incomplete source, and text that is not exactly one object's source stay literal. Source the file escaped is literal text the author asked for, so editing it leaves it literal and only the escape gesture below converts it, and `Undo` returns a committed link to the source it was written as. Because a committed link projects its source again whenever the caret returns, the visible text does not change.
- A `*`, `_`, or `~` typed against a marked run pairs with a matching literal run of the same length on the other side of that run, so the two become the emphasis, strong emphasis, or strikethrough the line spells and neither delimiter is left as content. The pair forms only where the outer neighbours let both runs flank their content, which leaves a run sitting after a letter or a digit literal, and only where the span does not already carry the mark the pair would apply. Pairing answers the character typed in this session rather than the shape the document holds, so a file that keeps a delimiter on each side of a run literal by escaping it opens that way and stays that way.
- A `*` or `_` typed to close a run of literal text pairs with the run that same text opens, over as many delimiters as the shorter of the two spells and leaving the surplus literal, so `***text*` reads as two literal asterisks before italic text and `_**text**` as a literal underscore before bold text. Where the closing run is the shorter one the author may still be extending it, so the pair is read once the caret leaves that run rather than as the character lands. A run stays literal where a letter, a digit, a colon, or a slash sits against the run it would open, where the run it closes could equally open a construct the author has not finished, and wherever CommonMark's flanking rules refuse it. A tilde is not read this way, because a strikethrough is spelled only by runs of equal length.
- Literal text the file keeps literal by escaping projects that escape when a caret or contained text selection reaches it, so the run reads as the file will write it. The gesture covers a run that spells exactly one inline link or image, which is what an escape there can be spent on; text kept literal in any other form shows no escape, because deleting one would change nothing. Deleting a projected backslash converts the run to the object it describes at once, without waiting for the caret to leave; that object then projects its own source in the escape's place, so the characters stay put and the caret keeps its position while the markers restyle around it. `Undo` reverses the conversion like any other document change, and typing the backslash back turns the object literal again. Leaving the escape alone restores the run unchanged, and an edit that neither keeps the escape nor spells one object becomes the literal text the source spells. Nothing durable records the escape; it is derived from the text and the serializer on every open, so the same gesture is available after a save and reopen. Source this session wrote keeps the commit rule above instead, so a run being typed never shows a backslash the author did not write.
- A character reference the file preserves projects its authored source when a caret or contained text selection reaches it, so `©`, `©`, and `©` read as the file will write them rather than as the character they render. References written next to each other are each preserved and each projected on their own, whether they repeat, differ, or name more than one character, so a file holding `©©` keeps both and breaking one leaves its neighbour intact, saving `&copy©`. Entry direction follows the rule for atomic inline projections: a caret entering from the left starts at the beginning of the source, a caret entering from the right starts at the end, a position between two adjacent references enters the one that follows it, and a selection over the rendered character selects the complete source. Leaving the source unchanged restores the rendered character exactly. An edit that still spells one reference commits that reference, so a form can be rewritten in place; any other edit becomes the literal text the source spells, on the same escape rule as a link, which is how removing a character from the source removes the reference from the saved file. Nothing durable records the projection, and a stored source that no longer spells the text it covers is written as that text and projects nothing.
- A character reference within one exact, contiguous supported mark combination belongs to that marked fragment. Entering through its text, the reference itself, or either end of the fragment projects one outer wrapper such as `**a©b**`, and a valid edit commits one mark with the reference intact. Logical links retain higher semantic ownership, so a caret or contained text selection anywhere in a label holding a reference projects the complete link source, while standalone or otherwise ineligible references use the reference-only projection above.
Expand Down
70 changes: 70 additions & 0 deletions src/features/editor/plugins/attentionPairing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,73 @@ describe("pairing a delimiter typed beside a mark", () => {
expect(settle(mounted)).toBe("text[emphasis] tail");
});
});

describe("typing a delimiter run into literal text", () => {
// The run needs a paragraph the tail does not share: a run closed against a letter reads as the
// opener of a construct the author has not finished, and stays literal.
const typeIntoEmptyParagraph = async (input: string) => {
const mounted = await mountEditor(`x${TAIL}`);

mounted.view.dispatch(mounted.view.state.tr.delete(1, 2));
setTextSelection(mounted.view, 1);
typeText(mounted.view, input);

return mounted;
};

it.each([
{ document: "** text[emphasis]", saved: "\\*\\**text*", typed: "***text*" },
{ document: "_* text[emphasis]", saved: "_\\**text*", typed: "_**text*" },
{ document: "_ text[strong]", saved: "_**text**", typed: "_**text**" },
{ document: "*_ text[emphasis]", saved: "*\\__text_", typed: "*__text_" },
{ document: "text[emphasis+strong]", saved: "_**text**_", typed: "_**text**_" },
{ document: "* text[emphasis]", saved: "\\**text*", typed: "**text*" },
{ document: "*** text[emphasis]", saved: "\\*\\*\\**text*", typed: "****text*" },
{ document: "* text[strong]", saved: "\\***text**", typed: "***text**" },
{ document: "* text[strong]", saved: "*__text__", typed: "*__text__" },
{ document: "text[emphasis+strong]", saved: "***text***", typed: "***text***" },
])(
"reads $typed the way CommonMark reads the same source",
async ({ document, saved, typed }) => {
const mounted = await typeIntoEmptyParagraph(typed);

expect(settle(mounted)).toBe(`${document} tail`);
expect(mounted.getMarkdown()).toBe(`${saved}\n\ntail\n`);
// The file spells the delimiters the author typed and no others, whether it writes them as
// syntax or escapes them as text.
expect(saved.replaceAll(/\\(?=[*_~])/gu, "")).toBe(typed);

const reopened = await mountEditor(mounted.getMarkdown());

expect(describeDocument(reopened)).toBe(`${document} tail`);
},
);

it("holds a run the author can still extend until the caret leaves it", async () => {
const mounted = await typeIntoEmptyParagraph("***text*");

expect(describeDocument(mounted)).toBe("***text* tail");

typeText(mounted.view, "*");

expect(settle(mounted)).toBe("* text[strong] tail");
});

it("leaves a run closed against a letter literal", async () => {
const mounted = await typeIntoEmptyParagraph("**a*b");

expect(settle(mounted)).toBe("**a*b tail");
});

it("leaves a run that follows a word literal", async () => {
const mounted = await typeIntoEmptyParagraph("lead**text**");

expect(settle(mounted)).toBe("lead**text** tail");
});

it("leaves an unequal tilde run literal", async () => {
const mounted = await typeIntoEmptyParagraph("~~text~");

expect(settle(mounted)).toBe("~~text~ tail");
});
});
Loading