markdown

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 30 Imported by: ? 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 repository Markdown with the Obsidian syntax that the documentation viewer supports. Paths are clean, slash-separated, and relative to the repository root, for example "docs/ref/plan.md".

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DrawingHTML added in v0.4.0

func DrawingHTML(p, label string) string

DrawingHTML returns the placeholder that the browser renders the drawing at p into. The raw URL keeps a changed file out of diff mode.

func Highlight

func Highlight(code []byte, lang, filename string, lineNumbers bool) string

Highlight renders code as class-based chroma HTML in <pre class="code-block chroma" data-lang="...">. An empty lang picks a lexer from filename. With lineNumbers the lines get "L1" anchors.

func HighlightCSS

func HighlightCSS() string

HighlightCSS returns chroma CSS for the light and dark viewer themes, scoped by html[data-theme]. Code block backgrounds are left to the page.

func IsDrawing added in v0.4.0

func IsDrawing(p string) bool

IsDrawing reports whether p is an Excalidraw scene file.

func Slug

func Slug(text string) string

Slug returns the GitHub anchor for heading text, without de-duplication.

func URL

func URL(path, fragment string) string

URL returns the viewer URL of a repository path and optional fragment.

Types

type Diff

type Diff struct {
	// Document is the merged body with diff marks, and the properties,
	// title, and aliases of the new version.
	*Document
	// PropertiesChanged is true when the front matter differs.
	PropertiesChanged bool
	// Approximate is true when the line diff gave up (linediff.MaxEdits).
	Approximate bool
	// Changes is the number of changes in the body. The first marked
	// element of each change has the attribute data-change.
	Changes int
}

Diff is the rendered diff of two versions of a note.

func RenderDiff

func RenderDiff(old, cur []byte, from string, r Resolver) (d *Diff, err error)

RenderDiff renders one merged note that shows the old and the new version: unchanged blocks once, and each changed block as its old version marked removed, then its new version marked added. Paired blocks also get word marks.

type Document

type Document struct {
	// HTML is the rendered body. It excludes frontmatter properties.
	HTML string
	// Properties lists frontmatter keys in source order.
	Properties []Property
	// PropertiesError describes invalid frontmatter. The body still renders.
	PropertiesError string
	// Title is the frontmatter title, else the first level-1 heading, else "".
	Title   string
	Aliases []string
	// Tags lists frontmatter tags, then inline tags, without "#" or duplicates.
	Tags     []string
	Headings []Heading
	// Links lists internal links, wikilinks, and embeds in document order.
	Links   []Link
	Mermaid bool
	Math    bool
}

Document is one rendered Markdown file.

func Render

func Render(src []byte, from string, r Resolver) (*Document, error)

Render converts Markdown to HTML with the Obsidian dialect. from is the repository path of src; r answers questions about other files.

type Heading

type Heading struct {
	Level int    `json:"level"`
	Text  string `json:"text"`
	ID    string `json:"id"`
}

Heading is one body heading and its anchor.

func Headings

func Headings(src []byte) []Heading

Headings lists the body headings of src with the IDs Render assigns.

type Link struct {
	// Path is the resolved repository path, or "" when unresolved.
	Path string
	// Target is the link target as written, without fragment or alias.
	Target   string
	Fragment string
	Embed    bool
	Wikilink bool
	// Context is plain text around the link for backlink panes.
	Context string
}

Link is one internal reference from a document.

type Property

type Property struct {
	Key    string
	List   bool
	Values []PropertyValue
}

Property is one frontmatter key.

type PropertyValue

type PropertyValue struct {
	Text       string
	Href       string
	Path       string
	Link       bool
	Unresolved bool
}

PropertyValue is one displayed property value. Link values have an Href.

type Resolver

type Resolver interface {
	// ResolveWikilink finds the file that an Obsidian link target names, as
	// seen from the document at from. The target has no fragment or alias.
	ResolveWikilink(from, target string) (string, bool)
	// Exists reports whether path is a file or directory that the viewer serves.
	Exists(path string) bool
	// HeadingID returns the anchor of a heading in the Markdown file at path.
	HeadingID(path, heading string) (string, bool)
	// Source returns the Markdown source of an embedded note.
	Source(path string) ([]byte, error)
}

Resolver answers questions about other repository files during one render.

type SourceLine

type SourceLine []SourceSpan

SourceLine is one highlighted source line without its line break.

func HighlightLines

func HighlightLines(code []byte, lang, filename string) ([]SourceLine, string)

HighlightLines highlights code as a whole and then splits the tokens into lines, so that a multi-line string or comment keeps its class on each line. The line texts joined with "\n" give code again: when a lexer changes the text, the lines are plain text. The second result is the language name for data-lang, as in Highlight.

func (SourceLine) HTML

func (l SourceLine) HTML(tag string, marks [][2]int) string

HTML renders the line as chroma spans. Each mark is a byte range of the line text, in order. The marked text is wrapped in <tag class="diff-word"> inside the token spans, so the colors stay; a mark that crosses tokens gives one wrap in each token.

func (SourceLine) Text

func (l SourceLine) Text() string

Text returns the text of the line.

type SourceSpan

type SourceSpan struct{ Class, Text string }

SourceSpan is one token of a highlighted source line. An empty Class is plain text.

Jump to

Keyboard shortcuts

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