github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/godoc/markdown.go (about) 1 // Copyright 2020 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package godoc 6 7 import ( 8 "bytes" 9 10 "github.com/yuin/goldmark" 11 "github.com/yuin/goldmark/parser" 12 "github.com/yuin/goldmark/renderer/html" 13 ) 14 15 // renderMarkdown converts a limited and opinionated flavor of Markdown (compliant with 16 // CommonMark 0.29) to HTML for the purposes of Go websites. 17 // 18 // The Markdown source may contain raw HTML, 19 // but Go templates have already been processed. 20 func renderMarkdown(src []byte) ([]byte, error) { 21 // parser.WithHeadingAttribute allows custom ids on headings. 22 // html.WithUnsafe allows use of raw HTML, which we need for tables. 23 md := goldmark.New( 24 goldmark.WithParserOptions(parser.WithHeadingAttribute()), 25 goldmark.WithRendererOptions(html.WithUnsafe())) 26 var buf bytes.Buffer 27 if err := md.Convert(src, &buf); err != nil { 28 return nil, err 29 } 30 return buf.Bytes(), nil 31 }