-
Notifications
You must be signed in to change notification settings - Fork 11
/
mdformatter.go
434 lines (372 loc) · 10.9 KB
/
mdformatter.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// Copyright (c) Bartłomiej Płotka @bwplotka
// Licensed under the Apache License 2.0.
package mdformatter
import (
"bytes"
"context"
"fmt"
"io"
"os"
"sort"
"time"
"github.com/Kunde21/markdownfmt/v3/markdown"
"github.com/bwplotka/mdox/pkg/gitdiff"
"github.com/efficientgo/core/logerrcapture"
"github.com/efficientgo/core/merrors"
"github.com/go-kit/log"
"github.com/gohugoio/hugo/parser/pageparser"
"github.com/mattn/go-isatty"
"github.com/prometheus/client_golang/prometheus"
"github.com/theckman/yacspin"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"gopkg.in/yaml.v3"
)
type mdformatterMetrics struct {
filesProcessed prometheus.Counter
perFileLatency *prometheus.HistogramVec
}
func newMdformatterMetrics(reg *prometheus.Registry) *mdformatterMetrics {
m := &mdformatterMetrics{}
m.filesProcessed = prometheus.NewCounter(prometheus.CounterOpts{
Name: "mdox_processed_files_total",
Help: "The total number of processed files",
})
m.perFileLatency = prometheus.NewHistogramVec(
prometheus.HistogramOpts{Name: "mdox_per_file_latency", Buckets: prometheus.DefBuckets},
[]string{"filepath"},
)
if reg != nil {
reg.MustRegister(m.filesProcessed, m.perFileLatency)
}
return m
}
type SourceContext struct {
context.Context
Filepath string
LineNumbers string
}
type FrontMatterTransformer interface {
TransformFrontMatter(ctx SourceContext, frontMatter map[string]interface{}) ([]byte, error)
Close(ctx SourceContext) error
}
type BackMatterTransformer interface {
TransformBackMatter(ctx SourceContext) ([]byte, error)
Close(ctx SourceContext) error
}
type LinkTransformer interface {
TransformDestination(ctx SourceContext, destination []byte) ([]byte, error)
Close(ctx SourceContext) error
}
type CodeBlockTransformer interface {
TransformCodeBlock(ctx SourceContext, infoString []byte, code []byte) ([]byte, error)
Close(ctx SourceContext) error
}
type Formatter struct {
ctx context.Context
fm FrontMatterTransformer
bm BackMatterTransformer
link LinkTransformer
cb CodeBlockTransformer
reg *prometheus.Registry
softWraps bool
codeFmt bool
}
// Option is a functional option type for Formatter objects.
type Option func(*Formatter)
// WithFrontMatterTransformer allows you to override the default FrontMatterTransformer.
func WithFrontMatterTransformer(fm FrontMatterTransformer) Option {
return func(m *Formatter) {
m.fm = fm
}
}
// WithBackMatterTransformer allows you to override the default BackMatterTransformer.
func WithBackMatterTransformer(bm BackMatterTransformer) Option {
return func(m *Formatter) {
m.bm = bm
}
}
// WithLinkTransformer allows you to override the default LinkTransformer.
func WithLinkTransformer(l LinkTransformer) Option {
return func(m *Formatter) {
m.link = l
}
}
// WithCodeBlockTransformer allows you to override the default CodeBlockTransformer.
func WithCodeBlockTransformer(cb CodeBlockTransformer) Option {
return func(m *Formatter) {
m.cb = cb
}
}
// WithMetrics allows you to pass in Prometheus registry.
func WithMetrics(reg *prometheus.Registry) Option {
return func(m *Formatter) {
m.reg = reg
}
}
// WithCodeBlockTransformer allows you to override default softWrap.
func WithSoftWraps() Option {
return func(m *Formatter) {
m.softWraps = true
}
}
// WithCodeFmt specifies that we shouldn't reformat code snippets.
func WithCodeFmt() Option {
return func(m *Formatter) {
m.codeFmt = true
}
}
func New(ctx context.Context, opts ...Option) *Formatter {
f := &Formatter{
ctx: ctx,
fm: FormatFrontMatterTransformer{},
}
for _, opt := range opts {
opt(f)
}
return f
}
type RemoveFrontMatter struct{}
func (RemoveFrontMatter) TransformFrontMatter(_ SourceContext, _ map[string]interface{}) ([]byte, error) {
return nil, nil
}
func (RemoveFrontMatter) Close() error { return nil }
type FormatFrontMatterTransformer struct{}
func (FormatFrontMatterTransformer) TransformFrontMatter(_ SourceContext, frontMatter map[string]interface{}) ([]byte, error) {
return FormatFrontMatter(frontMatter)
}
func FormatFrontMatter(m map[string]interface{}) ([]byte, error) {
if len(m) == 0 {
return nil, nil
}
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Sort(sort.Reverse(sort.StringSlice(keys)))
f := sortedFrontMatter{
m: m,
keys: keys,
}
b := bytes.NewBuffer([]byte("---\n"))
o, err := yaml.Marshal(f)
if err != nil {
return nil, fmt.Errorf("marshall front matter: %w", err)
}
_, _ = b.Write(o)
_, _ = b.Write([]byte("---\n\n"))
return b.Bytes(), nil
}
var _ yaml.Marshaler = sortedFrontMatter{}
type sortedFrontMatter struct {
m map[string]interface{}
keys []string
}
func (f sortedFrontMatter) MarshalYAML() (interface{}, error) {
n := &yaml.Node{
Kind: yaml.MappingNode,
}
for _, k := range f.keys {
n.Content = append(n.Content, &yaml.Node{Kind: yaml.ScalarNode, Value: k})
b, err := yaml.Marshal(f.m[k])
if err != nil {
return nil, fmt.Errorf("map marshal: %w", err)
}
v := &yaml.Node{}
if err := yaml.Unmarshal(b, v); err != nil {
return nil, err
}
// We expect a node of type document with single content containing other nodes.
if len(v.Content) != 1 {
return nil, fmt.Errorf("unexpected node after unmarshalling interface: %#v", v)
}
// TODO(bwplotka): This creates weird indentation, fix it.
n.Content = append(n.Content, v.Content[0])
}
return n, nil
}
func (FormatFrontMatterTransformer) Close(SourceContext) error { return nil }
type Diffs []gitdiff.Diff
func (d Diffs) String() string {
if len(d) == 0 {
return "files the same; no diff"
}
b := bytes.Buffer{}
for _, diff := range d {
_, _ = b.Write(diff.ToCombinedFormat())
}
return b.String()
}
func newSpinner(suffix string) (*yacspin.Spinner, error) {
cfg := yacspin.Config{
Frequency: 100 * time.Millisecond,
CharSet: yacspin.CharSets[11],
Suffix: suffix,
ColorAll: true,
Writer: os.Stderr,
Colors: []string{"cyan", "bold"},
}
spin, err := yacspin.New(cfg)
if err != nil {
return nil, err
}
if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {
return spin, nil
}
return nil, nil
}
// Format formats given markdown files in-place. IsFormatted `With...` function to see what modifiers you can add.
func Format(ctx context.Context, logger log.Logger, files []string, opts ...Option) error {
spin, err := newSpinner(" Formatting: ")
if err != nil {
return err
}
return format(ctx, logger, files, nil, spin, opts...)
}
// IsFormatted tries to formats given markdown files and return Diff if files are not formatted.
// If diff is empty it means all files are formatted.
func IsFormatted(ctx context.Context, logger log.Logger, files []string, opts ...Option) (diffs Diffs, err error) {
d := Diffs{}
spin, err := newSpinner(" Checking: ")
if err != nil {
return nil, err
}
if err := format(ctx, logger, files, &d, spin, opts...); err != nil {
return nil, err
}
return d, nil
}
func format(ctx context.Context, logger log.Logger, files []string, diffs *Diffs, spin *yacspin.Spinner, opts ...Option) error {
f := New(ctx, opts...)
b := bytes.Buffer{}
// TODO(bwplotka): Add concurrency (collector will need to redone).
m := newMdformatterMetrics(f.reg)
errs := merrors.New()
if spin != nil {
errs.Add(spin.Start())
}
for _, fn := range files {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
if spin != nil {
spin.Message(fn + "...")
}
errs.Add(func() error {
startTime := time.Now()
m.filesProcessed.Inc()
file, err := os.OpenFile(fn, os.O_RDWR, 0)
if err != nil {
return fmt.Errorf("open %v: %w", fn, err)
}
defer logerrcapture.ExhaustClose(logger, file, "close file %v", fn)
b.Reset()
if err := f.Format(file, &b); err != nil {
return err
}
if diffs != nil {
if _, err := file.Seek(0, 0); err != nil {
return err
}
in, err := io.ReadAll(file)
if err != nil {
return fmt.Errorf("read all %v: %w", fn, err)
}
if !bytes.Equal(in, b.Bytes()) {
*diffs = append(*diffs, gitdiff.CompareBytes(in, fn, b.Bytes(), fn+" (formatted)"))
}
return nil
}
n, err := file.WriteAt(b.Bytes(), 0)
if err != nil {
return fmt.Errorf("write %v: %w", fn, err)
}
timeTaken := time.Since(startTime)
m.perFileLatency.WithLabelValues(fn).Observe(timeTaken.Seconds())
return file.Truncate(int64(n))
}())
}
if spin != nil {
errs.Add(spin.Stop())
}
return errs.Err()
}
// Format writes formatted input file into out writer.
func (f *Formatter) Format(file *os.File, out io.Writer) error {
sourceCtx := SourceContext{
Context: f.ctx,
Filepath: file.Name(),
}
b, err := io.ReadAll(file)
if err != nil {
return fmt.Errorf("read %v: %w", file.Name(), err)
}
content := b
frontMatter := map[string]interface{}{}
fm, err := pageparser.ParseFrontMatterAndContent(bytes.NewReader(b))
if err == nil && len(fm.FrontMatter) > 0 {
content = fm.Content
frontMatter = fm.FrontMatter
}
if f.fm != nil {
// TODO(bwplotka): Handle some front matter, wrongly put not as header.
hdr, err := f.fm.TransformFrontMatter(sourceCtx, frontMatter)
if err != nil {
return err
}
if _, err := out.Write(hdr); err != nil {
return err
}
if err := f.fm.Close(sourceCtx); err != nil {
return err
}
}
if f.bm != nil {
back, err := f.bm.TransformBackMatter(sourceCtx)
if err != nil {
return err
}
content = append(content, back...)
if err := f.fm.Close(sourceCtx); err != nil {
return err
}
}
// Hack: run Convert two times to ensure deterministic whitespace alignment.
// This also immediately show transformers which are not working well together etc.
tmp := bytes.Buffer{}
renderer := markdown.NewRenderer()
if f.softWraps {
renderer.AddMarkdownOptions(markdown.WithSoftWraps())
}
// Enable Go code reformatting unless --no-code-fmt is set.
if f.codeFmt {
renderer.AddMarkdownOptions(markdown.WithCodeFormatters(markdown.GoCodeFormatter))
}
tr := &transformer{
wrapped: renderer,
sourceCtx: sourceCtx,
link: f.link, cb: f.cb,
frontMatterLen: len(frontMatter),
}
if err := goldmark.New(
goldmark.WithExtensions(extension.GFM),
goldmark.WithParserOptions(parser.WithAttribute() /* Enable # headers {#custom-ids} */, parser.WithHeadingAttribute()),
goldmark.WithRenderer(nopOpsRenderer{Renderer: tr}),
).Convert(content, &tmp); err != nil {
return fmt.Errorf("first formatting phase for %v: %w", file.Name(), err)
}
if err := tr.Close(sourceCtx); err != nil {
return fmt.Errorf("%v: %w", file.Name(), err)
}
if err := goldmark.New(
goldmark.WithExtensions(extension.GFM),
goldmark.WithParserOptions(parser.WithAttribute() /* Enable # headers {#custom-ids} */, parser.WithHeadingAttribute()),
goldmark.WithRenderer(renderer), // No transforming for second phase.
).Convert(tmp.Bytes(), out); err != nil {
return fmt.Errorf("second formatting phase for %v: %w", file.Name(), err)
}
return nil
}