From 727045c3be1e58b1566da7b08014ba3bbd1ae5a8 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Mon, 21 Dec 2020 18:44:31 +0900 Subject: [PATCH] Add json if (#217) * fix: json * fix: specs * 0.0.127 * fix if * fix: if * 0.1.129 --- README.md | 8 +++--- docx-core/src/documents/mod.rs | 2 +- docx-wasm/js/index.ts | 14 +++++++++- docx-wasm/package.json | 2 +- docx-wasm/src/doc.rs | 4 +++ docx-wasm/test/index.test.js | 47 ++++++++++++++++++---------------- 6 files changed, 48 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 5972a61..43a2bf2 100644 --- a/README.md +++ b/README.md @@ -50,12 +50,12 @@ import { saveAs } from "file-saver"; // // Note that a dynamic `import` statement here is required due to webpack/webpack#6615, import("docx-wasm").then(w => { - const buf = new w.Docx() + const { buffer } = new w.Docx() .addParagraph( new w.Paragraph().addRun(new w.Run().addText("Hello world!!")) ) .build(); - saveAs(new Blob([buf]), "hello.docx"); + saveAs(new Blob([buffer]), "hello.docx"); }); ``` @@ -65,11 +65,11 @@ import("docx-wasm").then(w => { const w = require("docx-wasm"); const { writeFileSync } = require("fs"); -const buf = new w.Docx() +const { buffer } = new w.Docx() .addParagraph(new w.Paragraph().addRun(new w.Run().addText("Hello world!!"))) .build(); -writeFileSync("hello.docx", buf); +writeFileSync("hello.docx", buffer); ``` ### More examples diff --git a/docx-core/src/documents/mod.rs b/docx-core/src/documents/mod.rs index 1595787..fad2a07 100644 --- a/docx-core/src/documents/mod.rs +++ b/docx-core/src/documents/mod.rs @@ -262,7 +262,7 @@ impl Docx { } } - pub fn json(&mut self) -> String { + pub fn json(&self) -> String { serde_json::to_string_pretty(&self).unwrap() } diff --git a/docx-wasm/js/index.ts b/docx-wasm/js/index.ts index 1bd3ea3..48359f1 100644 --- a/docx-wasm/js/index.ts +++ b/docx-wasm/js/index.ts @@ -563,7 +563,7 @@ export class Docx { return level; } - build() { + createDocx(): wasm.Docx { let docx = wasm.createDocx(); this.children.forEach((child) => { @@ -665,6 +665,18 @@ export class Docx { } } + return docx; + } + + json() { + const docx = this.createDocx(); + const json = docx.json(); + docx.free(); + return json; + } + + build() { + const docx = this.createDocx(); const buf = docx.build(this.hasNumberings); docx.free(); return buf; diff --git a/docx-wasm/package.json b/docx-wasm/package.json index 494b88e..8a019bc 100644 --- a/docx-wasm/package.json +++ b/docx-wasm/package.json @@ -1,6 +1,6 @@ { "name": "docx-wasm", - "version": "0.0.125", + "version": "0.0.129", "main": "dist/node/index.js", "browser": "dist/web/index.js", "author": "bokuweb ", diff --git a/docx-wasm/src/doc.rs b/docx-wasm/src/doc.rs index 92e2d72..00bf8da 100644 --- a/docx-wasm/src/doc.rs +++ b/docx-wasm/src/doc.rs @@ -90,4 +90,8 @@ impl Docx { } Ok(cur.into_inner()) } + + pub fn json(&self) -> String { + self.0.json() + } } diff --git a/docx-wasm/test/index.test.js b/docx-wasm/test/index.test.js index 83b47b1..db8453a 100644 --- a/docx-wasm/test/index.test.js +++ b/docx-wasm/test/index.test.js @@ -4,28 +4,28 @@ const Zip = require("adm-zip"); describe("reader", () => { test("should read lvlOverride docx", () => { - const buf = readFileSync("../fixtures/lvl_override/override.docx"); - const json = w.readDocx(buf); + const buffer = readFileSync("../fixtures/lvl_override/override.docx"); + const json = w.readDocx(buffer); expect(json).toMatchSnapshot(); }); test("should read gridAfter docx", () => { - const buf = readFileSync("../fixtures/grid_after/grid_after.docx"); - const json = w.readDocx(buf); + const buffer = readFileSync("../fixtures/grid_after/grid_after.docx"); + const json = w.readDocx(buffer); expect(json).toMatchSnapshot(); }); test("should read table style docx", () => { - const buf = readFileSync("../fixtures/table_style/table_style.docx"); - const json = w.readDocx(buf); + const buffer = readFileSync("../fixtures/table_style/table_style.docx"); + const json = w.readDocx(buffer); expect(json).toMatchSnapshot(); }); test("should read extended comments docx", () => { - const buf = readFileSync( + const buffer = readFileSync( "../fixtures/extended_comments/extended_comments.docx" ); - const json = w.readDocx(buf); + const json = w.readDocx(buffer); expect(json).toMatchSnapshot(); }); @@ -41,8 +41,8 @@ describe("reader", () => { describe("writer", () => { test("should write hello", () => { const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!")); - const buf = new w.Docx().addParagraph(p).build(); - const z = new Zip(Buffer.from(buf)); + const buffer = new w.Docx().addParagraph(p).build(); + const z = new Zip(Buffer.from(buffer)); for (const e of z.getEntries()) { if (e.entryName.match(/document.xml|numbering.xml/)) { expect(z.readAsText(e)).toMatchSnapshot(); @@ -60,13 +60,13 @@ describe("writer", () => { new w.Level(0, 3, "decimal", "%1", "left") ) ); - const buf = new w.Docx() + const buffer = new w.Docx() .addParagraph(p) .addAbstractNumbering(new w.AbstractNumbering(0)) .addNumbering(num) .build(); - const z = new Zip(Buffer.from(buf)); + const z = new Zip(Buffer.from(buffer)); for (const e of z.getEntries()) { if (e.entryName.match(/document.xml|numbering.xml/)) { expect(z.readAsText(e)).toMatchSnapshot(); @@ -76,8 +76,8 @@ describe("writer", () => { test("should write page size", () => { const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!")); - const buf = new w.Docx().addParagraph(p).pageSize(400, 800).build(); - const z = new Zip(Buffer.from(buf)); + const buffer = new w.Docx().addParagraph(p).pageSize(400, 800).build(); + const z = new Zip(Buffer.from(buffer)); for (const e of z.getEntries()) { if (e.entryName.match(/document.xml|numbering.xml/)) { expect(z.readAsText(e)).toMatchSnapshot(); @@ -87,11 +87,11 @@ describe("writer", () => { test("should write page margin", () => { const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!")); - const buf = new w.Docx() + const buffer = new w.Docx() .addParagraph(p) .pageMargin({ top: 1000, left: 2000 }) .build(); - const z = new Zip(Buffer.from(buf)); + const z = new Zip(Buffer.from(buffer)); for (const e of z.getEntries()) { if (e.entryName.match(/document.xml|numbering.xml/)) { expect(z.readAsText(e)).toMatchSnapshot(); @@ -105,13 +105,13 @@ describe("writer", () => { .eastAsia("Arial") .ascii("Arial") .hiAnsi("Arial"); - const buf = new w.Docx() + const buffer = new w.Docx() .addParagraph(p) .defaultSize(40) .defaultFonts(fonts) .build(); - writeFileSync("../output/default_font.docx", buf); - const z = new Zip(Buffer.from(buf)); + writeFileSync("../output/default_font.docx", buffer); + const z = new Zip(Buffer.from(buffer)); for (const e of z.getEntries()) { if (e.entryName.match(/document.xml|numbering.xml/)) { expect(z.readAsText(e)).toMatchSnapshot(); @@ -121,9 +121,12 @@ describe("writer", () => { test("should write doc vars", () => { const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!!!")); - const buf = new w.Docx().addParagraph(p).addDocVar("foo", "bar").build(); - writeFileSync("../output/doc_vars.docx", buf); - const z = new Zip(Buffer.from(buf)); + const buffer = new w.Docx() + .addParagraph(p) + .addDocVar("foo", "bar") + .build(); + writeFileSync("../output/doc_vars.docx", buffer); + const z = new Zip(Buffer.from(buffer)); for (const e of z.getEntries()) { if (e.entryName.match(/document.xml|numbering.xml/)) { expect(z.readAsText(e)).toMatchSnapshot();