Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

fix: preserve whitespace outside Markdown marks (#6404)

lemoana1724f0bd26bd
diff --git a/web/src/components/MemoEditor/Editor/formatting.ts b/web/src/components/MemoEditor/Editor/formatting.ts
index 49ffdd9fc..814b091b7 100644
--- a/web/src/components/MemoEditor/Editor/formatting.ts
+++ b/web/src/components/MemoEditor/Editor/formatting.ts
@@ -106,9 +106,30 @@ function lineListInfo(text: string): LineListInfo {
function wrapSelection(view: EditorView, token: string) {
const { from, to } = view.state.selection.main;
const sel = view.state.sliceDoc(from, to);
+ // CommonMark cannot close an emphasis delimiter immediately after
+ // whitespace. Keep selection-edge whitespace outside the mark instead of
+ // producing source such as `**important **`, which renders literally.
+ const edgeLeadingWhitespace = sel.match(/^\s*/)?.[0] ?? "";
+ const edgeTrailingWhitespace = sel.match(/\s*$/)?.[0] ?? "";
+ const contentEnd = sel.length - edgeTrailingWhitespace.length;
+ const content = sel.slice(edgeLeadingWhitespace.length, contentEnd);
+
+ // There is no text to mark. Preserve the existing empty-cursor behavior
+ // (an editable delimiter pair), but do not replace a whitespace-only
+ // selection with invalid emphasis or discard its whitespace.
+ if (!content) {
+ if (sel) return;
+ view.dispatch({
+ changes: { from, to, insert: `${token}${token}` },
+ selection: { anchor: from + token.length },
+ });
+ return;
+ }
+
+ const contentStart = from + edgeLeadingWhitespace.length;
view.dispatch({
- changes: { from, to, insert: `${token}${sel}${token}` },
- selection: { anchor: from + token.length, head: from + token.length + sel.length },
+ changes: { from, to, insert: `${edgeLeadingWhitespace}${token}${content}${token}${edgeTrailingWhitespace}` },
+ selection: { anchor: contentStart + token.length, head: contentStart + token.length + content.length },
});
}
diff --git a/web/tests/editor-keys.test.ts b/web/tests/editor-keys.test.ts
index 4bfd40da1..2a1249a04 100644
--- a/web/tests/editor-keys.test.ts
+++ b/web/tests/editor-keys.test.ts
@@ -93,6 +93,19 @@ describe("editor key bindings", () => {
view.destroy();
});
+ it("keeps selected trailing whitespace outside a bold mark", () => {
+ const view = makeView("23, lemo ");
+ view.dispatch({ selection: { anchor: 0, head: 9 } });
+
+ press(view, "b", mod());
+
+ // `**23, lemo **` is not valid GFM emphasis: its closing delimiter follows
+ // whitespace and is rendered literally. The trailing space belongs after
+ // the closing delimiter instead.
+ expect(view.state.doc.toString()).toBe("**23, lemo** ");
+ view.destroy();
+ });
+
it("converts a heading to a paragraph with Mod-Alt-0", () => {
const view = makeView("## text");
view.dispatch({ selection: { anchor: 4 } });