@siping/html-to-markdown-node
v1.0.1
Published
A robust HTML to Markdown converter with plugin support
Maintainers
Readme
html-to-markdown-node
A robust HTML to Markdown converter for Node.js with plugin support, inspired by html-to-markdown.
Features
- Bold & Italic: Supports bold and italic formatting
- Lists: Handles ordered and unordered lists with nesting
- Blockquotes: Supports nested blockquotes
- Code: Inline code and code blocks
- Links & Images: Properly formats links and images
- Smart Escaping: Escapes special characters only when necessary
- Plugin System: Easily extend with custom plugins
- TypeScript: Full TypeScript support with type definitions
Installation
npm install html-to-markdown-nodeUsage
Basic Usage
import { convertString } from 'html-to-markdown-node';
const html = '<strong>Bold Text</strong>';
const markdown = convertString(html);
console.log(markdown); // **Bold Text**With Options
import { convertString } from 'html-to-markdown-node';
const html = '<img src="/assets/image.png" />';
const markdown = convertString(html, {
domain: 'https://example.com'
});
console.log(markdown); // Advanced Usage with Plugins
import { Converter } from 'html-to-markdown-node';
import { BasePlugin } from 'html-to-markdown-node/plugins/base';
import { CommonMarkPlugin } from 'html-to-markdown-node/plugins/commonmark';
const converter = new Converter({
plugins: [
new BasePlugin(),
new CommonMarkPlugin({
strongDelimiter: '__'
})
]
});
const markdown = converter.convertString('<strong>Bold Text</strong>');
console.log(markdown); // __Bold Text__API
convertString(html: string, options?: ConvertOptions): string
Converts an HTML string to Markdown.
Converter
The main converter class with full plugin support.
Options
domain?: string- Base domain for converting relative URLs to absoluteescapeMode?: 'smart' | 'disabled'- Escape mode (default: 'smart')plugins?: Plugin[]- Array of plugins to use
Plugins
Built-in Plugins
- BasePlugin: Basic functionality (removing nodes, etc.)
- CommonMarkPlugin: CommonMark specification implementation
Creating Custom Plugins
import { Plugin, Converter } from 'html-to-markdown-node';
class MyPlugin implements Plugin {
name = 'my-plugin';
init(converter: Converter): void {
converter.register.renderer('custom-tag', (ctx, node) => {
return '**custom**';
}, 100);
}
}License
MIT
