- Removes the remark / unified complexity and easy to use.
- Powered by the unified processor for an extensible plugin pipeline.
- Built in caching 💥 making it render very fast when there isn't a change
- Frontmatter support built in by default. 🎉
- Easily Render to
ReactorHTML. - Generates a Table of Contents for your markdown files (remark-toc).
- Slug generation for your markdown files (rehype-slug).
- Code Highlighting (rehype-highlight).
- Math Support (rehype-katex).
- Markdown to HTML (rehype-stringify).
- Github Flavor Markdown (remark-gfm).
- Emoji Support (remark-emoji).
- MDX Support (remark-mdx).
- Built in Hooks for adding code to render pipeline.
Writr builds on top of the open source unified processor – the core project that powers
remark, rehype, and many other content tools. Unified
provides a pluggable pipeline where each plugin transforms a syntax tree. Writr configures a default set of plugins to turn
Markdown into HTML, but you can access the processor through the .engine property to add your own behavior with
writr.engine.use(myPlugin). The unified documentation has more details and guides for building
plugins and working with the processor directly.
- Unified Processor Engine
- Getting Started
- API
new Writr(arg?: string | WritrOptions, options?: WritrOptions).content.body.options.frontmatter.frontMatterRaw.cache.engine.render(options?: RenderOptions).renderSync(options?: RenderOptions).renderToFile(filePath: string, options?).renderToFileSync(filePath: string, options?).renderReact(options?: RenderOptions, reactOptions?: HTMLReactParserOptions).renderReactSync( options?: RenderOptions, reactOptions?: HTMLReactParserOptions).validate(content?: string, options?: RenderOptions).validateSync(content?: string, options?: RenderOptions).loadFromFile(filePath: string).loadFromFileSync(filePath: string).saveToFile(filePath: string).saveToFileSync(filePath: string)
- Caching On Render
- GitHub Flavored Markdown (GFM)
- Hooks
- Emitters
- ESM and Node Version Support
- Code of Conduct and Contributing
- License
> npm install writrThen you can use it like this:
import { Writr } from 'writr';
const writr = new Writr(`# Hello World ::-):\n\n This is a test.`);
const html = await writr.render(); // <h1>Hello World 🙂</h1><p>This is a test.</p>Its just that simple. Want to add some options? No problem.
import { Writr } from 'writr';
const writr = new Writr(`# Hello World ::-):\n\n This is a test.`);
const options = {
emoji: false
}
const html = await writr.render(options); // <h1>Hello World ::-):</h1><p>This is a test.</p>An example passing in the options also via the constructor:
import { Writr, WritrOptions } from 'writr';
const writrOptions = {
throwErrors: true,
renderOptions: {
emoji: true,
toc: true,
slug: true,
highlight: true,
gfm: true,
math: true,
mdx: true,
caching: true,
}
};
const writr = new Writr(`# Hello World ::-):\n\n This is a test.`, writrOptions);
const html = await writr.render(options); // <h1>Hello World ::-):</h1><p>This is a test.</p>By default the constructor takes in a markdown string or WritrOptions in the first parameter. You can also send in nothing and set the markdown via .content property. If you want to pass in your markdown and options you can easily do this with new Writr('## Your Markdown Here', { ...options here}). You can access the WritrOptions from the instance of Writr. Here is an example of WritrOptions.
import { Writr, WritrOptions } from 'writr';
const writrOptions = {
throwErrors: true,
renderOptions: {
emoji: true,
toc: true,
slug: true,
highlight: true,
gfm: true,
math: true,
mdx: true,
caching: true,
}
};
const writr = new Writr(writrOptions);Setting the markdown content for the instance of Writr. This can be set via the constructor or directly on the instance and can even handle frontmatter.
import { Writr } from 'writr';
const writr = new Writr();
writr.content = `---
title: Hello World
---
# Hello World ::-):\n\n This is a test.`;gets the body of the markdown content. This is the content without the frontmatter.
import { Writr } from 'writr';
const writr = new Writr();
writr.content = `---
title: Hello World
---
# Hello World ::-):\n\n This is a test.`;
console.log(writr.body); // '# Hello World ::-):\n\n This is a test.'Accessing the default options for this instance of Writr. Here is the default settings for WritrOptions. These are the default settings for the WritrOptions:
{
throwErrors: false,
renderOptions: {
emoji: true,
toc: true,
slug: true,
highlight: true,
gfm: true,
math: true,
mdx: true,
caching: false,
}
}Accessing the frontmatter for this instance of Writr. This is a Record<string, any> and can be set via the .content property.
import { Writr } from 'writr';
const writr = new Writr();
writr.content = `---
title: Hello World
---
# Hello World ::-):\n\n This is a test.`;
console.log(writr.frontmatter); // { title: 'Hello World' }you can also set the front matter directly like this:
import { Writr } from 'writr';
const writr = new Writr();
writr.frontmatter = { title: 'Hello World' };Accessing the raw frontmatter for this instance of Writr. This is a string and can be set via the .content property.
import { Writr } from 'writr';
const writr = new Writr();
writr.content = `---
title: Hello World
---
# Hello World ::-):\n\n This is a test.`;
console.log(writr.frontMatterRaw); // '---\ntitle: Hello World\n---'Accessing the cache for this instance of Writr. By default this is an in memory cache and is disabled (set to false) by default. You can enable this by setting caching: true in the RenderOptions of the WritrOptions or when calling render passing the RenderOptions like here:
import { Writr } from 'writr';
const writr = new Writr(`# Hello World ::-):\n\n This is a test.`);
const options = {
caching: true
}
const html = await writr.render(options); // <h1>Hello World ::-):</h1><p>This is a test.</p>Accessing the underlying engine for this instance of Writr. This is a Processor<Root, Root, Root, undefined, undefined> from the core unified project and uses the familiar .use() plugin pattern. You can chain additional unified plugins on this processor to customize the render pipeline. Learn more about the unified engine at unifiedjs.com and check out the getting started guide for examples.
Rendering markdown to HTML. the options are based on RenderOptions. Which you can access from the Writr instance.
import { Writr } from 'writr';
const writr = new Writr(`# Hello World ::-):\n\n This is a test.`);
const html = await writr.render(); // <h1>Hello World 🙂</h1><p>This is a test.</p>
//passing in with render options
const options = {
emoji: false
}
const html = await writr.render(options); // <h1>Hello World ::-):</h1><p>This is a test.</p>Rendering markdown to HTML synchronously. the options are based on RenderOptions. Which you can access from the Writr instance. The parameters are the same as the .render() function.
import { Writr } from 'writr';
const writr = new Writr(`# Hello World ::-):\n\n This is a test.`);
const html = writr.renderSync(); // <h1>Hello World 🙂</h1><p>This is a test.</p>Rendering markdown to a file. The options are based on RenderOptions.
import { Writr } from 'writr';
const writr = new Writr(`# Hello World ::-):\n\n This is a test.`);
await writr.renderToFile('path/to/file.html');Rendering markdown to a file synchronously. The options are based on RenderOptions.
import { Writr } from 'writr';
const writr = new Writr(`# Hello World ::-):\n\n This is a test.`);
writr.renderToFileSync('path/to/file.html');Rendering markdown to React. The options are based on RenderOptions and now HTMLReactParserOptions from html-react-parser.
import { Writr } from 'writr';
const writr = new Writr(`# Hello World ::-):\n\n This is a test.`);
const reactElement = await writr.renderReact(); // Will return a React.JSX.ElementRendering markdown to React. The options are based on RenderOptions and now HTMLReactParserOptions from html-react-parser.
import { Writr } from 'writr';
const writr = new Writr(`# Hello World ::-):\n\n This is a test.`);
const reactElement = writr.renderReactSync(); // Will return a React.JSX.ElementValidate markdown content by attempting to render it. Returns a WritrValidateResult object with a valid boolean and optional error property. Note that this will disable caching on render to ensure accurate validation.
import { Writr } from 'writr';
const writr = new Writr(`# Hello World\n\nThis is a test.`);
// Validate current content
const result = await writr.validate();
console.log(result.valid); // true
// Validate external content without changing the instance
const externalResult = await writr.validate('## Different Content');
console.log(externalResult.valid); // true
console.log(writr.content); // Still "# Hello World\n\nThis is a test."
// Handle validation errors
const invalidWritr = new Writr('Put invalid markdown here');
const errorResult = await invalidWritr.validate();
console.log(errorResult.valid); // false
console.log(errorResult.error?.message); // "Failed to render markdown: Invalid plugin"Synchronously validate markdown content by attempting to render it. Returns a WritrValidateResult object with a valid boolean and optional error property.
This is the synchronous version of .validate() with the same parameters and behavior.
import { Writr } from 'writr';
const writr = new Writr(`# Hello World\n\nThis is a test.`);
// Validate current content synchronously
const result = writr.validateSync();
console.log(result.valid); // true
// Validate external content without changing the instance
const externalResult = writr.validateSync('## Different Content');
console.log(externalResult.valid); // true
console.log(writr.content); // Still "# Hello World\n\nThis is a test."Load your markdown content from a file path.
import { Writr } from 'writr';
const writr = new Writr();
await writr.loadFromFile('path/to/file.md');Load your markdown content from a file path synchronously.
import { Writr } from 'writr';
const writr = new Writr();
writr.loadFromFileSync('path/to/file.md');Save your markdown and frontmatter (if included) content to a file path.
import { Writr } from 'writr';
const writr = new Writr(`# Hello World ::-):\n\n This is a test.`);
await writr.saveToFile('path/to/file.md');Save your markdown and frontmatter (if included) content to a file path synchronously.
import { Writr } from 'writr';
const writr = new Writr(`# Hello World ::-):\n\n This is a test.`);
writr.saveToFileSync('path/to/file.md');Caching is built into Writr and is an in-memory cache using CacheableMemory from Cacheable. It is turned off by default and can be enabled by setting caching: true in the RenderOptions of the WritrOptions or when calling render passing the RenderOptions like here:
import { Writr } from 'writr';
const writr = new Writr(`# Hello World ::-):\n\n This is a test.`, { renderOptions: { caching: true } });or via RenderOptions such as:
import { Writr } from 'writr';
const writr = new Writr(`# Hello World ::-):\n\n This is a test.`);
await writr.render({ caching: true});If you want to set the caching options for the instance of Writr you can do so like this:
// we will set the lruSize of the cache and the default ttl
import {Writr} from 'writr';
const writr = new Writr(`# Hello World ::-):\n\n This is a test.`, { renderOptions: { caching: true } });
writr.cache.store.lruSize = 100;
writr.cache.store.ttl = '5m'; // setting it to 5 minutesWritr includes full support for GitHub Flavored Markdown (GFM) through the remark-gfm and remark-github-blockquote-alert plugins. GFM is enabled by default and adds several powerful features to standard Markdown.
When GFM is enabled (which it is by default), you get access to the following features:
Create tables using pipes and hyphens:
| Feature | Supported |
|---------|-----------|
| Tables | Yes |
| Alerts | Yes |Use ~~ to create strikethrough text:
~~This text is crossed out~~Create interactive checkboxes:
- [x] Completed task
- [ ] Incomplete task
- [ ] Another taskURLs are automatically converted to clickable links:
https://github.comGitHub-style alerts are supported to emphasize critical information. These are blockquote-based admonitions that render with special styling:
> [!NOTE]
> Useful information that users should know, even when skimming content.
> [!TIP]
> Helpful advice for doing things better or more easily.
> [!IMPORTANT]
> Key information users need to know to achieve their goal.
> [!WARNING]
> Urgent info that needs immediate user attention to avoid problems.
> [!CAUTION]
> Advises about risks or negative outcomes of certain actions.GFM is enabled by default. Here's an example:
import { Writr } from 'writr';
const markdown = `
# Task List Example
- [x] Learn Writr basics
- [ ] Master GFM features
> [!NOTE]
> GitHub Flavored Markdown is enabled by default!
| Feature | Status |
|---------|--------|
| GFM | ✓ |
`;
const writr = new Writr(markdown);
const html = await writr.render(); // Renders with full GFM supportIf you need to disable GFM features, you can set gfm: false in the render options:
import { Writr } from 'writr';
const writr = new Writr('~~strikethrough~~ text');
// Disable GFM
const html = await writr.render({ gfm: false });
// Output: <p>~~strikethrough~~ text</p>
// With GFM enabled (default)
const htmlWithGfm = await writr.render({ gfm: true });
// Output: <p><del>strikethrough</del> text</p>Note: When GFM is disabled, GitHub blockquote alerts will not be processed and will render as regular blockquotes.
Hooks are a way to add additional parsing to the render pipeline. You can add hooks to the the Writr instance. Here is an example of adding a hook to the instance of Writr:
import { Writr, WritrHooks } from 'writr';
const writr = new Writr(`# Hello World ::-):\n\n This is a test.`);
writr.onHook(WritrHooks.beforeRender, data => {
data.body = 'Hello, Universe!';
});
const result = await writr.render();
console.log(result); // Hello, Universe!For beforeRender the data object is a renderData object. Here is the interface for renderData:
export type renderData = {
body: string
options: RenderOptions;
}For afterRender the data object is a resultData object. Here is the interface for resultData:
export type resultData = {
result: string;
}For saveToFile the data object is an object with the filePath and content. Here is the interface for saveToFileData:
export type saveToFileData = {
filePath: string;
content: string;
}This is called when you call saveToFile, saveToFileSync.
For renderToFile the data object is an object with the filePath and content. Here is the interface for renderToFileData:
export type renderToFileData = {
filePath: string;
content: string;
}This is called when you call renderToFile, renderToFileSync.
For loadFromFile the data object is an object with content so you can change before it is set on writr.content. Here is the interface for loadFromFileData:
export type loadFromFileData = {
content: string;
}This is called when you call loadFromFile, loadFromFileSync.
Writr extends the Hookified class, which provides event emitter capabilities. This means you can listen to events emitted by Writr during its lifecycle, particularly error events.
Writr emits an error event whenever an error occurs in any of its methods. This provides a centralized way to handle errors without wrapping every method call in a try/catch block.
You can listen to error events using the .on() method:
import { Writr } from 'writr';
const writr = new Writr('# Hello World');
// Listen for any errors
writr.on('error', (error) => {
console.error('An error occurred:', error.message);
// Handle the error appropriately
// Log to error tracking service, display to user, etc.
});
// Now when any error occurs, your listener will be notified
try {
await writr.render();
} catch (error) {
// Error is also thrown, so you can handle it here too
}The following methods emit error events when they fail:
Rendering Methods:
render()- Emits error before throwing when markdown rendering failsrenderSync()- Emits error before throwing when markdown rendering failsrenderReact()- Emits error before throwing when React rendering failsrenderReactSync()- Emits error before throwing when React rendering fails
Validation Methods:
validate()- Emits error when validation fails (returns error in result object)validateSync()- Emits error when validation fails (returns error in result object)
File Operations:
renderToFile()- Emits error when file writing fails (does not throw ifthrowErrors: false)renderToFileSync()- Emits error when file writing fails (does not throw ifthrowErrors: false)loadFromFile()- Emits error when file reading fails (does not throw ifthrowErrors: false)loadFromFileSync()- Emits error when file reading fails (does not throw ifthrowErrors: false)saveToFile()- Emits error when file writing fails (does not throw ifthrowErrors: false)saveToFileSync()- Emits error when file writing fails (does not throw ifthrowErrors: false)
Front Matter Operations:
frontMattergetter - Emits error when YAML parsing failsfrontMattersetter - Emits error when YAML serialization fails
Example 1: Global Error Handler
import { Writr } from 'writr';
const writr = new Writr();
// Set up a global error handler
writr.on('error', (error) => {
// Log to your monitoring service
console.error('Writr error:', error);
// Send to error tracking (e.g., Sentry, Rollbar)
// errorTracker.captureException(error);
});
// All errors will be emitted to the listener above
await writr.loadFromFile('./content.md');
const html = await writr.render();Example 2: Validation with Error Listening
import { Writr } from 'writr';
const writr = new Writr('# My Content');
let lastError = null;
writr.on('error', (error) => {
lastError = error;
});
const result = await writr.validate();
if (!result.valid) {
console.log('Validation failed');
console.log('Error details:', lastError);
// result.error is also available
}Example 3: File Operations Without Try/Catch
import { Writr } from 'writr';
const writr = new Writr('# Content', { throwErrors: false });
writr.on('error', (error) => {
console.error('File operation failed:', error.message);
// Handle gracefully - maybe use default content
});
// Won't throw, but will emit error event if file doesn't exist
await writr.loadFromFile('./maybe-missing.md');Since Writr extends Hookified, you have access to standard event emitter methods:
writr.on(event, handler)- Add an event listenerwritr.once(event, handler)- Add a one-time event listenerwritr.off(event, handler)- Remove an event listenerwritr.emit(event, data)- Emit an event (used internally)
For more information about event handling capabilities, see the Hookified documentation.
This package is ESM only and tested on the current lts version and its previous. Please don't open issues for questions regarding CommonJS / ESM or previous Nodejs versions.
Please use our Code of Conduct and Contributing guidelines for development and testing. We appreciate your contributions!
MIT & © Jared Wray