Skip to content

Commit

Permalink
Parse and render Smart Tags
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitri-gb committed Apr 16, 2024
1 parent b42b380 commit ec0ba32
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 4 deletions.
28 changes: 28 additions & 0 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.

28 changes: 27 additions & 1 deletion src/document-parser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
DomType, WmlTable, IDomNumbering,
WmlHyperlink, IDomImage, OpenXmlElement, WmlTableColumn, WmlTableCell,
WmlHyperlink, WmlSmartTag, IDomImage, OpenXmlElement, WmlTableColumn, WmlTableCell,
WmlTableRow, NumberingPicBullet, WmlText, WmlSymbol, WmlBreak, WmlNoteReference
} from './document/dom';
import { DocumentElement } from './document/document';
Expand Down Expand Up @@ -502,6 +502,10 @@ export class DocumentParser {
case "hyperlink":
result.children.push(this.parseHyperlink(el, result));
break;

case "smartTag":
result.children.push(this.parseSmartTag(el, result));
break;

case "bookmarkStart":
result.children.push(parseBookmarkStart(el, xml));
Expand Down Expand Up @@ -599,6 +603,28 @@ export class DocumentParser {

return result;
}

parseSmartTag(node: Element, parent?: OpenXmlElement): WmlSmartTag {
var result: WmlSmartTag = { type: DomType.SmartTag, parent, children: [] };
var uri = xml.attr(node, "uri");
var element = xml.attr(node, "element");

if (uri)
result.uri = uri;

if (element)
result.element = element;

xmlUtil.foreach(node, c => {
switch (c.localName) {
case "r":
result.children.push(this.parseRun(c, result));
break;
}
});

return result;
}

parseRun(node: Element, parent?: OpenXmlElement): WmlRun {
var result: WmlRun = <WmlRun>{ type: DomType.Run, parent: parent, children: [] };
Expand Down
6 changes: 6 additions & 0 deletions src/document/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export enum DomType {
Row = "row",
Cell = "cell",
Hyperlink = "hyperlink",
SmartTag = "smartTag",
Drawing = "drawing",
Image = "image",
Text = "text",
Expand Down Expand Up @@ -90,6 +91,11 @@ export interface WmlHyperlink extends OpenXmlElement {
href?: string;
}

export interface WmlSmartTag extends OpenXmlElement {
uri?: string;
element?: string;
}

export interface WmlNoteReference extends OpenXmlElement {
id: string;
}
Expand Down
13 changes: 11 additions & 2 deletions src/html-renderer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { WordDocument } from './word-document';
import {
DomType, WmlTable, IDomNumbering,
WmlHyperlink, IDomImage, OpenXmlElement, WmlTableColumn, WmlTableCell, WmlText, WmlSymbol, WmlBreak, WmlNoteReference
WmlHyperlink, IDomImage, OpenXmlElement, WmlTableColumn, WmlTableCell, WmlText, WmlSymbol, WmlBreak, WmlNoteReference,
WmlSmartTag
} from './document/dom';
import { CommonProperties } from './document/common';
import { Options } from './docx-preview';
Expand Down Expand Up @@ -734,6 +735,9 @@ section.${c}>footer { z-index: 1; }

case DomType.Hyperlink:
return this.renderHyperlink(elem);

case DomType.SmartTag:
return this.renderSmartTag(elem);

case DomType.Drawing:
return this.renderDrawing(elem);
Expand Down Expand Up @@ -947,7 +951,12 @@ section.${c}>footer { z-index: 1; }

return result;
}


renderSmartTag(elem: WmlSmartTag) {
var result = this.createElement("span");
this.renderChildren(elem, result);
return result;
}

renderCommentRangeStart(commentStart: WmlCommentRangeStart) {
if (!this.options.renderComments)
Expand Down

0 comments on commit ec0ba32

Please sign in to comment.