format

package module
v0.0.0-...-2c50f0b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 5, 2026 License: LGPL-3.0 Imports: 10 Imported by: 0

README

format

Go Reference

Markdown to HTML, via goldmark, tuned for Matrix-adjacent surfaces. One function, Render, sane defaults, no ceremony. Inspired by mautrix-go's format package.

It's a separate module (and its Go floor is old on purpose)

format has its own go.mod, because it depends on goldmark and the root go-kit module refuses to carry dependencies. So you install it on its own line:

go get github.com/etkecc/go-kit/format

Here's the deliberate part: its Go floor is 1.22, lower than the root's 1.26. That's not neglect, it's a lifeline. Some service is stuck on an old Go version and still needs to render Markdown, and pinning the floor low means it can. Don't "helpfully" bump format/go.mod up to match the parent without checking who's still down there, or you'll strand exactly the caller this floor exists for.

import "github.com/etkecc/go-kit/format"

html := format.Render("Ship **it** or ~~ship~~ it.")
// "Ship <strong>it</strong> or <del>ship</del> it."

What Render does

  • Single paragraph in, no wrapper out. A one-paragraph input comes back without the outer <p>...</p>, so you can drop it inline. Multi-paragraph input keeps its <p> tags. Empty input gives empty output, no stray tags.
  • Every link opens in a new tab. All <a> tags get target="_blank" via an AST transformer, which is what you want for user content pasted into a chat.
  • Extensions: strikethrough and tables.
  • Raw HTML passes through (goldmark's Unsafe mode) and hard line breaks are honored. That "unsafe" is a real choice: this renders content you already trust or sanitize elsewhere, not arbitrary attacker input straight to a browser. If the source is hostile, sanitize the output before you serve it.

That's the whole package. godoc has the exported goldmark handles if you want to build on them.

License

GNU LGPL-3.0. See ../LICENSE.

Documentation

Overview

Package format converts Markdown to HTML using Goldmark, adapted from mautrix-go/format.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Extensions enables Strikethrough (~~text~~) and Table rendering.
	Extensions = goldmark.WithExtensions(extension.Strikethrough, extension.Table)
	// RendererOptions enables HardWraps (newlines become <br>) and Unsafe (raw HTML passthrough).
	RendererOptions = goldmark.WithRendererOptions(html.WithHardWraps(), html.WithUnsafe())
	// ParserOptions registers LinksTransformer at priority 1000, ahead of other AST transformations.
	ParserOptions = goldmark.WithParserOptions(parser.WithASTTransformers(util.Prioritized(&LinksTransformer{}, 1000)))

	// Renderer is the package-level Goldmark renderer, safe for concurrent use; replace it to customize rendering.
	Renderer = goldmark.New(Extensions, RendererOptions, ParserOptions)
)

Functions

func Render

func Render(markdown string) (htmlString string)

Render converts Markdown to HTML, unwrapping outer <p> tags for single-paragraph content.

Types

type LinksTransformer

type LinksTransformer struct{}

LinksTransformer is a goldmark AST transformer that adds target="_blank" to every link node.

func (*LinksTransformer) Transform

func (t *LinksTransformer) Transform(node *ast.Document, _ text.Reader, _ parser.Context)

Transform adds target="_blank" to every link node so rendered links open in a new tab.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL