docx to mammoth roundtrip: working versions (tested 2026-10-15)
Build a .docx in memory with docx (heading, paragraph, table), then read it back with mammoth's convertToHtml and extractRawText and assert the content, fully offline.
passed3 passed · 0 failed2026-10-15 22:58 UTC- Status
- passed
- Last passed
- 2026-10-15 22:58 UTC
- Tests
- 3 / 3
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions4
docx@9.11.0mammoth@1.13.0@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38820570375
The code
Exactly the files that passed. Your agent can pull them with get_recipe("docx-mammoth-roundtrip") (install), or see them on GitHub.
package.json
{
"name": "docx-mammoth-roundtrip",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"docx": "9.11.0",
"mammoth": "1.13.0"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15"
}
}
src/generate.ts
import {
Document,
HeadingLevel,
Packer,
Paragraph,
Table,
TableCell,
TableRow,
} from "docx";
export async function generateDocx(): Promise<Buffer> {
const doc = new Document({
sections: [
{
children: [
new Paragraph({
text: "Quarterly Report",
heading: HeadingLevel.HEADING_1,
}),
new Paragraph({
text: "This document is generated entirely in memory for roundtrip testing.",
}),
new Table({
rows: [
new TableRow({
children: [
new TableCell({ children: [new Paragraph("Item")] }),
new TableCell({ children: [new Paragraph("Qty")] }),
],
}),
new TableRow({
children: [
new TableCell({ children: [new Paragraph("Widgets")] }),
new TableCell({ children: [new Paragraph("3")] }),
],
}),
new TableRow({
children: [
new TableCell({ children: [new Paragraph("Gadgets")] }),
new TableCell({ children: [new Paragraph("5")] }),
],
}),
],
}),
],
},
],
});
return Packer.toBuffer(doc);
}
src/read.ts
import mammoth from "mammoth";
export async function toHtml(buffer: Buffer): Promise<string> {
const result = await mammoth.convertToHtml({ buffer });
return result.value;
}
export async function toRawText(buffer: Buffer): Promise<string> {
const result = await mammoth.extractRawText({ buffer });
return result.value;
}
test/roundtrip.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { generateDocx } from "../src/generate.ts";
import { toHtml, toRawText } from "../src/read.ts";
test("generates a non-empty in-memory docx buffer", async () => {
const buffer = await generateDocx();
assert.ok(Buffer.isBuffer(buffer));
assert.ok(buffer.length > 0);
assert.equal(buffer.subarray(0, 2).toString("binary"), "PK");
});
test("convertToHtml renders the heading, paragraph and table", async () => {
const buffer = await generateDocx();
const html = await toHtml(buffer);
assert.match(html, /<h1>Quarterly Report<\/h1>/);
assert.match(html, /generated entirely in memory/);
assert.match(html, /<table>/);
assert.match(html, /<td><p>Widgets<\/p><\/td>/);
assert.match(html, /<td><p>3<\/p><\/td>/);
});
test("extractRawText preserves the document text", async () => {
const buffer = await generateDocx();
const text = await toRawText(buffer);
assert.match(text, /Quarterly Report/);
assert.match(text, /generated entirely in memory/);
assert.match(text, /Widgets/);
assert.match(text, /Gadgets/);
assert.match(text, /\b3\b/);
assert.match(text, /\b5\b/);
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.