-
Notifications
You must be signed in to change notification settings - Fork 0
/
blog.go
336 lines (287 loc) · 7.74 KB
/
blog.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
package blog
import (
"bytes"
"encoding/json"
"fmt"
"github.com/foreversmart/notion_blog/lib/bdfanyi"
"github.com/foreversmart/notion_blog/log"
"github.com/foreversmart/notion_blog/meta"
"github.com/foreversmart/notion_blog/utils"
"github.com/go-rod/rod"
"html/template"
"regexp"
"strconv"
"strings"
"time"
)
type HugoBlogMeta struct {
Title string `json:"title"`
SubTitle string `json:"sub_title"`
Excerpt string `json:"excerpt"`
Description string `json:"description"`
Date string `json:"date"`
Image string `json:"image"`
Tags []string `json:"tags"`
Category []string `json:"category"`
UrlPath string `json:"url_path"`
Url string `json:"url"`
}
type Blog struct {
Browser *rod.Browser
}
func NewBlog() *Blog {
// Launch a new browser with default options, and connect to it.
browser := rod.New()
err := browser.Connect()
if err != nil {
panic(err)
}
browser = browser.Timeout(time.Minute)
return &Blog{
Browser: browser,
}
}
func (b *Blog) Close() error {
return b.Browser.Close()
}
func (b *Blog) PageIndex(pageId string) (subPages []string, err error) {
url := Host + pageId
page := b.Browser.MustPage(url)
// sleep enough time to wait page render
time.Sleep(time.Second * 7)
eles, err := page.Elements(".notion-page-block")
if err != nil {
return nil, err
}
for _, ele := range eles {
aEle, err := ele.Element("a")
if err != nil {
continue
}
aHref, err := aEle.Attribute("href")
if err != nil {
continue
}
subPages = append(subPages, *aHref)
}
page.Close()
return
}
//func (b *Blog) WalkPages(entrances []string) (allPages []string, err error) {
// // record page has already visited
// pageMap := make(map[string]bool)
// // page chan queue
// pageChan := make(chan string, 10000)
//
// // in queue
// for _, e := range entrances {
// pageChan <- e
// pageMap[e] = true
// }
//
// for len(pageChan) > 0 {
// pageId := <-pageChan
// fmt.Println("processing", pageId)
// pageMeta, err := meta.PageMetaInfo(pageId)
// if err != nil {
// return nil, err
// }
//
// fmt.Println(pageMeta)
//
// // if is index page
// if pageMeta.Meta["index"] == "index" {
// fmt.Println("processing index", pageId)
// subPages, err := b.PageIndex(pageId)
// if err != nil {
// return nil, err
// }
//
// for _, s := range subPages {
// if !pageMap[s] {
// pageChan <- s
// pageMap[s] = true
// }
// }
//
// }
//
// // find the publish page
// if len(pageMeta.Meta["sub_title"]) > 0 {
// allPages = append(allPages, pageId)
// }
// }
//
// return
//}
func (b *Blog) HugoBlog(pageId string, pageMeta *meta.PageMeta) (content string, err error) {
url := Host + pageId
page := b.Browser.MustPage(url)
// sleep enough time to wait page render
time.Sleep(time.Second * 7)
title, _, err := PageTile(page)
if err != nil {
return "", err
}
pageContent, err := PageContent(pageId, page)
if err != nil {
return "", err
}
category := pageMeta.PageFolders
category = append(category, PageCategory(pageMeta)...)
subTitle := pageMeta.SubTitle
desc, err := PageDesc(page)
if err != nil {
log.Logger.Error(err)
return "", err
}
r := []rune(desc)
if len(r) > 150 {
desc = strconv.Quote(string(r[0:150]))
desc = strings.Trim(desc, `""`)
desc = strings.Replace(desc, ":", "", -1)
desc = strings.Replace(desc, "\\n", " ", -1)
desc = strings.Replace(desc, "\\t", " ", -1)
}
hugoMeta := &HugoBlogMeta{
Title: pageMeta.Title,
SubTitle: subTitle,
Excerpt: subTitle,
Description: desc,
Date: pageMeta.CreatedAt,
Image: pageMeta.PageCover,
Tags: pageMeta.Tags,
Category: category,
UrlPath: utils.PathPath(category[len(category)-1]),
Url: pageMeta.PageName,
}
ts, _ := json.Marshal(hugoMeta)
log.Logger.Infof("page hugo meta %s", string(ts))
// rewrite some meta from pageMeta
if v, ok := pageMeta.Meta["title"]; ok {
hugoMeta.Title = v
}
if v, ok := pageMeta.Meta["created_at"]; ok {
hugoMeta.Date = v
}
if v, ok := pageMeta.Meta["desc"]; ok {
hugoMeta.Description = v
}
if v, ok := pageMeta.Meta["url_path"]; ok {
hugoMeta.UrlPath = v
}
if v, ok := pageMeta.Meta["url"]; ok {
hugoMeta.Url = v
}
tpl, err := template.New("hugo").Parse(HugoBlogHeaderTpl)
if err != nil {
return title, err
}
writer := &bytes.Buffer{}
err = tpl.Execute(writer, hugoMeta)
if err != nil {
log.Logger.Error(err)
return title, err
}
writer.WriteString(Head)
//writer.WriteString(titleHtml)
writer.WriteString(Mid)
writer.WriteString(pageContent)
writer.WriteString(Tail)
return writer.String(), err
}
func PageCategory(m *meta.PageMeta) (categories []string) {
for _, comment := range m.Comment {
if strings.HasPrefix(comment, "cate:") {
comment = strings.TrimPrefix(comment, "cate:")
items := strings.Split(comment, "/")
categories = append(categories, items...)
}
}
return
}
func PageTile(page *rod.Page) (title, titleHtml string, err error) {
titleEles, err := page.Elements(".notion-page-block")
if err != nil {
return "", "", fmt.Errorf("page title ele not found %w", err)
}
for i, ele := range titleEles {
switch i {
case 0:
//category, err = ele.Text()
//if err != nil {
// return "", "", fmt.Errorf("page category text not found %w", err)
//}
//
//// filter category
//items := strings.Split(category, "\n")
//if len(items) > 1 {
// category = items[1]
//}
case 1:
title, err = ele.Text()
if err != nil {
return "", "", fmt.Errorf("page title text not found %w", err)
}
titleHtml, err = ele.HTML()
if err != nil {
return "", "", fmt.Errorf("page title text not found %w", err)
}
// append title style
titleHtml = TitleStyle + titleHtml + "</div>"
}
}
return
}
func PageContent(pageId string, page *rod.Page) (content string, err error) {
ele, err := page.Element(".notion-page-content")
if err != nil {
return "", nil
}
content = ele.MustHTML()
// adjust .notion-page-content style is first style
content = strings.Replace(content, "padding-bottom: 30vh;", "padding-bottom: 10vh;", 1)
content = strings.Replace(content, "padding-left: calc(96px + env(safe-area-inset-left));", "padding-left: 5%;", 1)
content = strings.Replace(content, "padding-right: calc(96px + env(safe-area-inset-right));", "padding-right: 5%; margin: auto;", 1)
content = strings.Replace(content, "padding-bottom: 30vh;", "padding-bottom: 10vh;", 1)
// content filter
content = strings.Replace(content, `contenteditable="true"`, "", -1)
// convert local image to notion cdn image
content = strings.Replace(content, `img src="/image`, `img src="https://www.notion.so/image`, -1)
// replace html anchor
content = strings.Replace(content, `data-block-id`, "id", -1)
// remove crossorigin
content = strings.Replace(content, `crossorigin="anonymous"`, "", -1)
// match href="/20ecc4449a3843e6bea9324c3328241e#026465ad4766428784aa1163c89432f1" and replace to href="#026465ad-47664-28784aa11-63c89432f1"
regStr := "href=\"/" + pageId + `#[\w]+"`
reg := regexp.MustCompile(regStr)
content = reg.ReplaceAllStringFunc(content, func(s string) string {
if items := strings.Split(s, "#"); len(items) == 2 {
return `href="` + "#" + utils.PageUuid(items[1]) + `"`
}
return s
})
return
}
func PageDesc(page *rod.Page) (content string, err error) {
ele, err := page.Element(".notion-page-content")
if err != nil {
return "", nil
}
content, err = ele.Text()
return
}
func HugoPageUrl(title string, pageId string) string {
var tk, err = bdfanyi.Gtk()
if err != nil {
log.Logger.Error(err)
return pageId
}
options := bdfanyi.NewOptions(bdfanyi.ZH, bdfanyi.EN, tk, "")
r, err := bdfanyi.Do(title, options)
if err != nil {
log.Logger.Error(err)
return pageId
}
return r.TransResult.Data[0].Dst
}