Preserve QPowerPoint text hyperlinks in PPTX

This commit is contained in:
Курнат Андрей
2026-06-02 06:08:47 +03:00
parent 2f1c70852d
commit 0a675236b3
6 changed files with 152 additions and 9 deletions
+64
View File
@@ -527,6 +527,66 @@ describe("powerPointOffice helpers", () => {
expect(exportedSlideXml).toContain('typeface="Times New Roman"');
});
it("импортирует внешние ссылки текста PPTX через hlinkClick relationships", async () => {
const zip = new JSZip();
zip.file(
"ppt/presentation.xml",
`<p:presentation xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><p:sldIdLst><p:sldId id="256" r:id="rId1"/></p:sldIdLst></p:presentation>`
);
zip.file(
"ppt/_rels/presentation.xml.rels",
`<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide" Target="slides/slide1.xml"/></Relationships>`
);
zip.file("ppt/slides/slide1.xml", slideXmlWithTextHyperlinks("Roadmap", "Open report"));
zip.file(
"ppt/slides/_rels/slide1.xml.rels",
`<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rIdTitleLink" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="https://example.com/title" TargetMode="External"/><Relationship Id="rIdSubtitleLink" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="mailto:team@example.com" TargetMode="External"/></Relationships>`
);
const blob = await zip.generateAsync({
type: "blob",
mimeType: "application/vnd.openxmlformats-officedocument.presentationml.presentation"
});
const imported = await importPptxFile(new File([blob], "Links.pptx", { type: blob.type }));
expect(imported.slides[0]).toMatchObject({
title: "Roadmap",
titleHyperlink: "https://example.com/title",
subtitle: "Open report",
subtitleHyperlink: "mailto:team@example.com"
});
});
it("экспортирует внешние ссылки заголовка и подзаголовка PPTX", async () => {
const blob = await exportPptxBlob({
title: "Links.pptx",
slides: [
{
id: "slide-1",
title: "Roadmap",
titleHyperlink: "https://example.com/title",
subtitle: "Open report",
subtitleHyperlink: "mailto:team@example.com",
notes: "",
theme: "classic"
}
]
});
const zip = await JSZip.loadAsync(await blob.arrayBuffer());
const slideXml = (await zip.file("ppt/slides/slide1.xml")?.async("string")) ?? "";
const relationshipsXml = (await zip.file("ppt/slides/_rels/slide1.xml.rels")?.async("string")) ?? "";
const reimported = await importPptxFile(new File([blob], "Links.pptx", { type: blob.type }));
expect(slideXml).toContain("hlinkClick");
expect(relationshipsXml).toContain('Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"');
expect(relationshipsXml).toContain('Target="https://example.com/title"');
expect(relationshipsXml).toContain('Target="mailto:team@example.com"');
expect(reimported.slides[0]).toMatchObject({
titleHyperlink: "https://example.com/title",
subtitleHyperlink: "mailto:team@example.com"
});
});
it("нормализует пробелы в импортированном тексте", () => {
expect(normalizeWhitespace(" Строка\tс \n пробелами ")).toBe("Строка с пробелами");
});
@@ -554,6 +614,10 @@ function slideXmlWithTextStyles(title: string, subtitle: string) {
return `<p:sld xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"><p:cSld><p:spTree><p:sp><p:txBody><a:p><a:r><a:rPr sz="3400"><a:solidFill><a:srgbClr val="C2410C"/></a:solidFill><a:latin typeface="Courier New"/></a:rPr><a:t>${title}</a:t></a:r></a:p></p:txBody></p:sp><p:sp><p:txBody><a:p><a:r><a:rPr sz="2200"><a:solidFill><a:srgbClr val="0F766E"/></a:solidFill><a:latin typeface="Times New Roman"/></a:rPr><a:t>${subtitle}</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:cSld></p:sld>`;
}
function slideXmlWithTextHyperlinks(title: string, subtitle: string) {
return `<p:sld xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><p:cSld><p:spTree><p:sp><p:txBody><a:p><a:r><a:rPr><a:hlinkClick r:id="rIdTitleLink"/></a:rPr><a:t>${title}</a:t></a:r></a:p></p:txBody></p:sp><p:sp><p:txBody><a:p><a:r><a:rPr><a:hlinkClick r:id="rIdSubtitleLink"/></a:rPr><a:t>${subtitle}</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:cSld></p:sld>`;
}
function slideXmlWithMultilineSubtitle(title: string, subtitleParagraphs: string[]) {
return `<p:sld xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"><p:cSld><p:spTree><p:sp><p:txBody><a:p><a:r><a:t>${title}</a:t></a:r></a:p></p:txBody></p:sp><p:sp><p:txBody>${subtitleParagraphs
.map((text) => `<a:p><a:r><a:t>${text}</a:t></a:r></a:p>`)