Skip to content

Commit fb354e7

Browse files
committed
Update unit tests
1 parent 744b93a commit fb354e7

File tree

6 files changed

+77
-35
lines changed

6 files changed

+77
-35
lines changed

examples/nextjs/src/app/api/docx/route.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import remarkMath from "remark-math";
77
import { toDocx } from "mdast2docx";
88
import { tablePlugin, listPlugin, mathPlugin } from "mdast2docx/dist/plugins";
99
import { NextResponse } from "next/server";
10+
import { emojiPlugin } from "@m2d/emoji";
1011

1112
const docxProcessor = unified()
1213
.use(remarkParse)
@@ -24,7 +25,7 @@ export const GET = async () => {
2425
const buffer = await toDocx(
2526
mdast,
2627
{},
27-
{ plugins: [tablePlugin(), listPlugin(), mathPlugin()] },
28+
{ plugins: [tablePlugin(), listPlugin(), mathPlugin(), emojiPlugin()] },
2829
"arraybuffer",
2930
);
3031
return new NextResponse(new Uint8Array(buffer as ArrayBuffer), {

lib/__tests__/index.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { describe, it, expect } from "vitest";
2+
import { toDocx } from "@m2d/core"; // Adjust path based on your setup
3+
import { unified } from "unified";
4+
import remarkParse from "remark-parse";
5+
import remarkGfm from "remark-gfm";
6+
import fs from "fs";
7+
import { emojiPlugin } from "../src";
8+
9+
const markdown = fs.readFileSync("../sample.md", "utf-8");
10+
11+
describe("toDocx", () => {
12+
it("should handle emojis", async ({ expect }) => {
13+
const mdast = unified().use(remarkParse).use(remarkGfm).parse(markdown);
14+
15+
const docxBlob = await toDocx(mdast, {}, { plugins: [emojiPlugin()] });
16+
17+
expect(docxBlob).toBeInstanceOf(Blob);
18+
});
19+
});

lib/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@
4343
"docx": "^9.3.0",
4444
"esbuild-plugin-rdi": "^0.0.0",
4545
"jsdom": "^26.0.0",
46+
"remark-gfm": "^4.0.1",
47+
"remark-parse": "^11.0.0",
4648
"tsup": "^8.4.0",
4749
"typescript": "^5.8.2",
50+
"unified": "^11.0.5",
4851
"vite-tsconfig-paths": "^5.1.4",
4952
"vitest": "^3.0.9"
5053
},
@@ -75,8 +78,8 @@
7578
"mdast-to-docx",
7679
"markdown",
7780
"mdast",
78-
"mdast2docx",
7981
"md2docx",
82+
"mdast2docx",
8083
"plugin",
8184
"converter",
8285
"docx",

lib/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const emojiPlugin: () => IPlugin = () => ({
66
if (node.type === "text") {
77
node.value = node.value.replace(
88
/:[a-z0-9_+-]+:/g,
9-
(match: keyof typeof emojis) => emojis[match] ?? match,
9+
match => emojis[match.slice(1, -1) as keyof typeof emojis] ?? match,
1010
);
1111
}
1212
return [];

packages/shared/src/client/demo/demo.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ import demoCode from "./demo.tsx?raw";
1414
import { useState } from "react";
1515
// import { remarkDocx } from "@m2d/remark-docx";
1616
import { toDocx } from "mdast2docx";
17+
import { emojiPlugin } from "@m2d/emoji";
18+
import {
19+
tablePlugin,
20+
listPlugin,
21+
mathPlugin,
22+
imagePlugin,
23+
htmlPlugin,
24+
} from "mdast2docx/dist/plugins";
1725

1826
/** React live demo */
1927
export function Demo() {
@@ -38,7 +46,20 @@ export function Demo() {
3846
const downloadDocx = () => {
3947
setLoading(true);
4048

41-
toDocx(mdast).then(blob => {
49+
toDocx(
50+
mdast,
51+
{},
52+
{
53+
plugins: [
54+
htmlPlugin(),
55+
tablePlugin(),
56+
listPlugin(),
57+
mathPlugin(),
58+
emojiPlugin(),
59+
imagePlugin(),
60+
],
61+
},
62+
).then(blob => {
4263
const url = URL.createObjectURL(blob as Blob);
4364
const link = document.createElement("a");
4465
link.href = url;

pnpm-lock.yaml

Lines changed: 29 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)