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

Method replaceLinks

controllers/BaseController.go:477–531  ·  view source on GitHub ↗

替换链接 如果是summary,则根据这个进行排序调整

(bookIdentify string, docHtml string, isSummary ...bool)

Source from the content-addressed store, hash-verified

475// 替换链接
476// 如果是summary,则根据这个进行排序调整
477func (this *BaseController) replaceLinks(bookIdentify string, docHtml string, isSummary ...bool) string {
478 var (
479 book models.Book
480 docs []models.Document
481 o = orm.NewOrm()
482 )
483
484 o.QueryTable("md_books").Filter("identify", bookIdentify).One(&book, "book_id")
485 if book.BookId > 0 {
486 o.QueryTable("md_documents").Filter("book_id", book.BookId).Limit(5000).All(&docs, "identify", "document_id")
487 if len(docs) > 0 {
488 Links := make(map[string]string)
489 for _, doc := range docs {
490 idStr := strconv.Itoa(doc.DocumentId)
491 if len(doc.Identify) > 0 {
492 Links["$"+strings.ToLower(doc.Identify)] = beego.URLFor("DocumentController.Read", ":key", bookIdentify, ":id", doc.Identify) + "||" + idStr
493 }
494 if doc.DocumentId > 0 {
495 Links["$"+strconv.Itoa(doc.DocumentId)] = beego.URLFor("DocumentController.Read", ":key", bookIdentify, ":id", doc.DocumentId) + "||" + idStr
496 }
497 }
498
499 //替换文档内容中的链接
500 if gq, err := goquery.NewDocumentFromReader(strings.NewReader(docHtml)); err == nil {
501 gq.Find("a").Each(func(i int, selection *goquery.Selection) {
502 if href, ok := selection.Attr("href"); ok && strings.HasPrefix(href, "$") {
503 if slice := strings.Split(href, "#"); len(slice) > 1 {
504 if newHref, ok := Links[strings.ToLower(slice[0])]; ok {
505 arr := strings.Split(newHref, "||") //整理的arr数组长度,肯定为2,所以不做数组长度判断
506 selection.SetAttr("href", arr[0]+"#"+strings.Join(slice[1:], "#"))
507 selection.SetAttr("data-pid", arr[1])
508 }
509 } else {
510 if newHref, ok := Links[strings.ToLower(href)]; ok {
511 arr := strings.Split(newHref, "||") //整理的arr数组长度,肯定为2,所以不做数组长度判断
512 selection.SetAttr("href", arr[0])
513 selection.SetAttr("data-pid", arr[1])
514 }
515 }
516 }
517 })
518
519 if newHtml, err := gq.Find("body").Html(); err == nil {
520 docHtml = newHtml
521 if len(isSummary) > 0 && isSummary[0] == true { //更新排序
522 docHtml = this.sortBySummary(bookIdentify, docHtml, book.BookId) //更新排序
523 }
524 }
525 } else {
526 beego.Error(err.Error())
527 }
528 }
529 }
530 return docHtml
531}
532
533// 内容采集
534func (this *BaseController) Crawl() {

Callers 3

sortBySummaryMethod · 0.95
ContentMethod · 0.80
RenderMarkdownMethod · 0.80

Calls 3

sortBySummaryMethod · 0.95
AllMethod · 0.45
FindMethod · 0.45

Tested by

no test coverage detected