md
Use md to turn Markdown into HTML (and back with md.fromHTML). It is built for serving pages with serve and composing markup with template.
import "std:md"Example
import "std:md"
log md.toHTML("# Hello\n\n**world**")
// <h1 id="hello">Hello</h1>\n<p><strong>world</strong></p>\nWith serve
Pass the HTML string straight to ctx.html when you want a full response body:
import "std:md"
import "std:serve"
*serve.Router app = serve.new()
app.GET("/", def(ctx) -> (
string body = md.toHTML("# Home\n\nWelcome.")
ctx.html(200, body)
))
app.run(":8080")Or wrap it in a small page layout first:
With template
template.renderHTML escapes interpolated values by default. Use {{& name}} when the value is already HTML from md.toHTML:
Using {{body}} (without &) would escape the tags and show the HTML source.
API reference
md.toHTML(source) → string
Renders Markdown source to HTML. Enables GitHub Flavored Markdown (tables, strikethrough, autolinks, task lists) and auto heading IDs. Raw HTML tags in the source are not passed through (safe default for untrusted content).
md.toHTMLUnsafe(source) → string
Same as md.toHTML, but raw HTML in the Markdown is left as-is. It shares the safe engine's GFM and heading options; only raw-HTML rendering differs. Only use this with trusted input.
md.fromHTML(source) → string
The inverse of md.toHTML: converts an HTML source string into Markdown. Headings, emphasis, links, lists, and other common elements map to their Markdown equivalents; unconvertible input yields an empty string.
Notes
Prefer
import "std:md"; the olderimport "osl/md"spelling remains supported.Rendering is powered by goldmark.
Return values are ordinary OSL strings. Pass them to
ctx.html,ctx.send, or template slots as needed.
md.sanitize(source) → string
Sanitizes Markdown by rendering it through md.toHTML and converting that safe HTML back to Markdown. Repeated sanitization is idempotent.
Last updated