diff --git a/ts/docx/paragraph/index.ts b/ts/docx/paragraph/index.ts index fa16b00970..b811d9f2d3 100644 --- a/ts/docx/paragraph/index.ts +++ b/ts/docx/paragraph/index.ts @@ -22,7 +22,6 @@ export class Paragraph extends XmlComponent { constructor(text?: string) { super("w:p"); - this.root.push(new Attributes()); this.properties = new ParagraphProperties(); this.root.push(this.properties); this.root.push(new TextRun(text)); diff --git a/ts/export/formatter.ts b/ts/export/formatter.ts index e3ae7ca590..bde53e9b6f 100644 --- a/ts/export/formatter.ts +++ b/ts/export/formatter.ts @@ -4,8 +4,9 @@ export class Formatter { format(input: any): Object { this.deepTraverseJson(input, (parent, value, key) => { - if (isNaN(key)) { - var newKey = this.getReplacementKey(parent, key); + if (isNaN(key) && key !== "rootKey") { + var newKey = parent.rootKey; + console.log(key); if (newKey !== key) { parent[newKey] = parent[key]; delete parent[key]; @@ -30,6 +31,9 @@ export class Formatter { if (key === "xmlKeys") { delete parent[key]; } + if (key === "rootKey") { + delete parent[key]; + } }); return newJson @@ -42,21 +46,11 @@ export class Formatter { private deepTraverseJson(json: Object, lambda: (json: any, value: any, key: any) => void): void { _.forOwn(json, (value, key) => { - if (_.isObject(value) && key !== "xmlKeys") { + if (_.isObject(value) && key !== "xmlKeys" && key != "rootKey") { this.deepTraverseJson(value, lambda); } lambda(json, value, key); }); } - private getReplacementKey(input: any, key: string): string { - var newKey = input.xmlKeys[key]; - - if (newKey !== undefined) { - return newKey; - } else { - return key; - } - } - } \ No newline at end of file diff --git a/ts/tests/formatterTest.ts b/ts/tests/formatterTest.ts index 146e180dd7..0369510ec3 100644 --- a/ts/tests/formatterTest.ts +++ b/ts/tests/formatterTest.ts @@ -36,11 +36,12 @@ describe("Formatter", () => { assert(stringifiedJson.indexOf("xmlKeys") < 0); }); - it("should format simple paragraph with bold text", () => { + it.only("should format simple paragraph with bold text", () => { var paragraph = new docx.Paragraph(); paragraph.addText(new docx.TextRun("test").bold()); var newJson = formatter.format(paragraph); newJson = jsonify(newJson); + console.log(JSON.stringify(newJson, null, " ")); assert.isDefined(newJson["w:p"][3]["w:r"][0]["w:rPr"][0]["w:b"][0]["_attr"]["w:val"]); }); diff --git a/ts/tests/paragraphTest.ts b/ts/tests/paragraphTest.ts index 591b68ee08..38eedf8887 100644 --- a/ts/tests/paragraphTest.ts +++ b/ts/tests/paragraphTest.ts @@ -35,7 +35,7 @@ describe("Paragraph", () => { paragraph.heading1(); var newJson = jsonify(paragraph); - assert(newJson.p[1].pPr[1].pStyle[0]._attr.val === "Heading1"); + assert(newJson.root[1].root[1].root[0]._attr.val === "Heading1"); }); });