ReplaceIdentify 重命名文件文档标识之后,要重新替换内容中的旧文档链接为新的文档链接。
(bookId int, oldIdentify, newIdentify string)
| 94 | |
| 95 | // ReplaceIdentify 重命名文件文档标识之后,要重新替换内容中的旧文档链接为新的文档链接。 |
| 96 | func (m *Document) ReplaceIdentify(bookId int, oldIdentify, newIdentify string) { |
| 97 | // 1. 查找书籍的所有文档 |
| 98 | // 2. 替换存在这些链接的文档 |
| 99 | var ( |
| 100 | o = orm.NewOrm() |
| 101 | replaces []string |
| 102 | docs []Document |
| 103 | ds = &DocumentStore{} |
| 104 | ) |
| 105 | |
| 106 | linkFmt := []string{ |
| 107 | "]($%s ", // 如 [xxx]($aaa.md "aaaa") |
| 108 | "]($%s)", // 如 [xxx]($aaa.md) |
| 109 | "]($%s#", // 如 [xxx]($aaa.md#xxx) |
| 110 | "href=\"$%s\"", // href="$aaa.md" |
| 111 | "href=\"$%s#", // href="$aaa.md#xxx |
| 112 | } |
| 113 | |
| 114 | for _, link := range linkFmt { |
| 115 | replaces = append(replaces, fmt.Sprintf(link, oldIdentify), fmt.Sprintf(link, newIdentify)) |
| 116 | } |
| 117 | replacer := strings.NewReplacer(replaces...) |
| 118 | |
| 119 | o.QueryTable(m).Filter("book_id", bookId).Limit(100000).All(&docs, "document_id") |
| 120 | for _, doc := range docs { |
| 121 | o.QueryTable(ds).Filter("document_id", doc.DocumentId).One(ds, "document_id", "markdown") |
| 122 | if strings.Count(ds.Markdown, "$"+oldIdentify) > 0 { |
| 123 | ds.Markdown = replacer.Replace(ds.Markdown) |
| 124 | o.Update(ds, "document_id", "markdown") |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | //插入和更新文档. |
| 130 | //存在文档id或者文档标识,则表示更新文档内容 |