1、当导出Markdown时,如果当前文档没有内容,则将文档标题作为内容

2、修复导入文档时链接处理错误的BUG
pull/244/head
Minho 2018-03-27 00:55:11 +08:00
parent 95408e4b0d
commit bf1b38a36e
2 changed files with 44 additions and 40 deletions

View File

@ -615,9 +615,9 @@ func (book *Book) ImportBook(zipPath string) error {
filetil.CopyFile(linkPath, dstPath) filetil.CopyFile(linkPath, dstPath)
tempLink := strings.TrimPrefix(strings.Replace(dstPath, "\\", "/", -1), strings.Replace(conf.WorkingDirectory, "\\", "/", -1)) tempLink := conf.BaseUrl + strings.TrimPrefix(strings.Replace(dstPath, "\\", "/", -1), strings.Replace(conf.WorkingDirectory, "\\", "/", -1))
link = strings.TrimSuffix(tempLink, originalLink+")") + link + ")" link = strings.TrimSuffix(link, originalLink+")") + tempLink + ")"
} }
} }

View File

@ -451,12 +451,13 @@ func exportMarkdown(p string, parentId int, bookId int,baseDir string) error {
dirPath := filepath.Dir(docPath) dirPath := filepath.Dir(docPath)
os.MkdirAll(dirPath, 0766) os.MkdirAll(dirPath, 0766)
markdown := doc.Markdown
//如果当前文档不为空
if strings.TrimSpace(doc.Markdown) != "" {
re := regexp.MustCompile(`!\[(.*?)\]\((.*?)\)`) re := regexp.MustCompile(`!\[(.*?)\]\((.*?)\)`)
//处理文档中图片 //处理文档中图片
markdown := re.ReplaceAllStringFunc(doc.Markdown, func(image string) string { markdown = re.ReplaceAllStringFunc(doc.Markdown, func(image string) string {
images := re.FindAllSubmatch([]byte(image), -1) images := re.FindAllSubmatch([]byte(image), -1)
if len(images) <= 0 || len(images[0]) < 3 { if len(images) <= 0 || len(images[0]) < 3 {
return image return image
@ -476,8 +477,8 @@ func exportMarkdown(p string, parentId int, bookId int,baseDir string) error {
imageUrl = "/" + imageUrl imageUrl = "/" + imageUrl
} }
} }
}else if strings.HasPrefix(imageUrl,"/"){ } else if strings.HasPrefix(imageUrl, "/") {
filetil.CopyFile(filepath.Join(conf.WorkingDirectory,imageUrl),filepath.Join(baseDir,imageUrl)) filetil.CopyFile(filepath.Join(conf.WorkingDirectory, imageUrl), filepath.Join(baseDir, imageUrl))
} }
imageUrl = strings.Replace(strings.TrimSuffix(image, originalImageUrl+")")+imageUrl+")", "\\", "/", -1) imageUrl = strings.Replace(strings.TrimSuffix(image, originalImageUrl+")")+imageUrl+")", "\\", "/", -1)
@ -492,12 +493,15 @@ func exportMarkdown(p string, parentId int, bookId int,baseDir string) error {
if len(links) > 0 && len(links[0]) >= 3 { if len(links) > 0 && len(links[0]) >= 3 {
originalLink := links[0][2] originalLink := links[0][2]
link = strings.TrimSuffix(link, originalLink+")") + strings.TrimPrefix(originalLink,conf.BaseUrl) + ")" link = strings.TrimSuffix(link, originalLink+")") + strings.TrimPrefix(originalLink, conf.BaseUrl) + ")"
} }
return link return link
}) })
}else{
markdown = "# " + doc.DocumentName + "\n"
}
if err := ioutil.WriteFile(docPath, []byte(markdown), 0644); err != nil { if err := ioutil.WriteFile(docPath, []byte(markdown), 0644); err != nil {
beego.Error("导出Markdown失败=>", err) beego.Error("导出Markdown失败=>", err)
return err return err