docx-handlebars-0.2.4 has been yanked.
docx-handlebars
δΈζζζ‘£ | English
A Rust library for processing DOCX files with Handlebars templates, supporting multiple platforms:
- π¦ Rust native
- π WebAssembly (WASM)
- π¦ npm package
- π’ Node.js
- π¦ Deno
- π Browser
- π JSR (JavaScript Registry)
Features
- β Smart Merging: Automatically handles Handlebars syntax split by XML tags
- β DOCX Validation: Built-in file format validation to ensure valid input files
- β Handlebars Support: Full template engine with variables, conditionals, loops, and helper functions
- β Cross-platform: Rust native + WASM support for multiple runtimes
- β TypeScript: Complete type definitions and intelligent code completion
- β Zero Dependencies: WASM binary with no external dependencies
- β Error Handling: Detailed error messages and type-safe error handling
Installation
Rust
npm
Deno
import { render, init } from "jsr:@sail/docx-handlebars";
Usage Examples
Rust
use render_handlebars;
use json;
JavaScript/TypeScript (Node.js)
import from 'docx-handlebars';
import fs from 'fs';
.;
Deno
import { render, init } from "https://deno.land/x/docx_handlebars/mod.ts";
async function processTemplate() {
// Initialize WASM module
await init();
// Read template file
const templateBytes = await Deno.readFile("template.docx");
// Prepare data
const data = {
name: "Alice Johnson",
department: "R&D Department",
projects: [
{ name: "AI Customer Service", status: "Live" },
{ name: "Data Visualization Platform", status: "In Development" }
]
};
// Render template
const result = render(templateBytes, JSON.stringify(data));
// Save result
await Deno.writeFile("output.docx", new Uint8Array(result));
}
if (import.meta.main) {
await processTemplate();
}
Browser
DOCX Handlebars Example
Process Template
Template Syntax
Basic Variable Substitution
Employee Name: {{name}}
Company: {{company}}
Position: {{position}}
Conditional Rendering
{{#if has_bonus}}
Bonus: ${{bonus_amount}}
{{else}}
No bonus
{{/if}}
{{#unless is_intern}}
Full-time employee
{{/unless}}
Loop Rendering
Project Experience:
{{#each projects}}
- {{name}}: {{description}} ({{status}})
{{/each}}
Skills:
{{#each skills}}
{{@index}}. {{this}}
{{/each}}
Helper Functions
Built-in helper functions:
{{upper name}} <!-- Convert to uppercase -->
{{lower company}} <!-- Convert to lowercase -->
{{len projects}} <!-- Array length -->
{{#if (eq status "completed")}}Completed{{/if}} <!-- Equality comparison -->
{{#if (gt score 90)}}Excellent{{/if}} <!-- Greater than comparison -->
{{#if (lt age 30)}}Young{{/if}} <!-- Less than comparison -->
Complex Example
=== Employee Report ===
Basic Information:
Name: {{employee.name}}
Department: {{employee.department}}
Position: {{employee.position}}
Hire Date: {{employee.hire_date}}
{{#if employee.has_bonus}}
π° Bonus: ${{employee.bonus_amount}}
{{/if}}
Project Experience ({{len projects}} total):
{{#each projects}}
{{@index}}. {{name}}
Description: {{description}}
Status: {{status}}
Team Size: {{team_size}} people
{{/each}}
Skills Assessment:
{{#each skills}}
- {{name}}: {{level}}/10 ({{years}} years experience)
{{/each}}
To delete an entire table row, simply add the following to any cell in that row:
{{removeTableRow}}
{{#if (gt performance.score 90)}}
π Performance Rating: Excellent
{{else if (gt performance.score 80)}}
π Performance Rating: Good
{{else}}
π Performance Rating: Needs Improvement
{{/if}}
Image:
{{img base64_image_data [width] [height]}}
only height: {{img base64 "" 200}}
only width: {{img base64 300}}
no width/height: {{img base64}}
both width/height: {{img base64 300 200}}
Error Handling
The library provides detailed error types and messages:
Rust
use ;
match render_handlebars
JavaScript/TypeScript
try catch
Build and Development
Build WASM Packages
# Build all targets
# Or build separately
Run Examples
# Rust example
# Node.js example
# Deno example
# Browser example
# Then open http://localhost:8080 in your browser
# Select examples/template.docx file to test
Technical Features
Smart Merging Algorithm
The core innovation of this library is the intelligent merging of Handlebars syntax that has been split by XML tags. In DOCX files, when users input template syntax, Word may split it into multiple XML tags.
File Validation
Built-in DOCX file validation ensures input file integrity:
- ZIP Format Validation: Check file signature and structure
- DOCX Structure Validation: Ensure required files are present
[Content_Types].xml_rels/.relsword/document.xml
- MIME Type Validation: Verify correct content types
Performance and Compatibility
- Zero Copy: Efficient memory management between Rust and WASM
- Streaming Processing: Suitable for handling large DOCX files
- Cross-platform: Support for Windows, macOS, Linux, Web
- Modern Browsers: Support for all modern browsers that support WASM
License
This project is licensed under the MIT License - see the LICENSE-MIT file for details.
Support
- π Documentation
- π Issue Tracker
- π¬ Discussions