mirror of
https://github.com/go-shiori/shiori.git
synced 2025-01-16 21:09:44 +08:00
Fix import not working
This commit is contained in:
parent
3b8ce6ce0d
commit
259d1b96ed
1 changed files with 47 additions and 36 deletions
|
@ -40,6 +40,15 @@ func init() {
|
|||
rootCmd.AddCommand(importCmd)
|
||||
}
|
||||
|
||||
func printTagName(s *goquery.Selection) string {
|
||||
tags := []string{}
|
||||
for _, nd := range s.Nodes {
|
||||
tags = append(tags, nd.Data)
|
||||
}
|
||||
|
||||
return strings.Join(tags, ",")
|
||||
}
|
||||
|
||||
func importBookmarks(pth string, generateTag bool) error {
|
||||
// Open file
|
||||
srcFile, err := os.Open(pth)
|
||||
|
@ -54,19 +63,14 @@ func importBookmarks(pth string, generateTag bool) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// Fetch each bookmark categories
|
||||
// Loop each bookmark item
|
||||
bookmarks := []model.Bookmark{}
|
||||
doc.Find("body>dl>dt").Each(func(_ int, el *goquery.Selection) {
|
||||
// Create category title
|
||||
category := el.Find("h3").First().Text()
|
||||
category = normalizeSpace(category)
|
||||
category = strings.ToLower(category)
|
||||
category = strings.Replace(category, " ", "-", -1)
|
||||
doc.Find("dt>a").Each(func(_ int, a *goquery.Selection) {
|
||||
// Get related elements
|
||||
dt := a.Parent()
|
||||
dl := dt.Parent()
|
||||
|
||||
// Fetch all link in this categories
|
||||
el.Find("dl>dt").Each(func(_ int, dt *goquery.Selection) {
|
||||
// Get bookmark link
|
||||
a := dt.Find("a").First()
|
||||
// Get metadata
|
||||
title := a.Text()
|
||||
url, _ := a.Attr("href")
|
||||
strModified, _ := a.Attr("last_modified")
|
||||
|
@ -75,28 +79,35 @@ func importBookmarks(pth string, generateTag bool) error {
|
|||
|
||||
// Get bookmark excerpt
|
||||
excerpt := ""
|
||||
if nxt := dt.Next(); nxt.Is("dd") {
|
||||
excerpt = nxt.Text()
|
||||
if dd := dt.Next(); dd.Is("dd") {
|
||||
excerpt = dd.Text()
|
||||
}
|
||||
|
||||
// Create bookmark item
|
||||
// Get category name for this bookmark
|
||||
category := ""
|
||||
if dtCategory := dl.Prev(); dtCategory.Is("h3") {
|
||||
category = dtCategory.Text()
|
||||
category = normalizeSpace(category)
|
||||
category = strings.ToLower(category)
|
||||
category = strings.Replace(category, " ", "-", -1)
|
||||
}
|
||||
|
||||
tags := []model.Tag{}
|
||||
if category != "" && generateTag {
|
||||
tags = []model.Tag{{Name: category}}
|
||||
}
|
||||
|
||||
// Add item to list
|
||||
bookmark := model.Bookmark{
|
||||
URL: url,
|
||||
Title: normalizeSpace(title),
|
||||
Excerpt: normalizeSpace(excerpt),
|
||||
Modified: modified.Format("2006-01-02 15:04:05"),
|
||||
Tags: []model.Tag{},
|
||||
}
|
||||
|
||||
if generateTag {
|
||||
bookmark.Tags = []model.Tag{
|
||||
{Name: category},
|
||||
}
|
||||
Tags: tags,
|
||||
}
|
||||
|
||||
bookmarks = append(bookmarks, bookmark)
|
||||
})
|
||||
})
|
||||
|
||||
// Save bookmarks to database
|
||||
for _, book := range bookmarks {
|
||||
|
|
Loading…
Reference in a new issue