Skip to content

Commit

Permalink
feat: add basic ins/del parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
VolodymyrBaydalka committed Sep 10, 2022
1 parent 579aa44 commit bf51a7c
Show file tree
Hide file tree
Showing 14 changed files with 285 additions and 46 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ renderAsync(
trimXmlDeclaration: boolean = true, //if true, xml declaration will be removed from xml documents before parsing
useBase64URL: boolean = false, //if true, images, fonts, etc. will be converted to base 64 URL, otherwise URL.createObjectURL is used
useMathMLPolyfill: boolean = false, //includes MathML polyfills for chrome, edge, etc.
showChanges: false, //enables experimental rendering of document changes (inserions/deletions)
debug: boolean = false, //enables additional logging
}
): Promise<any>
Expand Down
98 changes: 78 additions & 20 deletions dist/docx-preview.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/docx-preview.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/docx-preview.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/docx-preview.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "docx-preview",
"version": "0.1.11",
"version": "0.1.12",
"license": "Apache-2.0",
"keywords": [
"word",
Expand Down
63 changes: 48 additions & 15 deletions src/document-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class DocumentParser {
parseBodyElements(element: Element): OpenXmlElement[] {
var children = [];

xmlUtil.foreach(element, elem => {
for (let elem of xml.elements(element)) {
switch (elem.localName) {
case "p":
children.push(this.parseParagraph(elem));
Expand All @@ -108,10 +108,10 @@ export class DocumentParser {
break;

case "sdt":
this.parseSdt(elem).forEach(el => children.push(el));
children.push(...this.parseSdt(elem, e => this.parseBodyElements(e)));
break;
}
});
}

return children;
}
Expand Down Expand Up @@ -434,42 +434,68 @@ export class DocumentParser {
return result;
}

parseSdt(node: Element): OpenXmlElement[] {
parseSdt(node: Element, parser: Function): OpenXmlElement[] {
const sdtContent = xml.element(node, "sdtContent");
return sdtContent ? this.parseBodyElements(sdtContent) : [];
return sdtContent ? parser(sdtContent) : [];
}

parseInserted(node: Element, parentParser: Function): OpenXmlElement {
return <OpenXmlElement>{
type: DomType.Inserted,
children: parentParser(node)?.children ?? []
};
}

parseDeleted(node: Element, parentParser: Function): OpenXmlElement {
return <OpenXmlElement>{
type: DomType.Deleted,
children: parentParser(node)?.children ?? []
};
}

parseParagraph(node: Element): OpenXmlElement {
var result = <WmlParagraph>{ type: DomType.Paragraph, children: [] };

xmlUtil.foreach(node, c => {
switch (c.localName) {
for (let el of xml.elements(node)) {
switch (el.localName) {
case "pPr":
this.parseParagraphProperties(el, result);
break;

case "r":
result.children.push(this.parseRun(c, result));
result.children.push(this.parseRun(el, result));
break;

case "hyperlink":
result.children.push(this.parseHyperlink(c, result));
result.children.push(this.parseHyperlink(el, result));
break;

case "bookmarkStart":
result.children.push(parseBookmarkStart(c, xml));
result.children.push(parseBookmarkStart(el, xml));
break;

case "bookmarkEnd":
result.children.push(parseBookmarkEnd(c, xml));
result.children.push(parseBookmarkEnd(el, xml));
break;

case "oMath":
case "oMathPara":
result.children.push(this.parseMathElement(c));
result.children.push(this.parseMathElement(el));
break;

case "pPr":
this.parseParagraphProperties(c, result);
case "sdt":
result.children.push(...this.parseSdt(el, e => this.parseParagraph(e).children));
break;

case "ins":
result.children.push(this.parseInserted(el, e => this.parseParagraph(e)));
break;

case "del":
result.children.push(this.parseDeleted(el, e => this.parseParagraph(e)));
break;
}
});
}

return result;
}
Expand Down Expand Up @@ -547,6 +573,13 @@ export class DocumentParser {
});//.replace(" ", "\u00A0"); // TODO
break;

case "delText":
result.children.push(<WmlText>{
type: DomType.DeletedText,
text: c.textContent
});
break;

case "fldSimple":
result.children.push(<WmlFieldSimple>{
type: DomType.SimpleField,
Expand Down
5 changes: 4 additions & 1 deletion src/document/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export enum DomType {
MmlSuperArgument = "mmlSuperArgument",
MmlNary = "mmlNary",
MmlDelimiter = "mmlDelimiter",
VmlElement = "vmlElement"
VmlElement = "vmlElement",
Inserted = "inserted",
Deleted = "deleted",
DeletedText = "deletedText"
}

export interface OpenXmlElement {
Expand Down
4 changes: 3 additions & 1 deletion src/docx-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface Options {
ignoreLastRenderedPageBreak: boolean;
useBase64URL: boolean;
useMathMLPolyfill: boolean;
renderChanges: boolean;
}

export const defaultOptions: Options = {
Expand All @@ -37,7 +38,8 @@ export const defaultOptions: Options = {
renderFootnotes: true,
renderEndnotes: true,
useBase64URL: false,
useMathMLPolyfill: false
useMathMLPolyfill: false,
renderChanges: false
}

export function praseAsync(data: Blob | any, userOptions: Partial<Options> = null): Promise<any> {
Expand Down
Loading

0 comments on commit bf51a7c

Please sign in to comment.