diff --git a/apps/web/src/editor/deserialize.ts b/apps/web/src/editor/deserialize.ts index 3bca5e585a..4f0026de20 100644 --- a/apps/web/src/editor/deserialize.ts +++ b/apps/web/src/editor/deserialize.ts @@ -64,7 +64,10 @@ function parseAtRoomMentions(text: string, pc: PartCreator, opts: IParseOptions) } function parseLink(n: Node, pc: PartCreator, opts: IParseOptions): Part[] { - const { href } = n as HTMLAnchorElement; + // Read the attribute rather than the `href` property: the property is the resolved, serialised + // URL, so it appends a trailing slash to an origin-only link and absolutises a relative one. + // The composer should show the author what they wrote. + const href = (n as HTMLAnchorElement).getAttribute("href") ?? ""; const resourceId = getPrimaryPermalinkEntity(href); // The room/user ID switch (resourceId?.[0]) { diff --git a/apps/web/test/unit-tests/editor/deserialize-test.ts b/apps/web/test/unit-tests/editor/deserialize-test.ts index 2ed3e40e0d..9ad884a0c5 100644 --- a/apps/web/test/unit-tests/editor/deserialize-test.ts +++ b/apps/web/test/unit-tests/editor/deserialize-test.ts @@ -109,6 +109,24 @@ describe("editor/deserialize", function () { expect(parts.length).toBe(1); expect(parts[0]).toStrictEqual({ type: "plain", text: "click [this](http://example.com/)!" }); }); + it("hyperlink without a trailing slash keeps the original URL", function () { + const html = 'click this!'; + const parts = normalize(parseEvent(htmlMessage(html), createPartCreator())); + expect(parts.length).toBe(1); + expect(parts[0]).toStrictEqual({ type: "plain", text: "click [this](http://example.com)!" }); + }); + it("hyperlink with a relative URL keeps the original URL", function () { + const html = 'click this!'; + const parts = normalize(parseEvent(htmlMessage(html), createPartCreator())); + expect(parts.length).toBe(1); + expect(parts[0]).toStrictEqual({ type: "plain", text: "click [this](/local/path)!" }); + }); + it("autolinked URL is not rewritten as a markdown link", function () { + const html = 'http://example.com'; + const parts = normalize(parseEvent(htmlMessage(html), createPartCreator())); + expect(parts.length).toBe(1); + expect(parts[0]).toStrictEqual({ type: "plain", text: "http://example.com" }); + }); it("multiple lines with paragraphs", function () { const html = "
hello
world
"; const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));