Documentation
¶
Overview ¶
Package benmark renders Markdown to safe, styled HTML.
It is a batteries-included distribution built on goldmark: CommonMark + GitHub-Flavored Markdown (tables, task lists, strikethrough, autolinks), plus syntax highlighting, Mermaid diagrams, media embeds (video / audio / PDF / YouTube / Vimeo), heading anchors, wikilinks, and front matter — all sanitized by default so the output is safe to assign to innerHTML.
The zero-config path:
html, err := benmark.RenderString("# Hello\n\nSome **markdown**.")
For repeated rendering, build a Renderer once and reuse it (it is safe for concurrent use):
r := benmark.New(benmark.WithHighlightTheme("github"))
html, err := r.Render(src)
Diagrams and math-like rich content are emitted as hydration hooks (e.g. <pre class="mermaid">) and rendered on the client; pair the output with CSS() and HydrateJS() to get styling and client-side rendering.
Index ¶
- Variables
- func CSS() string
- func CSSForStyle(style string) string
- func HydrateJS() string
- func Render(src []byte, opts ...Option) (template.HTML, error)
- func RenderString(src string, opts ...Option) (string, error)
- type Document
- type Option
- func WithEmbedHosts(hosts ...string) Option
- func WithEmoji(on bool) Option
- func WithFrontmatter(on bool) Option
- func WithHeadingAnchors(on bool) Option
- func WithHighlightTheme(style string) Option
- func WithInline(on bool) Option
- func WithMediaEmbeds(on bool) Option
- func WithMermaid(on bool) Option
- func WithSanitize(on bool) Option
- func WithWikilinks(on bool) Option
- type Options
- type Renderer
Constants ¶
This section is empty.
Variables ¶
var DefaultEmbedHosts = []string{
"youtube.com", "www.youtube.com", "youtube-nocookie.com",
"youtu.be", "vimeo.com", "player.vimeo.com",
}
DefaultEmbedHosts is the iframe host allowlist used when Options.EmbedHosts is empty. Only these hosts are turned into <iframe> embeds; every other host is left as an ordinary link.
Functions ¶
func CSS ¶
func CSS() string
CSS returns a self-contained stylesheet for benmark output, including the syntax-highlighting rules for the default "github" style. Wrap rendered HTML in an element with class="benmark".
func CSSForStyle ¶
CSSForStyle is CSS with syntax-highlighting rules for a specific chroma style (e.g. "monokai", "dracula"). Unknown names fall back to a default.
func HydrateJS ¶
func HydrateJS() string
HydrateJS returns the client hydration script (an ES module exporting `hydrate`) that renders Mermaid diagrams in benmark output. Serve it and call hydrate() after inserting rendered HTML into the DOM.
Types ¶
type Document ¶
Document is the result of RenderDoc: the rendered HTML plus any front matter metadata parsed from the source.
type Option ¶
type Option func(*Options)
Option mutates Options. Passed to New / Render / RenderString.
func WithEmbedHosts ¶
WithEmbedHosts overrides the iframe host allowlist for MediaEmbeds.
func WithFrontmatter ¶
WithFrontmatter toggles front matter parsing/stripping.
func WithHeadingAnchors ¶
WithHeadingAnchors toggles heading ids + anchors.
func WithHighlightTheme ¶
WithHighlightTheme sets the chroma style (empty disables highlighting).
func WithInline ¶
WithInline enables the single-line "chat" rendering subset.
func WithMediaEmbeds ¶
WithMediaEmbeds toggles video/audio/PDF/YouTube/Vimeo embeds.
func WithSanitize ¶
WithSanitize toggles output sanitization. Only turn it OFF for trusted, first-party Markdown you control end to end.
type Options ¶
type Options struct {
// Sanitize runs the rendered HTML through the bluemonday allowlist.
// Keep this ON (the default) whenever the output is untrusted — it is
// the security boundary that makes the result safe for innerHTML.
Sanitize bool
// Inline renders the "chat subset": newlines become <br> and a single
// wrapping <p> is stripped, so a one-line message stays inline instead
// of becoming a block paragraph.
Inline bool
// HighlightTheme is the chroma style used for fenced code blocks
// (e.g. "github", "monokai"). Empty disables syntax highlighting.
// Highlighted spans are class-based; ship CSS() so they are styled.
HighlightTheme string
// HeadingAnchors adds an id to every heading and a linkable anchor.
HeadingAnchors bool
// Mermaid turns “`mermaid fences into <pre class="mermaid"> hydration
// hooks (rendered client-side by HydrateJS + mermaid.js).
Mermaid bool
// MediaEmbeds turns image tags and bare URLs that point at media into
// <video>/<audio>/<iframe> embeds. See embeds.go for the rules.
MediaEmbeds bool
// Wikilinks enables [[Page Name]] internal links.
Wikilinks bool
// Emoji enables :shortcode: emoji (e.g. :tada: → 🎉).
Emoji bool
// Frontmatter strips a leading YAML/TOML front matter block from the
// body and (via RenderDoc) exposes it as metadata.
Frontmatter bool
// EmbedHosts is the allowlist of iframe hosts for MediaEmbeds. Empty
// uses DefaultEmbedHosts (YouTube + Vimeo).
EmbedHosts []string
}
Options controls what benmark renders and how. Use the With* Option helpers with New/Render rather than constructing this directly, so future fields keep sensible defaults.
func DefaultOptions ¶
func DefaultOptions() Options
DefaultOptions is the batteries-included configuration: full document rendering, sanitized, with every extension enabled.
type Renderer ¶
type Renderer struct {
// contains filtered or unexported fields
}
Renderer is a reusable, concurrency-safe Markdown renderer. Build one with New and call Render/RenderString/RenderDoc as many times as you like.