docx-handlebars-0.1.0 has been yanked.
docx-handlebars

一个用于处理 DOCX 文件 Handlebars 模板的 Rust 库,支持多平台使用:
- 🦀 Rust 原生
- 🌐 WebAssembly (WASM)
- 📦 npm 包
- 🟢 Node.js
- 🦕 Deno
- 🌍 浏览器端
- 📋 JSR (JavaScript Registry)
功能特性
- ✅ 解析和处理 DOCX 文件
- ✅ Handlebars 模板引擎集成
- ✅ 支持复杂的模板语法(循环、条件等)
- ✅ 跨平台兼容性
- ✅ TypeScript 类型定义
- ✅ 零依赖的 WASM 二进制文件
安装
Rust
[dependencies]
docx-handlebars = "0.1"
npm
npm install docx-handlebars
Deno
import { DocxHandlebars } from "https://deno.land/x/docx_handlebars/mod.ts";
JSR
npx jsr add @sail/docx-handlebars
使用示例
Rust
use docx_handlebars::{DocxHandlebars, TemplateData};
use std::collections::HashMap;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut processor = DocxHandlebars::new();
let template_bytes = std::fs::read("template.docx")?;
processor.load_template(&template_bytes)?;
let mut data = HashMap::new();
data.insert("name".to_string(), "张三".into());
data.insert("company".to_string(), "ABC公司".into());
let result = processor.render(&data)?;
std::fs::write("output.docx", result)?;
Ok(())
}
JavaScript/TypeScript (Node.js)
import { DocxHandlebars } from 'docx-handlebars';
import fs from 'fs';
async function processTemplate() {
const processor = new DocxHandlebars();
const templateBuffer = fs.readFileSync('template.docx');
await processor.loadTemplate(templateBuffer);
const data = {
name: '张三',
company: 'ABC公司',
items: [
{ product: '产品A', price: 100 },
{ product: '产品B', price: 200 }
]
};
const result = await processor.render(data);
fs.writeFileSync('output.docx', result);
}
processTemplate().catch(console.error);
浏览器端
<!DOCTYPE html>
<html>
<head>
<title>DOCX Handlebars Demo</title>
</head>
<body>
<input type="file" id="templateFile" accept=".docx">
<button onclick="processTemplate()">处理模板</button>
<a id="downloadLink" style="display:none">下载结果</a>
<script type="module">
import init, { DocxHandlebars } from './pkg/docx_handlebars.js';
async function initWasm() {
await init();
}
window.processTemplate = async function() {
const fileInput = document.getElementById('templateFile');
const file = fileInput.files[0];
if (!file) {
alert('请选择一个 DOCX 文件');
return;
}
const arrayBuffer = await file.arrayBuffer();
const bytes = new Uint8Array(arrayBuffer);
const processor = new DocxHandlebars();
processor.load_template(bytes);
const data = {
name: '张三',
company: 'ABC公司'
};
const result = processor.render(JSON.stringify(data));
const blob = new Blob([result], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' });
const url = URL.createObjectURL(blob);
const link = document.getElementById('downloadLink');
link.href = url;
link.download = 'output.docx';
link.style.display = 'block';
link.textContent = '下载处理后的文档';
};
initWasm();
</script>
</body>
</html>
Deno
import { DocxHandlebars } from "https://deno.land/x/docx_handlebars/mod.ts";
const processor = new DocxHandlebars();
// 加载模板
const templateBytes = await Deno.readFile("template.docx");
await processor.loadTemplate(templateBytes);
// 渲染数据
const data = {
name: "张三",
company: "ABC公司"
};
const result = await processor.render(data);
// 保存结果
await Deno.writeFile("output.docx", result);
模板语法
支持完整的 Handlebars 语法:
{{name}} 在 {{company}} 工作
{{#if hasItems}}
产品列表:
{{#each items}}
- {{product}}: ¥{{price}}
{{/each}}
{{/if}}
{{#unless isEmpty}}
总计: ¥{{total}}
{{/unless}}
开发
前置条件
- Rust 1.70+
- wasm-pack
- Node.js 16+
构建
cargo build --release
wasm-pack build --target web --out-dir pkg
wasm-pack build --target nodejs --out-dir pkg-node
cargo test
wasm-pack test --headless --firefox
发布
cargo publish
cd pkg && npm publish
deno publish
许可证
本项目采用 MIT 或 Apache-2.0 双重许可证。
贡献
欢迎提交 Issue 和 Pull Request!
更新日志
0.1.0
- 初始版本
- 基本的 DOCX 模板处理功能
- 支持多平台部署
测试
浏览器兼容性测试
本项目提供了完整的浏览器兼容性测试套件,确保 npm 包在各种浏览器环境中正常工作:
cd npm_test
node server.js
测试功能包括:
- ✅ 包加载测试(多种构建版本)
- ✅ WASM 模块初始化
- ✅ 基础功能验证
- ✅ 实际文件处理测试
- ✅ 文件下载功能
支持的构建版本:
pkg/ - Web 目标,适用于浏览器
pkg-node/ - Node.js 目标,适用于服务端
pkg-bundler/ - Bundler 目标,适用于打包工具
JSR 包测试
cd jsr_test
deno run --allow-net --allow-read --allow-write test.ts
deno run --allow-net --allow-read --allow-write comprehensive_test.ts
npm 包测试
cd npm_test
npm install
node test.js