| name | md-pen |
| description | Usage patterns and API reference for md-pen. Use when formatting markdown strings programmatically. |
md-pen
Utilities for formatting Markdown strings. GFM-first, general-purpose.
Import
import md from 'md-pen'
md.bold('text')
import { bold, table, link } from 'md-pen'
API Reference
Inline Formatting
| Function | Example | Output |
|---|
code(text) | code('hello') | `hello` |
bold(text) | bold('important') | __important__ |
italic(text) | italic('emphasis') | *emphasis* |
strikethrough(text) | strikethrough('no') | ~~no~~ |
bold(italic(text)) | bold(italic('wow')) | __*wow*__ |
Links and Images
[!NOTE]
link(url, text) and image(url, alt) take URL first — matches HTML's <a href="url">text</a>, not Markdown's [text](url).
link('https://x.com')
link('https://x.com', 'click')
link('https://x.com', 'click', { title: 'T' })
link('https://x.com', 'click', { target: '_blank' })
image('cat.png', 'A cat')
image('cat.png', 'A cat', { width: 200 })
Block Elements
heading('Title', 2)
h1('Title')
blockquote('text')
codeBlock('const x = 1', 'ts')
hr()
Tables
Cells can be strings, numbers, or booleans.
table([
['Name', 'Age'],
['Alice', 30],
], { align: ['left', 'right'] })
table([{ name: 'Alice', age: 30 }, { name: 'Bob', age: 25 }])
table([{ firstName: 'Alice', age: 30 }], { columns: [['firstName', 'Name'], 'age'] })
table([['Before', 'After'], [codeBlock('old', 'js'), codeBlock('new', 'js')]], { html: true })
Alignment: 'left', 'center', 'right', 'none'. Ragged rows auto-padded. Pipes in cells escaped.
Lists
ul(['a', 'b', ['nested'], 'c'])
ol(['first', 'second', 'third'])
taskList([[true, 'Done'], [false, 'Todo']])
GFM Extras
alert('warning', 'Be careful')
details('Summary', 'Content')
details('S', 'C', { open: '' })
footnoteRef('1')
footnote('1', 'Source text')
[!TIP]
On GitHub, details() renders the content flush against the summary. For extra breathing room, prepend <br> to the content:
details('Summary', '<br>\n\n' + ul(['one', 'two']))
Niche
kbd('Ctrl')
kbd('Enter', { title: 'Confirm' })
sub('2')
sup('n')
sub('2', { title: 'subscript' })
math('E = mc^2')
mathBlock('\\sum')
mermaid('graph TD')
mention('octocat')
emoji('rocket')
Generic HTML Element
el('br')
el('img', { src: 'cat.png', alt: 'Cat' })
el('p', { align: 'center' }, 'centered')
Content is raw (not escaped). Without content, self-closing.
Escaping
escape('**not bold**')
Use escape() for untrusted user input. Bold uses __ and italic uses * — different delimiters that compose without collision:
bold(link('https://x.com', 'text'))