Skip to content

kekyo/mark-deco

Repository files navigation

MarkDeco

A high-performance Markdown to HTML conversion library written in TypeScript.

License: MIT CI

Package npm
mark-deco npm version
mark-deco-cli npm version

(日本語はこちら)

What is this?

A high-performance Markdown to HTML conversion library written in TypeScript. It interprets GitHub Flavored Markdown (GFM) and outputs HTML. Supports frontmatter parsing, heading analysis, source code formatting, oEmbed/card/Mermaid graph rendering, and custom code block processing through plugin extensions.

  • Can be used to render HTML from Markdown input.
  • Simple interface makes it very easy to use.
  • Highly independent with minimal runtime requirements. Works in both Node.js and browser environments.
  • Built-in plugins support oEmbed, cards, and Mermaid.js.

Installation

npm install mark-deco

Basic Usage

Here's the simplest usage example:

import { createMarkdownProcessor, createCachedFetcher } from 'mark-deco';

// Create a memory-cached fetcher
const fetcher = createCachedFetcher('MyApp/1.0');

// Create MarkDeco processor
const processor = createMarkdownProcessor({
  fetcher
});

// Markdown to convert
const markdown = `---
title: Sample Article
author: John Doe
---

# Hello World

This is a test article.`;

// Render HTML from Markdown input
const result = await processor.process(
  markdown,
  "id");     // ID prefix for HTML elements (described later)

// Generated HTML
console.log(result.html);
// Extracted frontmatter information (described later)
console.log(result.frontmatter);
// Extracted heading information (described later)
console.log(result.headingTree);

This will render HTML like this:

<h1 id="id-1">Hello World</h1>
<p>This is a test article.</p>

A "fetcher" is an abstraction for external server access. It's primarily used by oEmbed and card plugins for external API calls and page scraping. See details below. The argument passed to the fetcher is a user agent string, which is applied to HTTP request headers when accessing external servers.

HTML converted by the MarkDeco processor is formatted in a readable manner. Advanced options allow fine-tuning of formatting conditions (described later).

Aborting Processor Operations

While the MarkDeco processor engine itself doesn't access external servers, plugins may access external servers as needed (e.g., when using oEmbed APIs or performing page scraping).

To enable operation cancellation in such cases, pass an ECMAScript standard AbortSignal instance to notify cancellation signals:

// Abort controller
const abortController = new AbortController();

// ...

// Convert Markdown to HTML
const result = await processor.process(
  markdown, "id",
  { // Specify processor options
    signal: abortController.signal,   // Cancellation support
  });

For usage of AbortController and AbortSignal, refer to ECMAScript documentation.

CLI Interface

Although MarkDeco is a library, a CLI interface is also available in the package that allows you to easily try out MarkDeco. This allows you to try out conversions without having to write code in TypeScript, or call it as an independent application from another code.

# Take Markdown from standard input and output HTML 
echo "# Hello World" | mark-deco-cli 

For more information, see CLI documentation.


Documentation

MarkDeco has many useful features. For further details, please see below.


License

Under MIT.

Changelog

  • 0.1.0:
    • First public release.

About

A high-performance Markdown to HTML conversion library written in TypeScript.

Topics

Resources

Stars

Watchers

Forks