Preserve QWord bookmarks in DOCX

This commit is contained in:
Курнат Андрей
2026-06-01 19:43:35 +03:00
parent fd2e03a44c
commit f22c7af88b
4 changed files with 86 additions and 10 deletions
+19
View File
@@ -29,6 +29,15 @@ describe("wordOffice helpers", () => {
]);
});
it("сохраняет HTML id абзацев и заголовков как закладки DOCX", () => {
const blocks = htmlToWordBlocks('<h1 id="Section1">Раздел</h1><p id="Summary">Итог</p>');
expect(blocks).toEqual([
{ type: "heading", level: 1, text: "Раздел", runs: [{ text: "Раздел" }], bookmark: "Section1" },
{ type: "paragraph", text: "Итог", runs: [{ text: "Итог" }], bookmark: "Summary" }
]);
});
it("сохраняет базовое inline-форматирование текста для DOCX", () => {
const blocks = htmlToWordBlocks(`
<p>Обычный <strong>полужирный</strong> <em>курсив</em> <u>подчеркнутый</u> <span style="font-weight: 700; font-style: italic; text-decoration-line: underline; color: #0f5fae; background-color: rgb(255, 242, 204);">все стили</span></p>
@@ -237,6 +246,16 @@ describe("wordOffice helpers", () => {
expect(relationshipsXml).not.toContain('Target="#Section1"');
});
it("записывает HTML id как DOCX bookmarkStart/bookmarkEnd", async () => {
const blob = await exportDocxBlob("Закладка.docx", '<h1 id="Section1">Раздел</h1><p><a href="#Section1">К разделу</a></p>');
const zip = await JSZip.loadAsync(await blob.arrayBuffer());
const documentXml = await zip.file("word/document.xml")?.async("string");
expect(documentXml).toMatch(/<w:bookmarkStart\b[^>]*w:name="Section1"[^>]*\/>/);
expect(documentXml).toContain("<w:bookmarkEnd");
expect(documentXml).toMatch(/<w:hyperlink\b[^>]*w:anchor="Section1"[^>]*>/);
});
it("записывает data URI изображения в DOCX media relationships", async () => {
const blob = await exportDocxBlob("Изображение.docx", `<p>Логотип</p><img src="${tinyPngDataUri}" alt="Логотип" width="32" height="32">`);
const zip = await JSZip.loadAsync(await blob.arrayBuffer());