markdown API

markdown

package

API reference for the markdown package.

F
function

Parse

Parse converts Markdown source to HTML.

Parameters

src
string

Returns

string
markdown/markdown.go:14-23
func Parse(src string) string

{
	flags := blackfriday.CommonHTMLFlags &^ blackfriday.Smartypants &^ blackfriday.SmartypantsFractions &^ blackfriday.SmartypantsDashes &^ blackfriday.SmartypantsLatexDashes
	renderer := blackfriday.NewHTMLRenderer(blackfriday.HTMLRendererParameters{Flags: flags})
	out := blackfriday.Run(
		[]byte(src),
		blackfriday.WithExtensions(blackfriday.CommonExtensions),
		blackfriday.WithRenderer(renderer),
	)
	return string(out)
}
S
struct

Heading

Heading represents a parsed heading.

markdown/markdown.go:26-30
type Heading struct

Fields

Name Type Description
Text string
Depth int
ID string
S
struct

slugger

markdown/markdown.go:34-34
type slugger struct

Methods

slug
Method

Parameters

text string

Returns

string
func (*slugger) slug(text string) string
{
	slug := strings.ToLower(text)
	slug = slugRe.ReplaceAllString(slug, "")
	slug = strings.TrimSpace(slug)
	slug = strings.ReplaceAll(slug, " ", "-")
	if n, ok := s.seen[slug]; ok {
		s.seen[slug] = n + 1
		return fmt.Sprintf("%s-%d", slug, n)
	}
	s.seen[slug] = 1
	return slug
}

Fields

Name Type Description
seen map[string]int
F
function

newSlugger

Returns

markdown/markdown.go:36-36
func newSlugger() *slugger

{ return &slugger{seen: make(map[string]int)} }
F
function

Headings

Headings extracts headings from Markdown and generates ids.

Parameters

src
string

Returns

markdown/markdown.go:52-95
func Headings(src string) []Heading

{
	parser := blackfriday.New(blackfriday.WithExtensions(blackfriday.CommonExtensions))
	root := parser.Parse([]byte(src))

	headings := []Heading{}
	slug := newSlugger()

	var buf bytes.Buffer
	var collectText func(n *blackfriday.Node)
	collectText = func(n *blackfriday.Node) {
		switch n.Type {
		case blackfriday.Text, blackfriday.Code:
			buf.Write(n.Literal)
		}
		for c := n.FirstChild; c != nil; c = c.Next {
			collectText(c)
		}
	}

	root.Walk(func(n *blackfriday.Node, entering bool) blackfriday.WalkStatus {
		if !entering {
			return blackfriday.GoToNext
		}
		if n.Type == blackfriday.Heading {
			buf.Reset()
			for c := n.FirstChild; c != nil; c = c.Next {
				collectText(c)
				if c.Next != nil {
					buf.WriteByte(' ')
				}
			}
			text := strings.TrimSpace(buf.String())
			level := n.Level
			headings = append(headings, Heading{
				Text:  text,
				Depth: level,
				ID:    slug.slug(text),
			})
		}
		return blackfriday.GoToNext
	})

	return headings
}
F
function

TestParse

Parameters

markdown/markdown_test.go:8-14
func TestParse(t *testing.T)

{
	md := "# Title"
	html := Parse(md)
	if html == "" || html[:4] != "<h1>" {
		t.Fatalf("unexpected html: %q", html)
	}
}
F
function

TestHeadings

Parameters

markdown/markdown_test.go:16-25
func TestHeadings(t *testing.T)

{
	md := "# Title\n## Sub"
	hs := Headings(md)
	if len(hs) != 2 {
		t.Fatalf("expected 2 headings, got %d", len(hs))
	}
	if hs[0].Text != "Title" || hs[0].Depth != 1 {
		t.Fatalf("unexpected first heading: %+v", hs[0])
	}
}
F
function

TestParsePreservesQuotes

TestParsePreservesQuotes ensures markdown.Parse keeps plain quotes so
component include directives remain parseable.

Parameters

markdown/markdown_test.go:29-38
func TestParsePreservesQuotes(t *testing.T)

{
	src := `@include:Comp:{code:"/path/file.go", uri:"/demo"}`
	html := Parse(src)
	if !strings.Contains(html, `code:&quot;/path/file.go&quot;`) {
		t.Fatalf("expected quoted code path, got %q", html)
	}
	if strings.Contains(html, "&ldquo;") || strings.Contains(html, "&rdquo;") {
		t.Fatalf("unexpected smart quotes in %q", html)
	}
}