This commit inlines https://github.com/tj/go-termd for a few reasons: - off the shelf it relies on a broken, erroneously released version of blackfriday (a markdown parser) - based on discussion with ampinsk, there are some tweaks we'd like to make to markdown rendering beyond what the library exposes now - it's a small library (around 300 sloc) This commit only: - messes with go.mod to fix the blackfriday issues - adds an inclusion note - renames the package
36 lines
820 B
Go
36 lines
820 B
Go
// This package is sourced from https://github.com/tj/go-termd under the terms of the MIT license.
|
|
// It was inlined to work around some dependency issues.
|
|
package markdown
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/alecthomas/chroma"
|
|
"github.com/alecthomas/chroma/lexers"
|
|
)
|
|
|
|
// SyntaxHighlighter is the interface used to highlight blocks of code.
|
|
type SyntaxHighlighter interface {
|
|
Token(chroma.Token) string
|
|
}
|
|
|
|
// highlight returns highlighted code, or the input text on error.
|
|
func highlight(source, lang string, highlight SyntaxHighlighter) string {
|
|
l := lexers.Get(lang)
|
|
if l == nil {
|
|
return source
|
|
}
|
|
|
|
l = chroma.Coalesce(l)
|
|
|
|
it, err := l.Tokenise(nil, source)
|
|
if err != nil {
|
|
return source
|
|
}
|
|
|
|
var w strings.Builder
|
|
for _, t := range it.Tokens() {
|
|
w.WriteString(highlight.Token(t))
|
|
}
|
|
return w.String()
|
|
}
|