-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
md.go
43 lines (33 loc) · 808 Bytes
/
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
package hype
import (
"bytes"
"fmt"
"io"
"github.com/gopherguides/hype/mdx"
)
func Markdown() PreParseFn {
fn := func(p *Parser, r io.Reader) (io.Reader, error) {
if p == nil {
return nil, ErrIsNil("parser")
}
if r == nil {
return nil, ErrIsNil("reader")
}
md := mdx.New()
md.DisablePages = p.DisablePages
b, err := io.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("markdown: %w", err)
}
b = bytes.ReplaceAll(b, []byte("\\n"), []byte(" \n"))
b, err = md.Parse(b)
if err != nil {
return nil, fmt.Errorf("markdown: %w", err)
}
b = bytes.ReplaceAll(b, []byte("’"), []byte("'"))
b = bytes.ReplaceAll(b, []byte("“"), []byte("\""))
b = bytes.ReplaceAll(b, []byte("”"), []byte("\""))
return bytes.NewReader(b), nil
}
return fn
}