msmark
v0.1.0
Published
A small, dependency-free Markdown -> HTML parser focused on common markdown features and producing lightweight HTML
Downloads
5
Readme
msmark
A small, dependency-free Markdown -> HTML parser focused on common markdown features and producing lightweight HTML.
msmark is a tiny, zero-dependency Markdown parser that converts a subset of Markdown into simple, lightweight HTML. It supports headings, paragraphs, lists, code blocks (with optional highlighting), links, images (including reference-style definitions), tables, blockquotes and common inline formatting.
Features
- No dependencies, single-file implementation (
msmark.js). - Works in Node.js and the browser (exposes
msmarkonwindow). - Support for code block highlighting via a user-supplied function.
- Small and focused on common Markdown features — ideal for embedding in small projects.
Installation
Install from npm:
npm install msmarkOr include the script directly in a browser page and access the global msmark function.
Basic usage
Node / CommonJS
const msmark = require('msmark');
const md = `# Hello\n\nThis is *msmark*.`;
const html = msmark(md);
console.log(html);Browser
Include msmark.js and call the global:
<script src="msmark.js"></script>
<script>
const html = msmark('# Hello from browser');
document.body.innerHTML = html;
</script>API
msmark(markdownString, options?) -> string
Options (all optional)
hilightFunc(code: string, lang: string) => string— a function to highlight code blocks (for example using Prism or highlight.js). If provided, its return value will be used as the code block content. Otherwise code is HTML-escaped.codeBlockWrapper— a string template used to wrap code block HTML. The template can include%lang%and%code%placeholders which will be replaced with the language and HTML-escaped code respectively.imageLinkDefinitionBreak— a string used to customize the HTML comment marker that separates the main markdown body from image/link definitions. Default isDefinitions(i.e.<!-- Definitions -->).
Notes and behavior
- The package intentionally targets common Markdown constructs and aims to produce compact HTML rather than a fully spec-complete parser.
- Reference-style image and link definitions are supported when provided after the special definition break comment.
- Code fences using triple backticks or tildes are supported. If
hilightFuncis provided, it'll be used to render the code contents.
Contributing
Bug reports and pull requests are welcome. Please keep changes small and focused. The repository contains msmark.js and msmark.d.ts — keep the public API stable.
License
ISC — see the LICENSE file included with this package.
Author
Pho Thin Mg [email protected]
Examples
- Simple markdown -> output (Node)
const msmark = require('msmark');
const md = `# Sample\n\nThis is **bold** and *italic*.`;
console.log('Markdown:\n', md);
console.log('\nHTML:\n', msmark(md));Output (example):
Markdown:
# Sample
This is **bold** and *italic*.
HTML:
<h1>Sample</h1><p>This is <b>bold</b> and <em>italic</em>.</p>- Code block with highlight function
const msmark = require('msmark');
const md = '```js\nconsole.log(42)\n```';
const html = msmark(md, {
hilightFunc: (code, lang) => `<pre class="hl"><code>${code.replace(/</g, '<')}</code></pre>`
});
console.log(html);- ESM import (for ESM consumers)
If you consume this package as an ES module in a project, import the file directly:
import msmark from 'msmark'; // if your bundler maps 'msmark' to the distributed file
// or
import msmark from './msmark.js';
const html = msmark('# ESM Example');
console.log(html);Running tests locally
This repo includes a small test suite. From the repository root run:
npm testThe test script runs a CommonJS and an ESM check to validate the exported function.
