Skip to content

Commit 3586912

Browse files
klopez4212codex
andcommitted
Normalize external pasted link labels before Markdown serialization
Co-authored-by: Codex <[email protected]> Signed-off-by: klopez4212 <[email protected]>
1 parent f6b5cf6 commit 3586912

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎src/features/messages/EditableInput.test.tsx‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3166,7 +3166,6 @@ it.each(["ul", "ol", "blockquote", "pre"])(
31663166
);
31673167

31683168
it.each([
3169-
["first\nsecond", '<a href="https://example.com">first<br>second</a>'],
31703169
[
31713170
"Ship 🚀 guide",
31723171
'Ship <img data-stringify-type="emoji" alt="🚀"> <a href="https://example.com">guide</a>',
@@ -3212,3 +3211,27 @@ it.each([
32123211
href,
32133212
]);
32143213
});
3214+
3215+
it.each([
3216+
[
3217+
"Title\nDescription",
3218+
"\n <div>Title</div>\n\n <div>Description</div>\n",
3219+
"Title Description",
3220+
],
3221+
["safe\n\n# SECRET", "safe\n\n# SECRET", "safe # SECRET"],
3222+
["first\nsecond", "first<br>second", "first second"],
3223+
["first\nsecond", "<div>first</div><div>second</div>", "first second"],
3224+
])(
3225+
"normalizes external link labels into one Markdown paragraph: %s",
3226+
(plain, inner, label) => {
3227+
for (const text of [plain, ""]) {
3228+
const h = mount();
3229+
paste(h.input, text, `<a href="https://example.com/">${inner}</a>`);
3230+
expect(h.markdown().trim()).toBe(`[${label}](https://example.com/)`);
3231+
expect(scanMarkdown(h.markdown()).links.map((link) => link.url)).toEqual([
3232+
"https://example.com/",
3233+
]);
3234+
expect(h.input).toHaveTextContent(label);
3235+
}
3236+
},
3237+
);

‎src/features/messages/composer-clipboard.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,15 @@ export function externalLinkMarkdown(
203203
}
204204
}
205205
if (!linked) return;
206+
// External labels must fit one Markdown paragraph. Read child boundaries
207+
// before collapsing whitespace so adjacent blocks and <br> do not join words.
208+
for (const anchor of body.querySelectorAll("a[href]")) {
209+
const label = body.ownerDocument.createElement("div");
210+
label.append(...anchor.childNodes);
211+
anchor.textContent = serializeNode(label, "text")
212+
.replace(/\s+/g, " ")
213+
.trim();
214+
}
206215
if (plain !== undefined) {
207216
// Compare the serializer's actual labels, not the source HTML's textContent:
208217
// serialization can lose image emoji or boundaries inside an anchor.

0 commit comments

Comments
 (0)