| // Copyright 2020 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| package web |
| |
| import ( |
| "bytes" |
| "unicode/utf8" |
| |
| "github.com/yuin/goldmark" |
| "github.com/yuin/goldmark/parser" |
| "github.com/yuin/goldmark/renderer/html" |
| ) |
| |
| // renderMarkdown converts a limited and opinionated flavor of Markdown (compliant with |
| // CommonMark 0.29) to HTML for the purposes of Go websites. |
| // |
| // The Markdown source may contain raw HTML, |
| // but Go templates have already been processed. |
| func renderMarkdown(src []byte) ([]byte, error) { |
| src = replaceTabs(src) |
| // parser.WithHeadingAttribute allows custom ids on headings. |
| // html.WithUnsafe allows use of raw HTML, which we need for tables. |
| md := goldmark.New( |
| goldmark.WithParserOptions(parser.WithHeadingAttribute()), |
| goldmark.WithRendererOptions(html.WithUnsafe())) |
| var buf bytes.Buffer |
| if err := md.Convert(src, &buf); err != nil { |
| return nil, err |
| } |
| return buf.Bytes(), nil |
| } |
| |
| // replaceTabs replaces all tabs in text with spaces up to a 4-space tab stop. |
| // |
| // In Markdown, tabs used for indentation are required to be interpreted as |
| // 4-space tab stops. See https://spec.commonmark.org/0.30/#tabs. |
| // Go also renders nicely and more compactly on the screen with 4-space |
| // tab stops, while browsers often use 8-space. |
| // And Goldmark crashes in some inputs that mix spaces and tabs. |
| // Fix the crashes and make the Go code consistently compact across browsers, |
| // all while staying Markdown-compatible, by expanding to 4-space tab stops. |
| // |
| // This function does not handle multi-codepoint Unicode sequences correctly. |
| func replaceTabs(text []byte) []byte { |
| var buf bytes.Buffer |
| col := 0 |
| for len(text) > 0 { |
| r, size := utf8.DecodeRune(text) |
| text = text[size:] |
| |
| switch r { |
| case '\n': |
| buf.WriteByte('\n') |
| col = 0 |
| |
| case '\t': |
| buf.WriteByte(' ') |
| col++ |
| for col%4 != 0 { |
| buf.WriteByte(' ') |
| col++ |
| } |
| |
| default: |
| buf.WriteRune(r) |
| col++ |
| } |
| } |
| return buf.Bytes() |
| } |