Import QWord DOCX paragraph alignment

This commit is contained in:
Курнат Андрей
2026-06-01 05:41:46 +03:00
parent 04811e6580
commit 08fe16bdf6
3 changed files with 30 additions and 5 deletions
+9 -4
View File
@@ -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
};
}