markdown

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: BSD-3-Clause Imports: 25 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package markdown renders the body of a post or a comment to HTML and extracts the mentions from it. It is pure: it resolves no users and does no I/O.

This package is the only place where HTML made from user content is produced, and the web layer marks its output as trusted (template.HTML) because of the rules below (SPEC D9; CLAUDE.md "Don't"). Anyone who changes the package must keep every one of them.

Markdown bodies are rendered by goldmark with the GitHub-flavored extensions (tables, strikethrough, autolinks, task lists), in safe mode:

  • html.WithUnsafe is never used: raw HTML, block or inline, is replaced by an HTML comment that says it was omitted, so a body cannot add a tag, an attribute, a script or a style.
  • Links are emitted only for the schemes http, https and mailto, and for relative references; any other scheme (javascript:, vbscript:, data:, file:, ftp:, and the like, in any letter case, with or without hidden white space or control characters) drops the link and keeps its text.
  • Images are emitted only for http and https sources; anything else is replaced by its alt text.
  • Every link has rel="nofollow noopener noreferrer" and target="_blank". Every image has loading="lazy".
  • Task-list checkboxes are always disabled, so the reader cannot change them.
  • Headings are shifted down by two levels and capped at h6, so a body never emits h1 or h2 (the page has its own), and they have no id attribute.
  • Attribute values (href, src, alt, title) are always HTML-escaped.
  • A fenced code block is <pre class="code" data-lang="go"><code>…</code></pre> (code.go). The code is highlighted by chroma, which writes only <span class="hl-…"> around text it has escaped itself: no inline style, no other tag and no other attribute. data-lang is the first word of the info string, kept only when it matches [A-Za-z0-9_+#.-]{1,32}, so it needs no escaping. The lexer is found by exact name, alias or file extension, never by guessing; a block with no language, an unknown one, over 32 KiB or over 1,000 lines is plain (escaped text).
  • A mention is a link only when the caller passes its username to Renderer.RenderMentions (the mentions it saved). The link is built here from the username, which has only a-z, 0-9, "_" and "-": it points to "/search?q=from%3A<username>" and has the class "mention". It is never made inside code, in the text of a link or in a URL.

Text bodies are HTML-escaped as a whole; only the markup that this package writes itself (paragraphs, line breaks and links to http and https URLs) is HTML.

A Renderer is built once by New and shared. It holds no state that changes after construction, and goldmark's Convert and Parse are safe for concurrent use, so one Renderer serves any number of goroutines.

Index

Constants

View Source
const CodeClassPrefix = "hl-"

CodeClassPrefix is the prefix of the CSS classes of the highlighted tokens of a code block: a keyword is <span class="hl-k">. The class after the prefix is chroma's name of the token type. The stylesheet internal/web/static/code.css is generated for this prefix.

View Source
const (
	// TagFormatUnknown marks a body format that is neither FormatText nor
	// FormatMarkdown.
	TagFormatUnknown = "FormatUnknown"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Format

type Format string

Format is the format of a body. Its values are the same strings the store keeps, so a stored format can be converted to it directly.

const (
	// FormatText is a body shown as written: escaped, with its line breaks and
	// with http and https URLs made clickable.
	FormatText Format = "text"
	// FormatMarkdown is a body written in GitHub-flavored markdown.
	FormatMarkdown Format = "markdown"
)

type KeywordMatcher added in v1.2.0

type KeywordMatcher struct {
	// contains filtered or unexported fields
}

KeywordMatcher finds keywords in a text: whole words, ignoring case, and phrases of several words, in which any run of white space matches one space. Make it with NewKeywordMatcher; it is cheap to ask it about many keywords.

func NewKeywordMatcher added in v1.2.0

func NewKeywordMatcher(s string) KeywordMatcher

NewKeywordMatcher prepares s for matching: lower-cased, with every run of white space made one space.

func (KeywordMatcher) Matches added in v1.2.0

func (m KeywordMatcher) Matches(keyword string) bool

Matches reports whether the keyword is in the text as a whole word or phrase: the character before it and the one after it are not letters, digits or underscores (when the keyword itself starts or ends with one), so "deploy" matches in "Deploy now!" and "the deploy-script" but not in "redeployed". An empty keyword matches nothing.

type Renderer

type Renderer struct {
	// contains filtered or unexported fields
}

Renderer renders bodies and finds their mentions. Build it once with New and pass it around. It is safe for concurrent use. It keeps the HTML of the highlighted code blocks it has made, up to 8 MiB and least recently used first, so a block that is read again is not lexed again.

func New

func New() *Renderer

New returns a Renderer: goldmark with the GFM extensions, in safe mode, with the link, image and heading rules of the package documentation.

func (*Renderer) Mentions

func (r *Renderer) Mentions(format Format, body string) []string

Mentions returns the usernames mentioned in body as @username, lower-cased, without duplicates, in order of appearance. A username has 3 to 32 of [a-z0-9_-] and the @ stands at the start or after a character that is not a letter, a digit or an underscore (so an e-mail address is no mention). In a markdown body, code spans, code blocks, link destinations, autolinks and raw HTML are ignored. An unknown format has no mentions.

func (*Renderer) PlainText added in v1.2.0

func (r *Renderer) PlainText(format Format, body string) string

PlainText returns the readable text of body outside code, which is where keywords are looked for (SPEC §7.15). A text body is its own text. In a markdown body the code spans, code blocks, link destinations, autolinks, images and raw HTML are left out, as Mentions leaves them out; the blocks are separated by a line break. An unknown format has no text.

func (*Renderer) Render

func (r *Renderer) Render(format Format, body string) (template.HTML, error)

Render returns the HTML of body in the given format, with no mention made a link. The result is safe to insert in a page as it is (see the package documentation). An unknown format is the error TagFormatUnknown.

func (*Renderer) RenderMentions

func (r *Renderer) RenderMentions(format Format, body string, mentioned []string) (template.HTML, error)

RenderMentions is Render with the mentions of mentioned made links: each @username of body (outside code, links and URLs) whose lower-cased username is in mentioned becomes <a href="/search?q=from%3Ausername" class="mention">@username</a>. Any other @word stays text. mentioned is what the caller saved as the mentions of the post (see Renderer.Mentions); it is not checked here.

Directories

Path Synopsis
cmd
gencodecss command
Command gencodecss writes internal/web/static/code.css, the stylesheet of the code blocks that internal/markdown renders: the block itself and the colour of each hl- class of the highlighted tokens.
Command gencodecss writes internal/web/static/code.css, the stylesheet of the code blocks that internal/markdown renders: the block itself and the colour of each hl- class of the highlighted tokens.

Jump to

Keyboard shortcuts

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