Skip to content

Commit

Permalink
Add output option
Browse files Browse the repository at this point in the history
  • Loading branch information
inokawa committed Jan 11, 2022
1 parent a58fb3b commit bfd7b0b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ npm install remark-docx
```javascript
import { unified } from "unified";
import markdown from "remark-parse";
import docx, { Packer } from "remark-docx";
import docx from "remark-docx";
import { saveAs } from "file-saver";

const processor = unified().use(markdown).use(docx);
const processor = unified().use(markdown).use(docx, { output: "blob" });

const text = "# hello world";

(async () => {
const doc = await processor.process(text);
const blob = await Packer.toBlob(doc.result);
const blob = await doc.result;
saveAs(blob, "example.docx");
})();
```
Expand All @@ -46,23 +46,24 @@ const text = "# hello world";
```javascript
import { unified } from "unified";
import markdown from "remark-parse";
import docx, { Packer } from "remark-docx";
import docx from "remark-docx";
import * as fs from "fs";

const processor = unified().use(markdown).use(docx);
const processor = unified().use(markdown).use(docx, { output: "buffer" });

const text = "# hello world";

(async () => {
const doc = await processor.process(text);
const buffer = await Packer.toBuffer(doc.result);
const buffer = await doc.result;
fs.writeFileSync("example.docx", buffer);
})();
```

## Options

| Key | Default | Type | Description |
| ------------- | --------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| docProperties | undefined | object | Override properties of document. |
| imageResolver | undefined | ImageResolver | **You must set** if your markdown includes images. See example for [browser](https://github.com/inokawa/remark-docx/blob/main/stories/playground.stories.tsx) and [Node.js](https://github.com/inokawa/remark-docx/blob/main/src/index.spec.ts). |
| Key | Default | Type | Description |
| ------------- | --------- | --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| output | "buffer" | `"buffer"` `"blob"` `"raw"` | Set output type of `VFile.result`. `buffer` is `Promise<ArrayBuffer>`. `blob` is `Promise<Blob>`. `raw` is internal data for testing. |
| docProperties | undefined | object | Override properties of document. |
| imageResolver | undefined | ImageResolver | **You must set** if your markdown includes images. See example for [browser](https://github.com/inokawa/remark-docx/blob/main/stories/playground.stories.tsx) and [Node.js](https://github.com/inokawa/remark-docx/blob/main/src/index.spec.ts). |
1 change: 1 addition & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe("e2e", () => {
.use(frontmatter, ["yaml", "toml"])
.use(math)
.use(docx, {
output: "raw",
imageResolver: (url) => ({
image: new Uint8Array(),
width: 600,
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { default } from "./plugin";
export type { Options as DocxOptions } from "./plugin";
export { Packer } from "docx";
15 changes: 12 additions & 3 deletions src/transformer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as docx from "docx";
import { convertInchesToTwip } from "docx";
import { convertInchesToTwip, Packer } from "docx";
import { IPropertiesOptions } from "docx/build/file/core-properties";
import * as mdast from "./models/mdast";

Expand Down Expand Up @@ -95,6 +95,7 @@ type Context = Readonly<{
}>;

export type Opts = {
output?: "buffer" | "blob" | "raw";
docProperties?: Omit<IPropertiesOptions, "sections" | "numbering">;
imageResolver?: ImageResolver;
};
Expand All @@ -110,8 +111,16 @@ export function mdastToDocx(
node: mdast.Root,
opts: Opts,
images: ImageDataMap
): docx.File {
return buildDocxRoot(node, opts, images);
): Promise<any> | docx.File {
const doc = buildDocxRoot(node, opts, images);
switch (opts.output ?? "buffer") {
case "buffer":
return Packer.toBuffer(doc);
case "blob":
return Packer.toBlob(doc);
case "raw":
return doc;
}
}

function buildDocxRoot(
Expand Down
5 changes: 3 additions & 2 deletions stories/playground.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { unified } from "unified";
import markdown from "remark-parse";
import gfm from "remark-gfm";
import frontmatter from "remark-frontmatter";
import docx, { Packer } from "../src";
import docx from "../src";
import TextEditor from "./components/text-editor";
// @ts-expect-error no type definition
import text from "../fixtures/article.md";
Expand Down Expand Up @@ -33,12 +33,13 @@ const toDocxProcessor = unified()
.use(gfm)
.use(frontmatter)
.use(docx, {
output: "blob",
imageResolver: fetchImage,
});

const toDocx = async (s: string) => {
const doc = await toDocxProcessor.process(s);
return Packer.toBlob(doc.result as any);
return doc.result;
};

export default {
Expand Down

0 comments on commit bfd7b0b

Please sign in to comment.