CommonMark renderer options, unit test both renderers
This commit is contained in:
parent
ebe161a08a
commit
223dfdea66
|
@ -17,6 +17,8 @@ import (
|
|||
|
||||
"github.com/go-ini/ini"
|
||||
"github.com/writeas/web-core/log"
|
||||
"github.com/yuin/goldmark"
|
||||
"github.com/yuin/goldmark/extension"
|
||||
"golang.org/x/net/idna"
|
||||
)
|
||||
|
||||
|
@ -171,6 +173,11 @@ type (
|
|||
|
||||
// Which Markdown renderer to use
|
||||
Renderer string `ini:"markdown_renderer"`
|
||||
|
||||
// Options for the Goldmark renderer
|
||||
RendererOptions string `ini:"markdown_options"`
|
||||
// Conversion of options ready for the renderer
|
||||
rendererExtensions []goldmark.Extender
|
||||
}
|
||||
|
||||
// Config holds the complete configuration for running a writefreely instance
|
||||
|
@ -255,6 +262,38 @@ func (ac AppCfg) MarkdownRenderer() string {
|
|||
return "saturday"
|
||||
}
|
||||
|
||||
func (ac AppCfg) RendererExtensions() []goldmark.Extender {
|
||||
if ac.rendererExtensions != nil {
|
||||
return ac.rendererExtensions
|
||||
}
|
||||
var extlist []goldmark.Extender
|
||||
optlist := strings.FieldsFunc(ac.RendererOptions, func(r rune) bool {
|
||||
return r == ' ' || r == '\t' || r == ','
|
||||
})
|
||||
for _, opt := range optlist {
|
||||
switch opt {
|
||||
case "table":
|
||||
extlist = append(extlist, extension.Table)
|
||||
case "strikethrough":
|
||||
extlist = append(extlist, extension.Strikethrough)
|
||||
case "linkify":
|
||||
extlist = append(extlist, extension.Linkify)
|
||||
case "tasklist":
|
||||
extlist = append(extlist, extension.TaskList)
|
||||
case "gfm":
|
||||
extlist = append(extlist, extension.GFM)
|
||||
case "definitionlist":
|
||||
extlist = append(extlist, extension.DefinitionList)
|
||||
case "typographer":
|
||||
extlist = append(extlist, extension.Typographer)
|
||||
case "cjk":
|
||||
extlist = append(extlist, extension.CJK)
|
||||
}
|
||||
}
|
||||
ac.rendererExtensions = extlist
|
||||
return extlist
|
||||
}
|
||||
|
||||
// Load reads the given configuration file, then parses and returns it as a Config.
|
||||
func Load(fname string) (*Config, error) {
|
||||
if fname == "" {
|
||||
|
|
|
@ -31,7 +31,6 @@ import (
|
|||
"github.com/writeas/web-core/log"
|
||||
"github.com/writeas/web-core/stringmanip"
|
||||
"github.com/yuin/goldmark"
|
||||
"github.com/yuin/goldmark/extension"
|
||||
"github.com/yuin/goldmark/parser"
|
||||
|
||||
"github.com/writefreely/writefreely/config"
|
||||
|
@ -172,12 +171,7 @@ func applyMarkdownSpecial(data []byte, skipNoFollow bool, baseURL string, cfg *c
|
|||
}
|
||||
|
||||
func applyCommonmarkSpecial(data []byte, skipNoFollow bool, baseURL string, cfg *config.Config) string {
|
||||
extensions := []goldmark.Extender{
|
||||
extension.GFM,
|
||||
extension.DefinitionList,
|
||||
extension.Typographer,
|
||||
// but no footnotes, see https://github.com/writefreely/writefreely/issues/338
|
||||
}
|
||||
extensions := cfg.App.RendererExtensions()
|
||||
if baseURL != "" {
|
||||
tagPrefix := baseURL + "tag:"
|
||||
if cfg.App.Chorus {
|
||||
|
@ -188,11 +182,7 @@ func applyCommonmarkSpecial(data []byte, skipNoFollow bool, baseURL string, cfg
|
|||
})
|
||||
}
|
||||
md := goldmark.New(
|
||||
goldmark.WithExtensions(
|
||||
&hashtag.Extender{
|
||||
Resolver: hashtagResolver{},
|
||||
},
|
||||
),
|
||||
goldmark.WithExtensions(extensions...),
|
||||
goldmark.WithParserOptions(
|
||||
parser.WithAutoHeadingID(),
|
||||
),
|
||||
|
@ -258,18 +248,16 @@ func applySaturdaySpecial(data []byte, skipNoFollow bool, baseURL string, cfg *c
|
|||
|
||||
func applyBasicMarkdown(data []byte, cfg *config.Config) string {
|
||||
if cfg.App.MarkdownRenderer() == "goldmark" {
|
||||
return applyBasicCommonmark(data)
|
||||
return applyBasicCommonmark(data, cfg)
|
||||
} else {
|
||||
return applyBasicSaturday(data)
|
||||
}
|
||||
}
|
||||
|
||||
func applyBasicCommonmark(data []byte) string {
|
||||
func applyBasicCommonmark(data []byte, cfg *config.Config) string {
|
||||
md := goldmark.New(
|
||||
goldmark.WithExtensions(
|
||||
extension.Strikethrough,
|
||||
extension.Linkify,
|
||||
extension.Typographer,
|
||||
cfg.App.RendererExtensions()...,
|
||||
),
|
||||
)
|
||||
var inbuf bytes.Buffer
|
||||
|
@ -395,12 +383,13 @@ func sanitizePost(content string) string {
|
|||
// choosing what to generate. In case a post has a title, this function will
|
||||
// fail, and logic should instead be implemented to skip this when there's no
|
||||
// title, like so:
|
||||
// var desc string
|
||||
// if title == "" {
|
||||
// desc = postDescription(content, title, friendlyId)
|
||||
// } else {
|
||||
// desc = shortPostDescription(content)
|
||||
// }
|
||||
//
|
||||
// var desc string
|
||||
// if title == "" {
|
||||
// desc = postDescription(content, title, friendlyId)
|
||||
// } else {
|
||||
// desc = shortPostDescription(content)
|
||||
// }
|
||||
func postDescription(content, title, friendlyId string) string {
|
||||
maxLen := 140
|
||||
|
||||
|
|
|
@ -10,7 +10,11 @@
|
|||
|
||||
package writefreely
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/writefreely/writefreely/config"
|
||||
)
|
||||
|
||||
func TestApplyBasicMarkdown(t *testing.T) {
|
||||
tests := []struct {
|
||||
|
@ -32,12 +36,19 @@ func TestApplyBasicMarkdown(t *testing.T) {
|
|||
{"date", "12. April", `12. April`},
|
||||
{"table", "| Hi | There |", `| Hi | There |`},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
res := applyBasicMarkdown([]byte(test.in))
|
||||
if res != test.result {
|
||||
t.Errorf("%s: wanted %s, got %s", test.name, test.result, res)
|
||||
}
|
||||
})
|
||||
for _, renderer := range []string{"saturday", "goldmark"} {
|
||||
cfg := &config.Config{
|
||||
App: config.AppCfg{
|
||||
Renderer: renderer,
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
res := applyBasicMarkdown([]byte(test.in), cfg)
|
||||
if res != test.result {
|
||||
t.Errorf("%s: wanted %s, got %s", test.name, test.result, res)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue