99 "strings"
1010
1111 "github.com/flosch/pongo2/v6"
12- "github.com/honmaple/snow/builder/content"
1312 "github.com/honmaple/snow/builder/content/parser"
1413 "github.com/honmaple/snow/builder/theme/template"
1514 "github.com/honmaple/snow/config"
@@ -24,17 +23,15 @@ var (
2423 MARKDOWN_META = regexp .MustCompile (`^([^:]+):(\s+(.*)|$)` )
2524)
2625
27- type markdown struct {
28- conf config.Config
26+ type mdParser struct {
2927 opts []blackfriday.Option
3028}
3129
32- func readMeta (r io.Reader , contentBuf * bytes.Buffer , summaryBuf * bytes.Buffer ) (content. Meta , error ) {
30+ func readMeta (r io.Reader , content * bytes.Buffer , summary * bytes.Buffer , result * parser. Result ) error {
3331 var (
3432 isMeta = true
3533 isFormat = true
3634 isSummery = true
37- meta = make (content.Meta )
3835 scanner = bufio .NewScanner (r )
3936 )
4037
@@ -58,68 +55,70 @@ func readMeta(r io.Reader, contentBuf *bytes.Buffer, summaryBuf *bytes.Buffer) (
5855 }
5956
6057 if err := cf .ReadConfig (& b ); err != nil {
61- return nil , err
58+ return err
6259 }
6360 // 不要直接使用meta反序列化数据, 否则子元素map类型也会是page.Meta
64- meta = content . Meta ( cf .AllSettings () )
61+ result . FrontMatter = cf .AllSettings ()
6562 isFormat = false
6663 continue
6764 }
6865 isFormat = false
6966
7067 if isMeta {
7168 if match := MARKDOWN_META .FindStringSubmatch (line ); match != nil {
72- meta . Set (strings .ToLower (match [1 ]), strings .TrimSpace (match [3 ]))
69+ result . SetFrontMatter (strings .ToLower (match [1 ]), strings .TrimSpace (match [3 ]))
7370 continue
7471 }
7572 }
7673 isMeta = false
7774 if isSummery && MARKDOWN_MORE .MatchString (line ) {
78- summaryBuf .WriteString (contentBuf .String ())
75+ summary .WriteString (content .String ())
7976 isSummery = false
8077 }
81- contentBuf .WriteString (line )
82- contentBuf .WriteString ("\n " )
78+ content .WriteString (line )
79+ content .WriteString ("\n " )
8380 }
84- return meta , nil
81+ return nil
8582}
8683
87- func (m * markdown ) Read (r io.Reader ) (* parser.Result , error ) {
84+ func (m * mdParser ) Parse (r io.Reader ) (* parser.Result , error ) {
8885 var (
8986 summary bytes.Buffer
9087 content bytes.Buffer
9188 )
92- meta , err := readMeta (r , & content , & summary )
93- if err != nil {
94- return nil , err
95- }
9689 result := & parser.Result {
97- FrontMatter : meta ,
98- RawContent : content .String (),
90+ FrontMatter : make (map [string ]any ),
91+ }
92+ if err := readMeta (r , & content , & summary , result ); err != nil {
93+ return nil , err
9994 }
10095 if summary .Len () > 0 {
10196 result .Summary = m .HTML (summary .Bytes ())
10297 }
10398 result .Content = m .HTML (content .Bytes ())
99+ result .RawContent = content .String ()
104100 return result , nil
105101}
106102
107- func (m * markdown ) HTML (data []byte ) string {
103+ func (m * mdParser ) HTML (data []byte ) string {
108104 d := blackfriday .Run (data , m .opts ... )
109105 return string (d )
110106}
111107
112- func New (conf config.Config ) parser.Reader {
113- opts := []blackfriday.Option {
114- blackfriday .WithRenderer (NewChromaRenderer (conf .GetHighlightStyle ())),
108+ func New (conf config.Config ) parser.MarkupParser {
109+ return & mdParser {
110+ opts : []blackfriday.Option {
111+ blackfriday .WithRenderer (NewChromaRenderer (conf .GetHighlightStyle ())),
112+ },
115113 }
116- return & markdown {conf , opts }
117114}
118115
119116func NewPongo2Filter (conf config.Config ) pongo2.FilterFunction {
120- r := & markdown {conf , []blackfriday.Option {
121- blackfriday .WithRenderer (NewChromaRenderer (conf .GetHighlightStyle ())),
122- }}
117+ r := & mdParser {
118+ opts : []blackfriday.Option {
119+ blackfriday .WithRenderer (NewChromaRenderer (conf .GetHighlightStyle ())),
120+ },
121+ }
123122 return func (in * pongo2.Value , param * pongo2.Value ) (* pongo2.Value , * pongo2.Error ) {
124123 v , ok := in .Interface ().(string )
125124 if ! ok {
0 commit comments