Forráskód Böngészése

优化代码高亮

Minho 7 éve
szülő
commit
538f9c4c19

+ 23 - 1
models/document.go

@@ -12,6 +12,7 @@ import (
 	"os"
 	"path/filepath"
 	"strconv"
+	"github.com/PuerkitoBio/goquery"
 )
 
 // Document struct.
@@ -136,7 +137,28 @@ func (m *Document) ReleaseContent(bookId int) {
 		return
 	}
 	for _, item := range docs {
-		item.Release = item.Content
+		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 src, ok := contentSelection.Attr("href"); ok{
+						if strings.HasPrefix(src, "http://") || strings.HasPrefix(src,"https://") {
+							if conf.BaseUrl != "" && strings.Index(src,conf.BaseUrl) != 0 {
+								contentSelection.SetAttr("target", "_blank")
+								if html, err := content.Html();err == nil {
+									item.Release = html
+								}
+							}
+						}
+
+					}
+				})
+			}
+		}
+
 		attachList, err := NewAttachment().FindListByDocumentId(item.DocumentId)
 		if err == nil && len(attachList) > 0 {
 			content := bytes.NewBufferString("<div class=\"attach-list\"><strong>附件</strong><ul>")

+ 10 - 0
static/js/editor.js

@@ -300,6 +300,16 @@ function uploadImage($id,$callback) {
     });
 }
 
+/**
+ * 初始化代码高亮
+ */
+function initHighlighting() {
+    $('pre code,pre.ql-syntax').each(function (i, block) {
+        hljs.highlightBlock(block);
+    });
+
+    hljs.initLineNumbersOnLoad();
+}
 $(function () {
     window.vueApp = new Vue({
         el : "#attachList",

+ 4 - 1
static/js/kancloud.js

@@ -65,8 +65,11 @@ function loadDocument($url, $id, $callback) {
     });
 }
 
+/**
+ * 初始化代码高亮
+ */
 function initHighlighting() {
-    $('pre code').each(function (i, block) {
+    $('pre code,pre.ql-syntax').each(function (i, block) {
         hljs.highlightBlock(block);
     });
 

+ 3 - 1
static/js/quill.js

@@ -4,6 +4,7 @@ $(function () {
     window.uploader = null;
     window.editor = new Quill('#docEditor', {
         theme: 'snow',
+        syntax: true,
         modules : {
             toolbar :"#editormd-tools"
         }
@@ -136,6 +137,7 @@ $(function () {
                 window.isLoad = true;
 
                 pushVueLists(res.data.attach);
+                initHighlighting();
 
             }else{
                 layer.msg("文档加载失败");
@@ -149,6 +151,7 @@ $(function () {
     /**
      * 保存文档到服务器
      * @param $is_cover 是否强制覆盖
+     * @param callback
      */
     function saveDocument($is_cover,callback) {
         var index = null;
@@ -156,7 +159,6 @@ $(function () {
 
         var html = window.editor.root.innerHTML;
 
-        console.log(html)
         var content = "";
         if($.trim(html) !== ""){
             content = toMarkdown(html, { gfm: true });

+ 6 - 1
views/document/new_html_edit_template.tpl

@@ -30,7 +30,7 @@
     <link href="{{cdncss "/static/css/markdown.css"}}" rel="stylesheet">
     <link href="{{cdncss "/static/prettify/themes/atelier-estuary-dark.min.css"}}" rel="stylesheet">
     <link href="{{cdncss "/static/css/markdown.preview.css"}}" rel="stylesheet">
-
+    <link href="{{cdncss "/static/highlight/styles/zenburn.css"}}" rel="stylesheet">
     {{/*<link href="/static/bootstrap/plugins/bootstrap-wysiwyg/external/google-code-prettify/prettify.css" rel="stylesheet">*/}}
     <link href="{{cdncss "/static/katex/katex.min.css"}}" rel="stylesheet">
     <link href="{{cdncss "/static/quill/quill.core.css"}}" rel="stylesheet">
@@ -381,10 +381,15 @@
 <script src="{{cdnjs "/static/quill/quill.icons.js"}}" type="text/javascript"></script>
 <script src="{{cdnjs "/static/layer/layer.js"}}" type="text/javascript" ></script>
 <script src="{{cdnjs "/static/js/jquery.form.js"}}" type="text/javascript"></script>
+<script src="{{cdnjs "/static/highlight/highlight.js"}}" type="text/javascript"></script>
+<script src="{{cdnjs "/static/highlight/highlightjs-line-numbers.min.js"}}" type="text/javascript"></script>
 <script src="{{cdnjs "/static/js/editor.js"}}" type="text/javascript"></script>
 <script src="{{cdnjs "/static/js/quill.js"}}" type="text/javascript"></script>
 <script type="text/javascript">
     $(function () {
+        hljs.configure({   // optionally configure hljs
+            languages: ['javascript', 'ruby', 'python']
+        });
         $(".editor-code").on("dblclick",function () {
             var code = $(this).html();
             $("#createCodeToolbarModal").find("textarea").val(code);