Preserve QPowerPoint text hyperlinks in PPTX
This commit is contained in:
@@ -14,6 +14,7 @@ export interface ExportablePowerPointDeck {
|
||||
interface SlideTextBlock {
|
||||
text: string;
|
||||
listStyle?: SlideListStyle;
|
||||
hyperlink?: string;
|
||||
color?: string;
|
||||
fontSize?: number;
|
||||
fontFamily?: string;
|
||||
@@ -38,6 +39,7 @@ const slidePathPattern = /^ppt\/slides\/slide(\d+)\.xml$/;
|
||||
const slideRelationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide";
|
||||
const notesRelationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesSlide";
|
||||
const imageRelationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image";
|
||||
const hyperlinkRelationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink";
|
||||
const emuPerInch = 914400;
|
||||
const slideTransitions = ["fade", "push", "wipe"] as const;
|
||||
const themeColors: Record<Slide["theme"], { background: string; title: string; subtitle: string }> = {
|
||||
@@ -68,7 +70,10 @@ export async function importPptxFile(file: File): Promise<ImportedPowerPointDeck
|
||||
const slideXml = await readRequiredZipText(zip, path, "Не удалось прочитать слайд PowerPoint.");
|
||||
const parsedSlide = parser.parse(slideXml);
|
||||
const orderedSlide = orderedParser.parse(slideXml);
|
||||
const slideTextBlocks = mergeOrderedSlideTextBlocks(extractSlideTextBlocks(parsedSlide), extractOrderedTextBodyTexts(orderedSlide));
|
||||
const relationshipsPath = `${partDirectory(path)}/_rels/${partFileName(path)}.rels`;
|
||||
const slideRelationshipsXml = await readOptionalParsedXml(zip, parser, relationshipsPath);
|
||||
const hyperlinkTargets = relationshipTargetsById(slideRelationshipsXml, hyperlinkRelationshipType, path);
|
||||
const slideTextBlocks = mergeOrderedSlideTextBlocks(extractSlideTextBlocks(parsedSlide, hyperlinkTargets), extractOrderedTextBodyTexts(orderedSlide));
|
||||
const images = await extractSlideImages(zip, parser, parsedSlide, path, index);
|
||||
const notesPath = await slideNotesPath(zip, parser, path);
|
||||
const notesXml = notesPath ? await readOptionalZipText(zip, notesPath) : await readOptionalZipText(zip, `ppt/notesSlides/notesSlide${index}.xml`);
|
||||
@@ -86,7 +91,9 @@ export async function importPptxFile(file: File): Promise<ImportedPowerPointDeck
|
||||
return {
|
||||
id: `pptx-slide-${index}`,
|
||||
title: slideTextBlocks[0]?.text || `Слайд ${index}`,
|
||||
...(slideTextBlocks[0]?.hyperlink ? { titleHyperlink: slideTextBlocks[0].hyperlink } : {}),
|
||||
subtitle: slideTextBlocks.slice(1).map((block) => block.text).join("\n"),
|
||||
...(slideTextBlocks[1]?.hyperlink ? { subtitleHyperlink: slideTextBlocks[1].hyperlink } : {}),
|
||||
notes,
|
||||
theme: "classic" as const,
|
||||
...(hidden ? { hidden } : {}),
|
||||
@@ -140,6 +147,8 @@ export async function exportPptxBlob(deck: ExportablePowerPointDeck): Promise<Bl
|
||||
const subtitleFontSize = normalizeFontSize(sourceSlide.subtitleFontSize) || 18;
|
||||
const titleFontFamily = normalizeFontFamily(sourceSlide.titleFontFamily) || "Arial";
|
||||
const subtitleFontFamily = normalizeFontFamily(sourceSlide.subtitleFontFamily) || "Arial";
|
||||
const titleHyperlink = normalizedExternalHyperlinkTarget(sourceSlide.titleHyperlink);
|
||||
const subtitleHyperlink = normalizedExternalHyperlinkTarget(sourceSlide.subtitleHyperlink);
|
||||
const slide = pptx.addSlide();
|
||||
slide.background = { color: normalizeHexColor(sourceSlide.backgroundColor) || colors.background };
|
||||
sourceSlide.images?.forEach((image, index) => {
|
||||
@@ -167,7 +176,8 @@ export async function exportPptxBlob(deck: ExportablePowerPointDeck): Promise<Bl
|
||||
bold: true,
|
||||
color: titleColor,
|
||||
margin: 0.04,
|
||||
breakLine: false
|
||||
breakLine: false,
|
||||
...(titleHyperlink ? { hyperlink: { url: titleHyperlink } } : {})
|
||||
});
|
||||
slide.addText(sourceSlide.subtitle || "", {
|
||||
x: 0.75,
|
||||
@@ -179,7 +189,8 @@ export async function exportPptxBlob(deck: ExportablePowerPointDeck): Promise<Bl
|
||||
color: subtitleColor,
|
||||
valign: "top",
|
||||
fit: "shrink",
|
||||
breakLine: false
|
||||
breakLine: false,
|
||||
...(subtitleHyperlink ? { hyperlink: { url: subtitleHyperlink } } : {})
|
||||
});
|
||||
|
||||
if (sourceSlide.notes.trim() && slide.addNotes) {
|
||||
@@ -200,16 +211,17 @@ export function extractTextBlocks(xmlNode: unknown): string[] {
|
||||
return blocks.map(normalizeWhitespace).filter(Boolean);
|
||||
}
|
||||
|
||||
function extractSlideTextBlocks(slideXml: unknown): SlideTextBlock[] {
|
||||
const textBodies = recordsByKey(slideXml, "txBody");
|
||||
function extractSlideTextBlocks(slideXml: unknown, hyperlinkTargets: Record<string, string> = {}): SlideTextBlock[] {
|
||||
const textBodies = slideTextBodyRecords(slideXml);
|
||||
const blocks = textBodies
|
||||
.map((textBody) => {
|
||||
.map(({ textBody, hyperlinkSource }) => {
|
||||
const text = extractTextBodyText(textBody);
|
||||
if (!text) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const runProperties = firstRecordByKey(textBody, "rPr");
|
||||
const hyperlink = textBodyHyperlink(hyperlinkSource, hyperlinkTargets);
|
||||
const listStyle = textBodyListStyle(textBody);
|
||||
const color = runTextColor(runProperties);
|
||||
const fontSize = runTextFontSize(runProperties);
|
||||
@@ -217,6 +229,7 @@ function extractSlideTextBlocks(slideXml: unknown): SlideTextBlock[] {
|
||||
return {
|
||||
text,
|
||||
...(listStyle ? { listStyle } : {}),
|
||||
...(hyperlink ? { hyperlink } : {}),
|
||||
...(color ? { color } : {}),
|
||||
...(fontSize ? { fontSize } : {}),
|
||||
...(fontFamily ? { fontFamily } : {})
|
||||
@@ -227,6 +240,28 @@ function extractSlideTextBlocks(slideXml: unknown): SlideTextBlock[] {
|
||||
return blocks.length > 0 ? blocks : extractTextBlocks(slideXml).map((text) => ({ text }));
|
||||
}
|
||||
|
||||
function slideTextBodyRecords(slideXml: unknown): Array<{ textBody: Record<string, unknown>; hyperlinkSource: Record<string, unknown> }> {
|
||||
const shapeEntries = recordsByKey(slideXml, "sp")
|
||||
.map((shape) => {
|
||||
const textBody = childRecord(shape, "txBody");
|
||||
return Object.keys(textBody).length > 0 ? { textBody, hyperlinkSource: shape } : null;
|
||||
})
|
||||
.filter((entry): entry is { textBody: Record<string, unknown>; hyperlinkSource: Record<string, unknown> } => Boolean(entry));
|
||||
|
||||
return shapeEntries.length > 0 ? shapeEntries : recordsByKey(slideXml, "txBody").map((textBody) => ({ textBody, hyperlinkSource: textBody }));
|
||||
}
|
||||
|
||||
function textBodyHyperlink(textBody: Record<string, unknown>, hyperlinkTargets: Record<string, string>) {
|
||||
const hyperlink = recordsByKey(textBody, "hlinkClick")
|
||||
.map((record) => {
|
||||
const id = relationshipId(record);
|
||||
return id ? normalizedExternalHyperlinkTarget(hyperlinkTargets[id]) : "";
|
||||
})
|
||||
.find(Boolean);
|
||||
|
||||
return hyperlink ?? "";
|
||||
}
|
||||
|
||||
function mergeOrderedSlideTextBlocks(styledBlocks: SlideTextBlock[], orderedTexts: string[]) {
|
||||
if (orderedTexts.length === 0) {
|
||||
return styledBlocks;
|
||||
@@ -351,6 +386,9 @@ export function relationshipTargetByType(relationshipsXml: unknown, type: string
|
||||
export function resolvePptTarget(baseDirectory: string, target: string): string {
|
||||
const normalizedBase = baseDirectory.replace(/\\/g, "/").replace(/\/+$/, "");
|
||||
const normalizedTarget = target.replace(/\\/g, "/");
|
||||
if (/^[a-z][a-z0-9+.-]*:/i.test(normalizedTarget)) {
|
||||
return normalizedTarget;
|
||||
}
|
||||
const rawPath = normalizedTarget.startsWith("/") ? normalizedTarget.replace(/^\/+/, "") : `${normalizedBase}/${normalizedTarget}`;
|
||||
const segments: string[] = [];
|
||||
|
||||
@@ -591,6 +629,20 @@ function normalizeHexColor(value?: string) {
|
||||
return /^[0-9A-F]{6}$/.test(raw) ? raw : "";
|
||||
}
|
||||
|
||||
function normalizedExternalHyperlinkTarget(value?: string) {
|
||||
const raw = value?.trim();
|
||||
if (!raw) {
|
||||
return "";
|
||||
}
|
||||
|
||||
try {
|
||||
const url = new URL(raw);
|
||||
return ["http:", "https:", "mailto:", "tel:", "ftp:"].includes(url.protocol.toLowerCase()) ? url.href : "";
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
function runTextColor(runProperties: Record<string, unknown>) {
|
||||
const solidFill = childRecord(runProperties, "solidFill");
|
||||
const srgbColor = childRecord(solidFill, "srgbClr");
|
||||
|
||||
Reference in New Issue
Block a user