-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrule.go
More file actions
165 lines (146 loc) · 3.67 KB
/
Copy pathrule.go
File metadata and controls
165 lines (146 loc) · 3.67 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package paragraphreadability
import (
"bytes"
"fmt"
"math"
"github.com/jeduden/mdsmith/internal/lint"
"github.com/jeduden/mdsmith/internal/mdtext"
"github.com/jeduden/mdsmith/internal/rule"
"github.com/jeduden/mdsmith/internal/rules/settings"
"github.com/yuin/goldmark/ast"
)
func init() {
rule.Register(&Rule{
MaxIndex: 14.0,
MinWords: 20,
Index: ARI,
})
}
// Rule checks that the paragraph readability index does not exceed
// a configured maximum. Uses the Automated Readability Index by
// default.
type Rule struct {
MaxIndex float64
MinWords int
Index IndexFunc
}
// ID implements rule.Rule.
func (r *Rule) ID() string { return "MDS023" }
// Name implements rule.Rule.
func (r *Rule) Name() string { return "paragraph-readability" }
// Category implements rule.Rule.
func (r *Rule) Category() string { return "meta" }
// Check implements rule.Rule.
func (r *Rule) Check(f *lint.File) []lint.Diagnostic {
var diags []lint.Diagnostic
maxIndex := r.MaxIndex
minWords := r.MinWords
index := r.Index
if index == nil {
index = ARI
}
_ = ast.Walk(
f.AST,
func(n ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering {
return ast.WalkContinue, nil
}
para, ok := n.(*ast.Paragraph)
if !ok {
return ast.WalkContinue, nil
}
if isTable(para, f) {
return ast.WalkContinue, nil
}
text := mdtext.ExtractPlainText(para, f.Source)
words := mdtext.CountWords(text)
if words < minWords {
return ast.WalkContinue, nil
}
score := index(text)
if score > maxIndex {
line := paragraphLine(para, f)
diags = append(diags, lint.Diagnostic{
File: f.Path,
Line: line,
Column: 1,
RuleID: r.ID(),
RuleName: r.Name(),
Severity: lint.Warning,
Message: readabilityMessage(score, maxIndex, words, text),
})
}
return ast.WalkContinue, nil
},
)
return diags
}
func readabilityMessage(score, maxIndex float64, words int, text string) string {
rounded := math.Round(score*10) / 10
sentences := mdtext.CountSentences(text)
avgSentLen := 0
if sentences > 0 {
avgSentLen = words / sentences
}
return fmt.Sprintf(
"paragraph too hard to read (readability index: %.1f, max %.1f)"+
"; avg sentence length %d words — try splitting long sentences",
rounded, maxIndex, avgSentLen,
)
}
func paragraphLine(para *ast.Paragraph, f *lint.File) int {
lines := para.Lines()
if lines.Len() > 0 {
return f.LineOfOffset(lines.At(0).Start)
}
return 1
}
// ApplySettings implements rule.Configurable.
func (r *Rule) ApplySettings(s map[string]any) error {
for k, v := range s {
switch k {
case "max-index":
n, ok := settings.ToFloat(v)
if !ok {
return fmt.Errorf(
"paragraph-readability: max-index must be a number, got %T",
v,
)
}
r.MaxIndex = n
case "min-words":
n, ok := settings.ToInt(v)
if !ok {
return fmt.Errorf(
"paragraph-readability: min-words must be an integer, got %T",
v,
)
}
r.MinWords = n
default:
return fmt.Errorf(
"paragraph-readability: unknown setting %q", k,
)
}
}
return nil
}
// DefaultSettings implements rule.Configurable.
func (r *Rule) DefaultSettings() map[string]any {
return map[string]any{
"max-index": 14.0,
"min-words": 20,
}
}
// isTable returns true if the paragraph's first line starts with a pipe,
// indicating it is a markdown table (goldmark without the table extension
// parses tables as paragraphs).
func isTable(para *ast.Paragraph, f *lint.File) bool {
lines := para.Lines()
if lines.Len() == 0 {
return false
}
seg := lines.At(0)
return bytes.HasPrefix(bytes.TrimSpace(f.Source[seg.Start:seg.Stop]), []byte("|"))
}
var _ rule.Configurable = (*Rule)(nil)