peasy-document
v0.2.2
Published
Document conversion library — Markdown, HTML, CSV, JSON, YAML. Zero dependencies, TypeScript-first.
Maintainers
Readme
peasy-document
Pure TypeScript document conversion library for Markdown, HTML, CSV, JSON, and YAML transformations. Convert between 6 document formats with 10 conversion functions and frozen result objects -- all with zero runtime dependencies. Every conversion uses pure string processing, making it lightweight and fast for any JavaScript or TypeScript project.
Part of the Peasy Tools developer tools ecosystem.
Table of Contents
- Install
- Quick Start
- What You Can Do
- TypeScript Types
- API Reference
- REST API Client
- Learn More
- Also Available
- Peasy Developer Tools
- License
Install
npm install peasy-documentZero runtime dependencies. Works in Node.js, Deno, Bun, and modern bundlers.
Quick Start
import { markdownToHtml, csvToJson, htmlToText } from "peasy-document";
// Convert Markdown to HTML with headings, bold, italic, code blocks, and lists
const result = markdownToHtml("# Hello World\n\nThis is **bold** text.");
console.log(result.content);
// <h1>Hello World</h1>
// <p>This is <strong>bold</strong> text.</p>
// Convert CSV data to JSON array of objects
const json = csvToJson("name,age\nAlice,30\nBob,25");
console.log(json.content);
// [{"name": "Alice", "age": "30"}, {"name": "Bob", "age": "25"}]
// Strip HTML to plain text with entity decoding
const text = htmlToText("<h1>Title</h1><p>Hello & welcome.</p>");
console.log(text.content);
// Title
// Hello & welcome.All functions return a ConversionResult with metadata:
const result = markdownToHtml("# Hello");
console.log(result.sourceFormat); // "markdown"
console.log(result.targetFormat); // "html"
console.log(result.sourceSize); // 7 (bytes)
console.log(result.targetSize); // 14 (bytes)What You Can Do
Markdown Conversion
Convert Markdown to HTML using a built-in pure TypeScript parser. Supports headings (h1-h6), bold, italic, inline code, fenced code blocks with language hints, links, images, unordered and ordered lists, blockquotes, horizontal rules, and paragraphs.
import { markdownToHtml } from "peasy-document";
// Full Markdown document with multiple elements
const result = markdownToHtml(`
# API Documentation
| Feature | Status |
|---------|--------|
| Auth | Done |
\`\`\`typescript
const api = new Client({ key: "abc" });
\`\`\`
- First item
- Second item
> Important note about the API
`);
console.log(result.content);
// <h1>API Documentation</h1>
// <pre><code class="language-typescript">...</code></pre>
// <ul><li>First item</li>...</ul>
// <blockquote><p>Important note about the API</p></blockquote>Learn more: Markdown to HTML Converter · Markdown vs Rich Text vs Plain Text · How to Convert Markdown to Other Formats
HTML Processing
Extract plain text from HTML documents, convert HTML to Markdown, or turn plain text into HTML paragraphs.
import { htmlToText, htmlToMarkdown, textToHtml } from "peasy-document";
// Strip all HTML tags and decode entities
const text = htmlToText(`
<html><body>
<h1>Welcome</h1>
<p>This is a <strong>formatted</strong> document with & entities.</p>
<script>alert('ignored')</script>
</body></html>
`);
console.log(text.content);
// Welcome
// This is a formatted document with & entities.
// Convert HTML back to Markdown (handles h1-h6, a, strong, em, lists, code, pre, img)
const md = htmlToMarkdown(`
<h1>Document</h1>
<p>Visit <a href="https://example.com">our site</a> for <strong>more info</strong>.</p>
<ul><li>First</li><li>Second</li></ul>
`);
console.log(md.content);
// # Document
// Visit [our site](https://example.com) for **more info**.
// - First
// - Second
// Convert plain text to HTML paragraphs
const html = textToHtml("First paragraph.\n\nSecond paragraph.\nWith a line break.");
console.log(html.content);
// <p>First paragraph.</p>
// <p>Second paragraph.<br>With a line break.</p>Learn more: HTML Entities Encoder · Plain Text vs Rich Text vs Markdown · What is MIME Sniffing?
CSV and JSON Conversion
Transform between CSV and JSON formats with proper handling of quoted fields, commas inside values, escaped quotes, and custom delimiters. Roundtrip-safe.
import { csvToJson, jsonToCsv } from "peasy-document";
// CSV to JSON array of objects
const json = csvToJson("name,role,team\nAlice,Engineer,Backend\nBob,Designer,Frontend");
console.log(json.content);
// [
// {"name": "Alice", "role": "Engineer", "team": "Backend"},
// {"name": "Bob", "role": "Designer", "team": "Frontend"}
// ]
// JSON back to CSV
const csv = jsonToCsv(json.content);
console.log(csv.content);
// name,role,team
// Alice,Engineer,Backend
// Bob,Designer,Frontend
// Tab-separated values
const tsv = csvToJson("name\tage\nAlice\t30", "\t");Learn more: CSV vs JSON vs XML Data Formats · What is TSV? · CSV Format Reference
YAML Generation
Convert JSON to YAML-like format without any external YAML library. Handles nested objects, arrays, strings, numbers, booleans, and null.
import { jsonToYaml } from "peasy-document";
const result = jsonToYaml(JSON.stringify({
server: { host: "localhost", port: 8080 },
features: ["auth", "logging"],
debug: true,
cache: null,
}));
console.log(result.content);
// server:
// host: localhost
// port: 8080
// features:
// - auth
// - logging
// debug: true
// cache: nullLearn more: YAML JSON Converter · JSON vs YAML vs TOML Configuration Formats · YAML vs JSON vs TOML Configuration
Table Formatting
Parse CSV into structured table data, or render it directly as Markdown or HTML tables.
import { csvToTable, csvToMarkdown, csvToHtml } from "peasy-document";
// Parse into structured TableData
const table = csvToTable("Name,Age,City\nAlice,30,NYC\nBob,25,LA");
console.log(table.headers); // ["Name", "Age", "City"]
console.log(table.rowCount); // 2
console.log(table.columnCount); // 3
console.log(table.rows[0]); // ["Alice", "30", "NYC"]
// Render as Markdown table with aligned columns
const md = csvToMarkdown("Name,Age,City\nAlice,30,NYC\nBob,25,LA");
console.log(md.content);
// | Name | Age | City |
// | ----- | --- | ---- |
// | Alice | 30 | NYC |
// | Bob | 25 | LA |
// Render as HTML table with thead/tbody structure
const html = csvToHtml("Name,Age\nAlice,30");
console.log(html.content);
// <table>
// <thead>
// <tr><th>Name</th><th>Age</th></tr>
// </thead>
// <tbody>
// <tr><td>Alice</td><td>30</td></tr>
// </tbody>
// </table>Learn more: Document Format Interoperability Guide · What is Metadata Stripping? · HTML Format Reference
TypeScript Types
interface ConversionResult {
content: string; // The converted content
sourceFormat: string; // e.g. "markdown", "html", "csv", "json", "text"
targetFormat: string; // e.g. "html", "text", "json", "csv", "yaml", "markdown"
sourceSize: number; // Byte size of source (UTF-8)
targetSize: number; // Byte size of output (UTF-8)
}
interface TableData {
headers: string[]; // Column headers from first row
rows: string[][]; // Data rows
rowCount: number; // Number of data rows
columnCount: number; // Number of columns
}API Reference
| Function | Input | Output | Description |
|----------|-------|--------|-------------|
| markdownToHtml(source) | Markdown | HTML | Headings, bold, italic, code, lists, links, images, blockquotes |
| htmlToText(source) | HTML | Plain text | Strip tags, decode entities, normalize whitespace |
| htmlToMarkdown(source) | HTML | Markdown | h1-h6, a, strong, em, lists, code, pre, img, blockquote |
| textToHtml(source) | Plain text | HTML | Paragraphs to <p> tags, newlines to <br> |
| csvToJson(source, delimiter?) | CSV | JSON | First row as headers, quoted field support |
| jsonToCsv(source) | JSON | CSV | All unique keys as headers, nested values stringified |
| csvToTable(source, delimiter?) | CSV | TableData | Structured headers, rows, and counts |
| csvToMarkdown(source, delimiter?) | CSV | Markdown | Pipe-separated table with alignment row |
| csvToHtml(source, delimiter?) | CSV | HTML | <table> with <thead> and <tbody> |
| jsonToYaml(source) | JSON | YAML | Objects, arrays, primitives, null handling |
REST API Client
The API client connects to the PeasyFormats developer API for tool discovery and content.
import { PeasyDocumentClient } from "peasy-document";
const client = new PeasyDocumentClient();
// List available tools
const tools = await client.listTools();
console.log(tools.results);
// Search across all content
const results = await client.search("markdown");
console.log(results);
// Browse the glossary
const glossary = await client.listGlossary({ search: "format" });
for (const term of glossary.results) {
console.log(`${term.term}: ${term.definition}`);
}
// Discover guides
const guides = await client.listGuides({ category: "document" });
for (const guide of guides.results) {
console.log(`${guide.title} (${guide.audience_level})`);
}Full API documentation at peasyformats.com/developers/.
Learn More
- Tools: Markdown to HTML · YAML JSON Converter · Format Identifier · MIME Type Lookup · Base64 Encoder · URL Encoder · HTML Entities · Line Ending Converter · Hex Dump Viewer · All Tools
- Guides: JSON vs YAML vs TOML · CSV vs JSON vs XML · Text Encoding UTF-8 ASCII · Markdown vs Rich Text vs Plain Text · How to Convert Markdown to Other Formats · Document Format Interoperability Guide · YAML vs JSON vs TOML Configuration · All Guides
- Glossary: DOCX · EPUB · SVG · TSV · ODF · MIME Sniffing · File Signature · Metadata Stripping · MessagePack · All Terms
- Formats: CSV · JSON · HTML · Markdown · YAML · XML · TOML · TXT · TSV · RTF · DOCX · All Formats
- API: REST API Docs · OpenAPI Spec
Also Available
| Language | Package | Install |
|----------|---------|---------|
| Python | peasy-document | pip install "peasy-document[all]" |
| Go | peasy-document-go | go get github.com/peasytools/peasy-document-go |
| Rust | peasy-document | cargo add peasy-document |
| Ruby | peasy-document | gem install peasy-document |
Peasy Developer Tools
Part of the Peasy Tools open-source developer ecosystem.
| Package | PyPI | npm | Description | |---------|------|-----|-------------| | peasy-pdf | PyPI | npm | PDF merge, split, rotate, compress, 21 operations — peasypdf.com | | peasy-image | PyPI | npm | Image resize, crop, convert, compress — peasyimage.com | | peasy-audio | PyPI | npm | Audio trim, merge, convert, normalize — peasyaudio.com | | peasy-video | PyPI | npm | Video trim, resize, thumbnails, GIF — peasyvideo.com | | peasy-css | PyPI | npm | CSS minify, format, analyze — peasycss.com | | peasy-compress | PyPI | npm | ZIP, TAR, gzip compression — peasytools.com | | peasy-document | PyPI | npm | Markdown, HTML, CSV, JSON conversion — peasyformats.com | | peasytext | PyPI | npm | Text case conversion, slugify, word count — peasytext.com |
License
MIT
