-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotion_test_to_html.go
97 lines (83 loc) · 2.27 KB
/
notion_test_to_html.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
package main
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/kjk/notionapi/caching_downloader"
)
var (
destDir = "netlify_static"
)
func copyCSS() {
src := filepath.Join("www", "css", "main.css")
dst := filepath.Join(destDir, "main.css")
err := copyFile(dst, src)
panicIfErr(err)
}
func createDestDir() {
err := os.MkdirAll(destDir, 0755)
panicIfErr(err)
}
func createNotionDirs() {
err := os.MkdirAll(cacheDir, 0755)
panicIfErr(err)
}
// downloads and html
func testNotionToHTMLOnePage(d *caching_downloader.Downloader, id string) {
//id := "c9bef0f1c8fe40a2bc8b06ace2bd7d8f" // tools page, columns
//id := "0a66e6c0c36f4de49417a47e2c40a87e" // mono-spaced page with toggle, devlog 2018
//id := "484919a1647144c29234447ce408ff6b" // test toggle
//id := "88aee8f43620471aa9dbcad28368174c" // test image and gist
loadTemplates()
createNotionDirs()
createDestDir()
id = normalizeID(id)
article := loadPageAsArticle(d, id)
canonicalURL := netlifyRequestGetFullHost() + article.URL()
model := struct {
AnalyticsCode string
Article *Article
CanonicalURL string
CoverImage string
PageTitle string
TagsDisplay string
HeaderImageURL string
NotionEditURL string
Description string
TwitterShareURL string
FacebookShareURL string
LinkedInShareURL string
}{
AnalyticsCode: analyticsCode,
Article: article,
CanonicalURL: canonicalURL,
CoverImage: article.HeaderImageURL,
PageTitle: article.Title,
Description: article.Description,
TwitterShareURL: makeTwitterShareURL(article),
FacebookShareURL: makeFacebookShareURL(article),
LinkedInShareURL: makeLinkedinShareURL(article),
}
if article.page != nil {
id := normalizeID(article.page.ID)
model.NotionEditURL = "https://notion.so/" + id
}
var buf bytes.Buffer
err := templates.ExecuteTemplate(&buf, tmplArticle, model)
panicIfErr(err)
data := buf.Bytes()
data = bytes.Replace(data, []byte("/css/main.css"), []byte("/main.css"), -1)
path := filepath.Join(destDir, "index.html")
err = ioutil.WriteFile(path, data, 0644)
panicIfErr(err)
copyCSS()
err = os.Chdir(destDir)
panicIfErr(err)
go func() {
time.Sleep(time.Second * 1)
openBrowser("http://localhost:2015")
}()
runCaddy()
}