mirror of
https://github.com/Novaloop-AG/healthchat-web.git
synced 2026-11-25 11:13:41 +00:00
Keep the original URL when deserialising markdown links (#34488)
HTMLAnchorElement.href is the resolved, serialised URL, so editing a message containing [a](http://example.com) put [a](http://example.com/) in the composer and re-wrapped a plain autolinked URL as a markdown link. Read the href attribute instead so the composer shows what the author wrote. Co-authored-by: David Baker <dbkr@users.noreply.github.com>
This commit is contained in:
@@ -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]) {
|
||||
|
||||
@@ -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 <a href="http://example.com">this</a>!';
|
||||
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 <a href="/local/path">this</a>!';
|
||||
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 = '<a href="http://example.com">http://example.com</a>';
|
||||
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 = "<p>hello</p><p>world</p>";
|
||||
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
||||
|
||||
Reference in New Issue
Block a user