Skip to content

Commit 6f3a126

Browse files
committed
refact: Refactor the code
1: add boook mark shortcodes 2: auto copy shrotcodes from notion-site 3: auto copy shrotcodes static from notion-site
1 parent 8a9f847 commit 6f3a126

34 files changed

+30865
-35
lines changed

cmd/root.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ package cmd
22

33
import (
44
"fmt"
5-
"github.com/pkwenda/notion-site/pkg"
6-
"log"
7-
"os"
8-
95
"github.com/joho/godotenv"
6+
"github.com/pkwenda/notion-site/pkg"
107
"github.com/spf13/cobra"
118
"github.com/spf13/viper"
9+
"log"
10+
"os"
1211
)
1312

1413
var cfgFile string
@@ -26,6 +25,7 @@ var rootCmd = &cobra.Command{
2625
}
2726
api := pkg.NewAPI()
2827
files := pkg.NewFiles(config)
28+
files.CopyShortCodes(config.HomePath)
2929
tm := pkg.New()
3030
caches := pkg.NewNotionCaches()
3131
ns := pkg.NewNotionSite(api, tm, files, config, caches)

notion-site.yaml

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
1-
#notion:
2-
# databaseId: 2bd00e5dfff3449ba81e0142f8af9bbb
3-
# filterProp: Status
4-
# filterValue:
5-
# - Finished
6-
# - Published
7-
# - RePublish
8-
# publishedValue: Published
9-
#markdown:
10-
# shortcodeSyntax: hugo
11-
# homePath: ../compose/exampleSiteDev
12-
13-
14-
#blog
151
notion:
16-
databaseId: df7fb0e4e0114268b973f9d3e9a39982
2+
databaseId: 2bd00e5dfff3449ba81e0142f8af9bbb
173
filterProp: Status
184
filterValue:
195
- Finished
206
- Published
217
- RePublish
228
publishedValue: Published
239
markdown:
24-
shortcodeSyntax: hugo
25-
homePath: ../notion-hugo-website-builder
10+
homePath: ../notion-site-doc
11+
12+
13+
#blog
14+
#notion:
15+
# databaseId: df7fb0e4e0114268b973f9d3e9a39982
16+
# filterProp: Status
17+
# filterValue:
18+
# - Finished
19+
# - Published
20+
# - RePublish
21+
# publishedValue: Published
22+
#markdown:
23+
# homePath: ../notion-hugo-website-builder

pkg/config.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ type Notion struct {
1616
}
1717

1818
type Markdown struct {
19-
ShortcodeSyntax string `yaml:"shortcodeSyntax"` // hugo,hexo,vuepress
2019
HomePath string `yaml:"homePath"`
2120
ImagePublicLink string `yaml:"imagePublicLink"`
2221

@@ -39,8 +38,7 @@ func DefaultConfigInit() error {
3938
PublishedValue: "Published",
4039
},
4140
Markdown: Markdown{
42-
ShortcodeSyntax: "hugo",
43-
HomePath: "",
41+
HomePath: "",
4442
},
4543
}
4644
out, err := yaml.Marshal(defaultCfg)

pkg/file.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,57 @@ func (files *Files) saveTo(reader io.Reader, rawURL, distDir string) (string, er
223223
_, err = io.Copy(out, reader)
224224
return filename, err
225225
}
226+
227+
func (files *Files) CopyShortCodes(home string) {
228+
src := filepath.Join("pkg", "shortcodes")
229+
dst := filepath.Join(home, "layouts", "shortcodes")
230+
err := os.RemoveAll(dst)
231+
if err != nil {
232+
fmt.Errorf("couldn't del folder: %s", err)
233+
}
234+
files.copyDir(src, dst)
235+
src = filepath.Join("pkg", "shortcodes", "static")
236+
dst = filepath.Join(home, "static", "notion-site")
237+
err = os.RemoveAll(dst)
238+
if err != nil {
239+
fmt.Errorf("couldn't del folder: %s", err)
240+
}
241+
files.copyDir(src, dst)
242+
}
243+
244+
func (files *Files) copyDir(src, dst string) error {
245+
_, err := os.Stat(src)
246+
if err != nil {
247+
return err
248+
}
249+
err = files.mkdirPath(dst)
250+
if err != nil {
251+
return err
252+
}
253+
err = filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
254+
if err != nil {
255+
return err
256+
}
257+
if info.IsDir() {
258+
return nil
259+
}
260+
dstPath := filepath.Join(dst, path[len(src):])
261+
return files.copyFile(path, dstPath)
262+
})
263+
return err
264+
}
265+
266+
func (files *Files) copyFile(src, dst string) error {
267+
srcFile, err := os.Open(src)
268+
if err != nil {
269+
return err
270+
}
271+
defer srcFile.Close()
272+
dstFile, err := os.Create(dst)
273+
if err != nil {
274+
return err
275+
}
276+
defer dstFile.Close()
277+
_, err = io.Copy(dstFile, srcFile)
278+
return err
279+
}

