Skip to content

gossi/markdown-it-compiler

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 

markdown-it-compiler

Easy configurable markdown-it compiler

Installation

yarn add markdown-it-compiler

Usage

Instantiate an instance of the compiler and sequentially compile markdown to html.

const config = {...};
const compiler = new MarkdownItCompiler(config);
const doc = compiler.compile(mardownString);
const content = doc.html;

The config as follows:

  • preset - markdown-it preset name (default: 'default')
  • options - options for markdown-it, see docs
  • plugins - array of plugins to use() (see below)
  • configure - detailed configuration of markdown-it. Type: (md: MarkdownIt) => void
  • format - postprocess the html. Type: (content: Content) => string (see below)

Example:

const config = {
  options: {
    linkify: true,
    html: true,
    typographer: true
  },
  plugins: [
    'markdown-it-abbr',
    'markdown-it-anchor',
    'markdown-it-deflist',
    'markdown-it-highlightjs',
    'markdown-it-ins',
    'markdown-it-mark',
    [
      'markdown-it-plantuml',
      {
        openMarker: '```plantuml\n@startuml',
        closeMarker: '@enduml\n```'
      }
    ],
    'markdown-it-sub',
    'markdown-it-sup'
  ],
  configure(md) {
    // load custom plugins
    md.use(require('./lib/my-md-it-plugin'));
  },
  format(content) {
    if (content.attributes.layout && content.attributes.layout === 'article') {
      return `<article>${content.html}</article>`;
    }

    return content.html;
  }
};

Post-Processing

Use the format key for postprocessing. The passed content has the following structure:

  • attributes - attributes from frontmatter
  • toc - structured table of contents, see markdown-toc
  • body - the original markdown body
  • html - the already generated html

About

Easy configurable markdown-it compiler

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published