MCPcopy Index your code
hub / github.com/TruthHun/BookStack / Export2Markdown

Method Export2Markdown

models/book.go:867–989  ·  view source on GitHub ↗

Export2Markdown 将书籍导出markdown

(identify string)

Source from the content-addressed store, hash-verified

865
866// Export2Markdown 将书籍导出markdown
867func (m *Book) Export2Markdown(identify string) (path string, err error) {
868 var (
869 book *Book
870 exportDir = fmt.Sprintf("uploads/export/%v", identify)
871 attachPrefix string
872 exportAttachPrefix = "../attachments/"
873 docs []Document
874 ds []DocumentStore
875 docIds []interface{}
876 docMap = make(map[int]Document)
877 o = orm.NewOrm()
878 isOSSProject = utils.StoreType == utils.StoreOss
879 cover = ""
880 replaces []string
881 )
882
883 path = fmt.Sprintf("uploads/export/%v.zip", identify)
884 if book, err = m.FindByIdentify(identify); err != nil {
885 beego.Error(err)
886 return
887 }
888
889 os.MkdirAll(filepath.Join(exportDir, "docs"), os.ModePerm)
890 // 最后删除导出目录
891 defer func() {
892 os.RemoveAll(exportDir)
893 }()
894
895 attachPrefix = fmt.Sprintf("/uploads/projects/%s/", identify)
896 if isOSSProject {
897 attachPrefix = fmt.Sprintf("/projects/%s/", identify)
898 }
899
900 o.QueryTable(NewDocument()).Filter("book_id", book.BookId).Limit(100000).All(&docs, "document_id", "document_name", "identify")
901 if len(docs) == 0 {
902 err = errors.New("找不到书籍章节")
903 return
904 }
905
906 ext := ".md"
907 replaces = append(replaces, attachPrefix, exportAttachPrefix)
908 for _, doc := range docs {
909 docIds = append(docIds, doc.DocumentId)
910 identify := doc.Identify
911 id := strconv.Itoa(doc.DocumentId)
912
913 if docExt := strings.ToLower(strings.TrimSpace(filepath.Ext(doc.Identify))); docExt != ".md" {
914 doc.Identify = doc.Identify + ext
915 }
916 docMap[doc.DocumentId] = doc
917 replaces = append(replaces,
918 "]($"+identify, "]("+doc.Identify,
919 "href=\"$"+identify, "href=\""+doc.Identify,
920 "]($"+id, "]("+doc.Identify,
921 "href=\"$"+id, "href=\""+doc.Identify,
922 )
923 }
924

Callers

nothing calls this directly

Calls 11

FindByIdentifyMethod · 0.95
down2localMethod · 0.95
zipMethod · 0.95
ConvertFunction · 0.92
NewDocumentFunction · 0.85
NewDocumentStoreFunction · 0.85
appendFunction · 0.50
AllMethod · 0.45
ReplaceMethod · 0.45
FindMethod · 0.45

Tested by

no test coverage detected