Skip to content

API Reference

Function

javascript
markdownToHtml(input, filePath?, imagePrefix?, removeTitle?)

Renders Markdown to HTML string.

Parameters

input (Required)

  • Type: string
  • Description: The Markdown text to transform
  • Default: (none - required)
javascript
const markdown = `# Hello World

\`\`\`js
console.log('Hi!')
\`\`\`

filePath (Optional)

  • Type: string
  • Default: '/README.md'
  • Description: The path to Markdown file. Used for fixing relative image links.
javascript
const html = await markdownToHtml(
  markdown,
  '/docs/guide.md'  // filePath
)

imagePrefix (Optional)

  • Type: string
  • Default: '/'
  • Description: A prefix to add to relative image paths.
javascript
const html = await markdownToHtml(
  markdown,
  '/README.md',
  '/static'  // imagePrefix
)

removeTitle (Optional)

  • Type: boolean
  • Default: false
  • Description: Whether to remove the starting h1 (if titles are rendered separately).
javascript
const html = await markdownToHtml(
  markdown,
  '/README.md',
  '/',
  true  // removeTitle
)

Return Value

Returns a Promise<string> that resolves to rendered HTML.

Example

javascript
import markdownToHtml from 'BioX-markdown'

const markdown = `# My Document

![My Image](./images/photo.jpg)

\`\`\`js
const x = 5
\`\`\`
`

const html = await markdownToHtml(
  markdown,
  '/docs/my-doc.md',
  '/static'
)

console.log(html)
// Output:
// <h1>My Document</h1>
// <p><img src="/static/images/photo.jpg" /></p>
// <pre><code class="language-js">const x = 5</code></pre>

Usage Notes

Always Use await

BioX-markdown uses async operations internally. Always await the result:

javascript
// ❌ Incorrect
const html = markdownToHtml(markdown)

// ✅ Correct
const html = await markdownToHtml(markdown)

Image Path Handling

Relative image paths in Markdown are automatically prefixed based on filePath and imagePrefix:

javascript
// Markdown: ![photo](./images/photo.jpg)
// filePath: '/docs/guide.md'
// imagePrefix: '/assets'
// Result: <img src="/assets/images/photo.jpg" />

Absolute URLs are never modified.

Title Removal

If you want to render title separately from content:

javascript
const html = await markdownToHtml(
  markdown,
  '/README.md',
  '/',
  true  // Remove starting h1
)

// Render title separately
const title = extractTitle(markdown)

Frontmatter Support

BioX-markdown doesn't include frontmatter parsing by default. For frontmatter support, we recommend using gray-matter:

javascript
import matter from 'gray-matter'
import markdownToHtml from 'BioX-markdown'

const { data: frontmatter, content } = matter(markdown)

const html = await markdownToHtml(content)

console.log(frontmatter.title)
console.log(html)

Common Patterns

Static Site Generation

javascript
const html = await markdownToHtml(
  markdown,
  '/blog/my-post.md',
  '/static'
)

Documentation Sites

javascript
const html = await markdownToHtml(
  markdown,
  '/docs/guide.md',
  '/docs/assets'
)

Title-Only Layouts

javascript
const html = await markdownToHtml(
  markdown,
  '/README.md',
  '/',
  true  // Remove h1
)

// Render title separately
<h1>{extractTitle(markdown)}</h1>
{html}

Next Steps

Released under MIT License