/
md.go
73 lines (59 loc) · 1.96 KB
/
md.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package lint
import (
"bytes"
"regexp"
"strings"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
grh "github.com/yuin/goldmark/renderer/html"
"github.com/errata-ai/vale/v2/internal/core"
)
// Markdown configuration.
var goldMd = goldmark.New(
goldmark.WithExtensions(
extension.GFM,
extension.Footnote,
),
goldmark.WithRendererOptions(
grh.WithUnsafe(),
),
)
// Convert extended info strings -- e.g., ```callout{'title': 'NOTE'} -- that
// might confuse Blackfriday into normal "```".
var reExInfo = regexp.MustCompile("`{3,}" + `.+`)
var reLinkRef = regexp.MustCompile(`\]\[(?:[^]]+)\]`)
var reLinkDef = regexp.MustCompile(`\[(?:[^]]+)\]:`)
func (l Linter) lintMarkdown(f *core.File) error {
var buf bytes.Buffer
s, err := l.applyPatterns(f.Content, "\n```\n$1\n```\n", "`$1`", ".md")
if err != nil {
return err
}
if err = goldMd.Convert([]byte(s), &buf); err != nil {
return core.NewE100(f.Path, err)
}
// NOTE: This is required to avoid finding matches inside info strings. For
// example, if we're looking for 'json' we many incorrectly report the
// location as being in an infostring like '```json'.
//
// See https://github.com/errata-ai/vale/v2/issues/248.
body := reExInfo.ReplaceAllStringFunc(f.Content, func(m string) string {
parts := strings.Split(m, "`")
// This ensures that we respect the number of opening backticks, which
// could be more than 3.
//
// See https://github.com/errata-ai/vale/v2/issues/271.
tags := strings.Repeat("`", len(parts)-1)
span := strings.Repeat("*", len(parts[len(parts)-1]))
return tags + span
})
// NOTE: This is required to avoid finding matches inside link references.
body = reLinkRef.ReplaceAllStringFunc(body, func(m string) string {
return "][" + strings.Repeat("*", len(m)-3) + "]"
})
body = reLinkDef.ReplaceAllStringFunc(body, func(m string) string {
return "[" + strings.Repeat("*", len(m)-3) + "]:"
})
f.Content = body
return l.lintHTMLTokens(f, buf.Bytes(), 0)
}