pkg/generator.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func Run(ns *NotionSite) error {
4646

4747
func generate(ns *NotionSite, page notion.Page, blocks []notion.Block) error {
4848
// Generate markdown content to the file
49-
initNotionSite(ns, page)
49+
initNotionSite(ns, page, blocks)
5050

5151
if ns.api.CheckHasChildDataBase(blocks, func(b bool, id string) {
5252
// cache child database block id
@@ -78,16 +78,16 @@ func generate(ns *NotionSite, page notion.Page, blocks []notion.Block) error {
7878
}
7979

8080
// todo edit frontMatter
81-
if ns.config.Markdown.ShortcodeSyntax != "" {
82-
ns.tm.EnableExtendedSyntax(ns.config.Markdown.ShortcodeSyntax)
83-
}
81+
//if ns.config.Markdown.ShortcodeSyntax != "" {
82+
// ns.tm.EnableExtendedSyntax(ns.config.Markdown.ShortcodeSyntax)
83+
//}
8484

8585
//// todo how to support mention feature ???
86-
//ns.currentBlocks, _ = syncMentionBlocks(ns.api.Client, blocks)
86+
8787
return ns.tm.GenerateTo(ns)
8888
}
8989

90-
func initNotionSite(ns *NotionSite, page notion.Page) {
90+
func initNotionSite(ns *NotionSite, page notion.Page, blocks []notion.Block) {
9191
// set current origin page
9292
ns.currentPage = page
9393
// set current notion page prop
@@ -96,7 +96,7 @@ func initNotionSite(ns *NotionSite, page notion.Page) {
9696
// set notion site files info
9797
ns.tm.NotionProps = ns.currentPageProp
9898
ns.tm.Files = ns.files
99-
99+
ns.currentBlocks = blocks
100100
}
101101

102102
func processDatabase(ns *NotionSite, id string) error {

pkg/markdown.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
var mdTemplatesFS embed.FS
2121

2222
var (
23-
extendedSyntaxBlocks = []any{reflect.TypeOf(&notion.BookmarkBlock{}), reflect.TypeOf(&notion.CalloutBlock{})}
23+
extendedSyntaxBlocks = []any{reflect.TypeOf(&notion.CalloutBlock{})}
2424
blockTypeInExtendedSyntaxBlocks = func(bType any) bool {
2525
for _, blockType := range extendedSyntaxBlocks {
2626
if blockType == bType {

pkg/shortcodes.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ func (tm *ToMarkdown) injectBookmarkInfo(bookmark *notion.BookmarkBlock, extra *
2323
break
2424
}
2525
}
26+
(*extra)["Url"] = og.URL
2627
(*extra)["Title"] = og.Title
2728
(*extra)["Description"] = og.Description
29+
(*extra)["Icon"] = og.Favicon
2830
return nil
2931
}
3032

File renamed without changes.

layouts/hugo/shortcodes/bilibili.html renamed to pkg/shortcodes/bilibili.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
{{ end }}
1414

1515
<div class="video-wrapper">
16-
<iframe style="position: absolute; top: 0; left: 100; right:100; width: 80%; height: 80%; border:0;"
16+
<iframe style="position: absolute; top: 0; width: 100%; height: 100%; border:0;"
1717
src="https://player.bilibili.com/player.html?{{ $basicQuery | safeURL }}&{{ $videoQuery | safeURL }}"
1818
scrolling="no"
1919
frameborder="no"

pkg/shortcodes/bookmark.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
<link rel="preload" href="/notion-site/bookmark/bookmark.css" as="style">
3+
<link rel="stylesheet" href="/notion-site/bookmark/bookmark.css" data-n-g="">
4+
<a href="{{ .Get "url" }}" target="_blank" rel="noreferrer" class="">
5+
<article class="flex justify-between h-40 rounded border border-gray-400 border-solid">
6+
<div class="flex flex-col justify-between p-5 hover:bg-gray-100 w-3/5">
7+
<h3 class="text-2xl truncate">{{ .Get "title" }} </h3>
8+
<p class="overflow-hidden h-12 text-base text-gray-500">
9+
{{ .Get "des" }}
10+
</p>
11+
<div class="flex items-center">
12+
<img src="{{ .Get "icon" }}" class="h-6" alt="">
13+
<p class="text-base truncate ml-2">
14+
{{ .Get "url" }}
15+
</p>
16+
</div>
17+
</div>
18+
<div class="w-2/5 h-full rounded">
19+
<img src="{{ .Get "image" }}" class="object-cover w-full h-full" alt="">
20+
</div>
21+
</article>
22+
</a>

0 commit comments

Comments
 (0)