-
Notifications
You must be signed in to change notification settings - Fork 0
/
md.go
254 lines (216 loc) · 6.01 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
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
//go:build !exclude_pages_formats && !exclude_pages_format_md
// Copyright (c) 2022 The Go-Enjin Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package md
import (
"fmt"
"html/template"
"net/http"
"strings"
"github.com/blevesearch/bleve/v2/mapping"
"golang.org/x/net/html"
"github.com/go-corelibs/x-text/language"
"github.com/go-corelibs/x-text/message"
clStrings "github.com/go-corelibs/strings"
"github.com/go-enjin/be/pkg/context"
"github.com/go-enjin/be/pkg/feature"
"github.com/go-enjin/be/pkg/log"
)
const Tag feature.Tag = "pages-formats-md"
var (
_ Feature = (*CFeature)(nil)
_ MakeFeature = (*CFeature)(nil)
)
type Feature interface {
feature.Feature
feature.PageFormat
}
type MakeFeature interface {
SetMore(enabled bool) MakeFeature
Make() Feature
}
type CFeature struct {
feature.CFeature
more bool
}
func New() MakeFeature {
return NewTagged(Tag)
}
func NewTagged(tag feature.Tag) MakeFeature {
f := new(CFeature)
f.Init(f)
f.PackageTag = Tag
f.FeatureTag = tag
f.CFeature.Construct(f)
return f
}
func (f *CFeature) Init(this interface{}) {
f.CFeature.Init(this)
}
func (f *CFeature) SetMore(enabled bool) MakeFeature {
f.more = enabled
return f
}
func (f *CFeature) Make() Feature {
return f
}
func (f *CFeature) Name() (name string) {
name = "md"
return
}
func (f *CFeature) Extensions() (extensions []string) {
extensions = append(extensions, "md", "md.tmpl")
return
}
func (f *CFeature) Label() (label string) {
label = "Markdown"
return
}
func (f *CFeature) Prepare(ctx context.Context, content string) (out context.Context, err error) {
if f.more {
if rendered := RenderExcerpt(content); rendered != "" {
ctx.SetSpecific("Excerpt", rendered)
}
}
return
}
func (f *CFeature) Process(ctx context.Context, content string) (html template.HTML, redirect string, err error) {
html = template.HTML(f.Enjin.TranslateShortcodes(RenderMarkdown(content), ctx))
return
}
func (f *CFeature) SearchDocumentMapping(tag language.Tag) (doctype string, dm *mapping.DocumentMapping) {
doctype, _, dm = f.NewDocumentMapping(tag)
return
}
func (f *CFeature) AddSearchDocumentMapping(tag language.Tag, indexMapping *mapping.IndexMappingImpl) {
doctype, _, dm := f.NewDocumentMapping(tag)
indexMapping.AddDocumentMapping(doctype, dm)
}
func (f *CFeature) IndexDocument(pg feature.Page) (out interface{}, err error) {
r, _ := http.NewRequest("GET", pg.Url(), nil)
r = message.SetTag(r, pg.LanguageTag())
for _, ptp := range feature.FilterTyped[feature.PageTypeProcessor](f.Enjin.Features().List()) {
if v, _, processed, e := ptp.ProcessRequestPageType(r, pg); e != nil {
log.ErrorF("error processing page type for md format indexing: %v - %v", pg.Url(), e)
} else if processed {
pg = v
}
}
var rendered string
if strings.HasSuffix(pg.Format(), ".tmpl") {
renderer := f.Enjin.GetThemeRenderer(pg.Context())
if rendered, err = renderer.RenderTextTemplateContent(f.Enjin.MustGetTheme(), pg.Context(), pg.Content()); err != nil {
err = fmt.Errorf("error rendering .md.tmpl content: %v", err)
return
}
} else {
rendered = pg.Content()
}
rendered = RenderMarkdown(rendered)
doc := NewMarkdownDocument(pg.Language(), pg.Url(), pg.Title())
var parsed *html.Node
if parsed, err = html.Parse(strings.NewReader(rendered)); err != nil {
return
}
skipNext := false
addLinkNext := false
addHeadingNext := false
addFootnoteNext := false
contents := ""
var walk func(node *html.Node)
walk = func(node *html.Node) {
if node.Type == html.ElementNode {
switch node.Data {
case "a":
addLinkNext = true
for _, attr := range node.Attr {
if attr.Key == "href" {
if strings.HasPrefix(attr.Val, "#fn:") {
addLinkNext = false
break
}
}
}
case "li":
for _, attr := range node.Attr {
if attr.Key == "id" {
if strings.HasPrefix(attr.Val, "fn:") {
addFootnoteNext = true
break
}
}
}
case "h1", "h2", "h3", "h4", "h5", "h6":
addHeadingNext = true
case "div":
for _, attr := range node.Attr {
if attr.Key == "class" {
switch attr.Val {
case "h1", "h2", "h3", "h4", "h5", "h6":
addHeadingNext = true
break
}
}
if addHeadingNext {
break
}
}
case "style", "sup", "sub":
skipNext = true
}
} else if node.Type == html.TextNode {
if skipNext {
skipNext = false
// log.DebugF("skipping text: %v - %v", node.Type, node.Data)
} else {
data := clStrings.PruneTmplActions(node.Data)
data = strings.ReplaceAll(data, "permalink", "")
data = strings.ReplaceAll(data, "top", "")
if !clStrings.Empty(data) {
if addLinkNext {
addLinkNext = false
// log.DebugF("adding markdown link: %v", data)
doc.AddLink(data)
contents = clStrings.AppendWithSpace(contents, data)
} else if addHeadingNext {
addHeadingNext = false
// log.DebugF("adding markdown heading: %v", data)
doc.AddHeading(data)
} else if addFootnoteNext {
addFootnoteNext = false
// log.DebugF("adding markdown footnote: %v", data)
doc.AddFootnote(data)
} else {
contents = clStrings.AppendWithSpace(contents, data)
}
} else {
addLinkNext = false
addHeadingNext = false
addFootnoteNext = false
}
}
}
for c := node.FirstChild; c != nil; c = c.NextSibling {
walk(c)
}
}
walk(parsed)
if !clStrings.Empty(contents) {
doc.AddContent(contents)
// log.DebugF("adding markdown contents:\n%v", contents)
}
out = doc
err = nil
return
}