Explorar o código

fix:优化TOC功能

lifei6671 %!s(int64=7) %!d(string=hai) anos
pai
achega
14e1160d4d

+ 0 - 2
controllers/DocumentController.go

@@ -63,7 +63,6 @@ func (c *DocumentController) Index() {
 	if bookResult.IsUseFirstDocument {
 		doc, err := bookResult.FindFirstDocumentByBookId(bookResult.BookId)
 		if err == nil {
-			doc.AppendInfo()
 			selected = doc.DocumentId
 			c.Data["Title"] = doc.DocumentName
 			c.Data["Content"] = template.HTML(doc.Release)
@@ -177,7 +176,6 @@ func (c *DocumentController) Read() {
 	if doc.ModifyTime != doc.CreateTime {
 		docInfo += ";更新于 "
 		docInfo += doc.ModifyTime.Local().Format("2006-01-02 15:04")
-		doc.AppendInfo()
 	}
 
 	if c.IsAjax() {

+ 42 - 37
models/DocumentModel.go

@@ -243,29 +243,27 @@ func (item *Document) ReleaseContent() error {
 
 	bookId := item.BookId
 
+	var docQuery *goquery.Document
+	var err error
+
 	if item.Content != "" {
 		item.Release = item.Content
 		bufio := bytes.NewReader([]byte(item.Content))
 		//解析文档中非本站的链接,并设置为新窗口打开
-		if content, err := goquery.NewDocumentFromReader(bufio); err == nil {
-
-			content.Find("a").Each(func(i int, contentSelection *goquery.Selection) {
+		if docQuery, err = goquery.NewDocumentFromReader(bufio); err == nil {
+			docQuery.Find("a").Each(func(i int, contentSelection *goquery.Selection) {
 				if src, ok := contentSelection.Attr("href"); ok {
 					if strings.HasPrefix(src, "http://") || strings.HasPrefix(src, "https://") {
-						//beego.Info(src,conf.BaseUrl,strings.HasPrefix(src,conf.BaseUrl))
 						if conf.BaseUrl != "" && !strings.HasPrefix(src, conf.BaseUrl) {
 							contentSelection.SetAttr("target", "_blank")
-							if html, err := content.Html(); err == nil {
-								item.Release = strings.TrimSuffix(strings.TrimPrefix(strings.TrimSpace(html),"<html><head></head><body>"),"</body></html>")
-							}
 						}
 					}
-
 				}
 			})
 		}
 	}
 
+	//处理附件
 	attachList, err := NewAttachment().FindListByDocumentId(item.DocumentId)
 	if err == nil && len(attachList) > 0 {
 		content := bytes.NewBufferString("<div class=\"attach-list\"><strong>附件</strong><ul>")
@@ -278,8 +276,43 @@ func (item *Document) ReleaseContent() error {
 			content.WriteString(li)
 		}
 		content.WriteString("</ul></div>")
-		item.Release += content.String()
+		if docQuery == nil {
+			docQuery ,err = goquery.NewDocumentFromReader(content);
+		}else {
+			if selector := docQuery.Find("div.markdown-article").First(); selector.Size() > 0{
+				selector.AppendHtml(content.String())
+			}else{
+				docQuery.Find("article.markdown-article-inner").First().AppendHtml(content.String())
+			}
+		}
 	}
+
+	//处理格式化后的文档内容
+	if docQuery != nil {
+		//处理文档结尾信息
+		docCreator, err := NewMember().Find(item.MemberId,"real_name","account")
+		release := "<div class=\"wiki-bottom\">文档更新时间: " + item.ModifyTime.Local().Format("2006-01-02 15:04") + " &nbsp;&nbsp;作者:"
+		if err == nil && docCreator != nil {
+			if docCreator.RealName != "" {
+				release += docCreator.RealName
+			} else {
+				release += docCreator.Account
+			}
+		}
+		release += "</div>"
+
+		if selector := docQuery.Find("div.markdown-article").First() ; selector.Size() > 0{
+			beego.Info(selector)
+			selector.AppendHtml(release)
+		}else{
+			docQuery.Find("article.markdown-article-inner").First().AppendHtml(release)
+		}
+
+		if html, err := docQuery.Html(); err == nil {
+			item.Release = strings.TrimSuffix(strings.TrimPrefix(strings.TrimSpace(html), "<html><head></head><body>"), "</body></html>")
+		}
+	}
+
 	_, err = o.Update(item, "release")
 	if err != nil {
 		beego.Error(fmt.Sprintf("发布失败 => %+v", item), err)
@@ -298,32 +331,4 @@ func (item *Document) ReleaseContent() error {
 		}
 	}
 	return nil
-}
-
-//追加一些文档信息.
-func (doc *Document) AppendInfo() *Document {
-
-	docCreator, err := NewMember().Find(doc.MemberId,"real_name","account")
-
-	doc.Release = strings.TrimSuffix(strings.TrimPrefix(strings.TrimSpace(doc.Release),"<html><head></head><body>"),"</body></html>")
-	suffix := ""
-	if doc.Release != "" {
-		beego.Info(doc.Release)
-		if strings.HasPrefix(doc.Release,"<div class=\"markdown-toc") {
-
-			doc.Release = strings.TrimSuffix(doc.Release,"</div>")
-			suffix = "</div>"
-		}
-		doc.Release += "<div class=\"wiki-bottom\">文档更新时间: " + doc.ModifyTime.Local().Format("2006-01-02 15:04") + " &nbsp;&nbsp;作者:";
-		if err == nil && docCreator != nil {
-			if docCreator.RealName != "" {
-				doc.Release += docCreator.RealName
-			} else {
-				doc.Release += docCreator.Account
-			}
-		}
-		doc.Release += "</div>" + suffix
-	}
-
-	return doc
 }

+ 1 - 1
static/css/markdown.preview.css

@@ -57,7 +57,7 @@
     border-right: 2px solid #25b864;
 }
 
-.article-body .markdown-article,.article-body .attach-list{
+.article-body .markdown-article{
     margin-right: 200px;
 }
 .markdown-toc-list .directory-item {

+ 1 - 0
static/editor.md/editormd.js

@@ -3981,6 +3981,7 @@
             html = new String(html);
         }
 
+        html = '<article class="markdown-article-inner">' + html + "</article>";
         if (typeof filters !== "string") {
             return html;
         }

+ 30 - 5
static/js/kancloud.js

@@ -149,11 +149,36 @@ $(function () {
         $('.manual-right').animate({ scrollTop: '0px' }, 200);
     });
     $(".manual-right").scroll(function () {
-        var top = $(".manual-right").scrollTop();
-        if (top > 100) {
-            $(".view-backtop").addClass("active");
-        } else {
-            $(".view-backtop").removeClass("active");
+        try {
+            var top = $(".manual-right").scrollTop();
+            if (top > 100) {
+                $(".view-backtop").addClass("active");
+            } else {
+                $(".view-backtop").removeClass("active");
+            }
+        }catch (e) {
+            console.log(e);
+        }
+        try{
+            var scrollTop = $(document).scrollTop();
+            var oItem = $(".markdown-heading").find(".reference-link");
+            var oName = "";
+            $.each(oItem,function(){
+                var oneItem = $(this);
+                var offsetTop = oneItem.offset().top;
+                if(offsetTop-scrollTop < 200){
+                    oName = "#" + oneItem.attr("name");
+                }
+            });
+            $(".markdown-toc-list a").each(function () {
+                if(oName === $(this).attr("href")) {
+                    $(this).parents("li").addClass("directory-item-active");
+                }else{
+                    $(this).parents("li").removeClass("directory-item-active");
+                }
+            });
+        }catch (e) {
+            console.log(e);
         }
     });
     window.isFullScreen = false;

+ 0 - 21
views/document/default_read.tpl

@@ -288,27 +288,6 @@ $(function () {
             return $(body).highlight(window.keyword);
         });
     });
-    $(".manual-right").scroll(function(){
-        var scrollTop = $(document).scrollTop();
-        var oItem = $(".markdown-heading").find(".reference-link");
-        var oName = "";
-        $.each(oItem,function(){
-            var oneItem = $(this);
-            var offsetTop = oneItem.offset().top;
-            if(offsetTop-scrollTop < 200){
-                oName = "#" + oneItem.attr("name");
-            }
-        });
-        console.log(oName);
-        $(".markdown-toc-list a").each(function () {
-            if(oName === $(this).attr("href")) {
-                $(this).parents("li").addClass("directory-item-active");
-            }else{
-                $(this).parents("li").removeClass("directory-item-active");
-            }
-        });
-    });
-
 });
 </script>
 {{.Scripts}}