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 @@ -23,6 +23,7 @@ Leafdown uses lightweight [Keep a Changelog](https://keepachangelog.com/en/1.1.0

### Fixed

- Keep typed link and autolink source literal in the saved file when a space follows it, instead of writing it as live Markdown that turns into a link the next time the document is opened.
- Open the Markdown source of a link whose label holds a footnote reference, instead of leaving it closed everywhere in the label except on the reference itself.
- Keep a link label that mixes formatted text with a footnote reference as one link, instead of saving it as two links.
- Open bold, italic, or strikethrough that wraps a link as one Markdown source with the link inside it, instead of one side of the link at a time with markers that do not match the file.
Expand Down
55 changes: 55 additions & 0 deletions src/features/editor/tests/markdownCompatibility.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,61 @@ describe("Markdown compatibility", () => {
});
});

describe("Typed link source", () => {
const typedLinkSourceFixtures = [
{
expected: "\\[test link]\\(./test.html)",
name: "inline link",
typed: "[test link](./test.html)",
},
{ expected: "https\\://example.com", name: "autolink literal", typed: "https://example.com" },
{ expected: "\\<https\\://example.com>", name: "URI autolink", typed: "<https://example.com>" },
];

it.each(typedLinkSourceFixtures)(
"keeps a typed $name literal when it ends the paragraph",
async ({ expected, typed }) => {
const mounted = await mountEditor("");

setSelectionAtDocumentEnd(mounted.view);
typeText(mounted.view, typed);

expect(mounted.getMarkdown()).toBe(`${expected}\n`);
},
);

it.each(typedLinkSourceFixtures)(
"keeps a typed $name literal when a space follows it",
async ({ expected, typed }) => {
const mounted = await mountEditor("");

setSelectionAtDocumentEnd(mounted.view);
typeText(mounted.view, `${typed} `);

expect(mounted.getMarkdown()).toBe(`${expected} \n`);
},
);

it.each(["\\[test link]\\(./test.html)", "\\[test link]\\(./test.html) "])(
"reloads a typed inline link as the text the editor presented in %j",
async (source) => {
const mounted = await mountEditor(source);

expect(mounted.view.dom.querySelector("a")).toBeNull();
expect(mounted.view.dom).toHaveTextContent("[test link](./test.html)");
},
);

it("writes an ordinary trailing space as itself", async () => {
const mounted = await mountEditor("");

setSelectionAtDocumentEnd(mounted.view);
typeText(mounted.view, "plain tail ");

expect(mounted.getMarkdown()).toBe("plain tail \n");
});
});

describe("Authored raw line breaks", () => {
it.each([
"a <br /> b",
Expand Down
6 changes: 6 additions & 0 deletions src/features/editor/utils/createMilkdownEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
defaultValueCtx,
editorViewCtx,
editorViewOptionsCtx,
remarkStringifyOptionsCtx,
rootAttrsCtx,
rootCtx,
} from "@milkdown/kit/core";
Expand Down Expand Up @@ -67,6 +68,7 @@ import {
EMPTY_MARKDOWN_REFERENCE_CONTEXT,
type MarkdownReferenceContext,
} from "./markdownReferences";
import { serializeMarkdownText } from "./markdownText";

export interface MilkdownMarkdownUpdate {
markdown: string;
Expand Down Expand Up @@ -186,6 +188,10 @@ export const createMilkdownEditor = async ({
normalizeProseMirrorClipboardHtml(previousTransformPastedHTML?.(html, view) ?? html),
};
});
ctx.update(remarkStringifyOptionsCtx, (options) => ({
...options,
handlers: { ...options.handlers, text: serializeMarkdownText },
}));
ctx.update(hardbreakSchema.key, (getSchema) => (schemaCtx) => ({
...getSchema(schemaCtx),
linebreakReplacement: true,
Expand Down
23 changes: 23 additions & 0 deletions src/features/editor/utils/markdownText.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { remarkStringifyOptionsCtx } from "@milkdown/kit/core";

type RemarkStringifyHandlers = NonNullable<
ReturnType<typeof remarkStringifyOptionsCtx._typeInfo>["handlers"]
>;

const TRAILING_WHITESPACE_PATTERN = /\s+$/u;

export const serializeMarkdownText: NonNullable<RemarkStringifyHandlers["text"]> = (
node: { value: string },
_parent,
state,
info,
) => {
const { value } = node;
const trailingWhitespace = TRAILING_WHITESPACE_PATTERN.exec(value)?.[0] ?? "";
const escaped = state.safe(value.slice(0, value.length - trailingWhitespace.length), {
...info,
after: trailingWhitespace + info.after,
});

return escaped + trailingWhitespace;
};