Import QWord DOCX paragraph alignment
This commit is contained in:
@@ -556,7 +556,7 @@ async function styledDocxHtmlFromArrayBuffer(arrayBuffer: ArrayBuffer) {
|
||||
const parser = new XMLParser(xmlParserOptions);
|
||||
const parsed = parser.parse(documentXml);
|
||||
const result = parsedDocxDocumentToHtml(parsed);
|
||||
return result.hasInlineColorOrHighlight ? result.html : "";
|
||||
return result.hasInlineColorOrHighlight || result.hasParagraphAlignment ? result.html : "";
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
@@ -570,17 +570,20 @@ function parsedDocxDocumentToHtml(documentXml: unknown) {
|
||||
const document = childRecord(documentXml, "w:document");
|
||||
const body = childRecord(document, "w:body");
|
||||
let hasInlineColorOrHighlight = false;
|
||||
let hasParagraphAlignment = false;
|
||||
const paragraphs = toArray(body["w:p"])
|
||||
.map((paragraph) => {
|
||||
const result = docxParagraphToHtml(paragraph);
|
||||
hasInlineColorOrHighlight = hasInlineColorOrHighlight || result.hasInlineColorOrHighlight;
|
||||
hasParagraphAlignment = hasParagraphAlignment || result.hasParagraphAlignment;
|
||||
return result.html;
|
||||
})
|
||||
.filter(Boolean);
|
||||
|
||||
return {
|
||||
html: paragraphs.join(""),
|
||||
hasInlineColorOrHighlight
|
||||
hasInlineColorOrHighlight,
|
||||
hasParagraphAlignment
|
||||
};
|
||||
}
|
||||
|
||||
@@ -589,6 +592,7 @@ function docxParagraphToHtml(paragraph: unknown) {
|
||||
const properties = childRecord(record, "w:pPr");
|
||||
const tagName = docxParagraphTagName(properties);
|
||||
const style = docxParagraphStyleAttribute(properties);
|
||||
const hasParagraphAlignment = Boolean(style);
|
||||
let hasInlineColorOrHighlight = false;
|
||||
const runs = docxParagraphInlineRecords(record).map((run) => {
|
||||
const result = docxRunToHtml(run);
|
||||
@@ -598,12 +602,13 @@ function docxParagraphToHtml(paragraph: unknown) {
|
||||
const content = runs.join("");
|
||||
|
||||
if (!content.trim()) {
|
||||
return { html: "", hasInlineColorOrHighlight };
|
||||
return { html: "", hasInlineColorOrHighlight, hasParagraphAlignment };
|
||||
}
|
||||
|
||||
return {
|
||||
html: `<${tagName}${style ? ` style="${style}"` : ""}>${content}</${tagName}>`,
|
||||
hasInlineColorOrHighlight
|
||||
hasInlineColorOrHighlight,
|
||||
hasParagraphAlignment
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -20,4 +20,24 @@ describe("wordOffice DOCX import", () => {
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
||||
it("imports DOCX paragraph alignment from OOXML", async () => {
|
||||
const blob = await exportDocxBlob(
|
||||
"Alignment.docx",
|
||||
'<p style="text-align: center;">Centered</p><p align="right">Right</p><p style="text-align: justify;">Justified</p>'
|
||||
);
|
||||
const file = new File([blob], "Alignment.docx", {
|
||||
type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
||||
});
|
||||
const imported = await importDocxFile(file);
|
||||
|
||||
expect(imported.html).toContain('style="text-align: center;"');
|
||||
expect(imported.html).toContain('style="text-align: right;"');
|
||||
expect(imported.html).toContain('style="text-align: justify;"');
|
||||
expect(htmlToWordBlocks(imported.html)).toEqual([
|
||||
{ type: "paragraph", text: "Centered", runs: [{ text: "Centered" }], alignment: "center" },
|
||||
{ type: "paragraph", text: "Right", runs: [{ text: "Right" }], alignment: "right" },
|
||||
{ type: "paragraph", text: "Justified", runs: [{ text: "Justified" }], alignment: "both" }
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user