feat(import/markdown): normalize non-breaking spaces

This commit is contained in:
Elian Doran 2025-04-03 19:29:51 +03:00
parent 99461dbf7e
commit 32db26684d
No known key found for this signature in database
3 changed files with 27 additions and 0 deletions

View File

@ -226,4 +226,16 @@ describe("Markdown export", () => {
expect(markdownExportService.toMarkdown(html)).toBe(expected);
});
it("converts   to character", () => {
const html = /*html*/`<p>Hello&nbsp;world.</p>`;
const expected = `Hello\u00a0world.`;
expect(markdownExportService.toMarkdown(html)).toBe(expected);
});
it("preserves non-breaking space character", () => {
const html = /*html*/`<p>Hello\u00adworld.</p>`;
const expected = `Hello\u00adworld.`;
expect(markdownExportService.toMarkdown(html)).toBe(expected);
});
});

View File

@ -133,4 +133,16 @@ second line 2</code></pre><ul><li>Hello</li><li>world</li></ul><ol><li>Hello</li
expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);
});
it("preserves &nbsp;", () => {
const input = `Hello&nbsp;world.`;
const expected = /*html*/`<p>Hello&nbsp;world.</p>`;
expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);
});
it("converts non-breaking space character to &nbsp;", () => {
const input = `Hello\u00a0world.`;
const expected = /*html*/`<p>Hello&nbsp;world.</p>`;
expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);
});
});

View File

@ -87,6 +87,9 @@ function renderToHtml(content: string, title: string) {
// Remove slash for self-closing tags to match CKEditor's approach.
html = html.replace(/<(\w+)([^>]*)\s+\/>/g, "<$1$2>");
// Normalize non-breaking spaces to entity.
html = html.replaceAll("\u00a0", "&nbsp;");
return html;
}