优化代码高亮

This commit is contained in:
Minho
2018-01-27 13:56:45 +08:00
parent 55c0854be1
commit 538f9c4c19
5 changed files with 46 additions and 4 deletions

View File

@@ -12,6 +12,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"github.com/PuerkitoBio/goquery"
) )
// Document struct. // Document struct.
@@ -136,7 +137,28 @@ func (m *Document) ReleaseContent(bookId int) {
return return
} }
for _, item := range docs { 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) attachList, err := NewAttachment().FindListByDocumentId(item.DocumentId)
if err == nil && len(attachList) > 0 { if err == nil && len(attachList) > 0 {
content := bytes.NewBufferString("<div class=\"attach-list\"><strong>附件</strong><ul>") content := bytes.NewBufferString("<div class=\"attach-list\"><strong>附件</strong><ul>")

View File

@@ -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 () { $(function () {
window.vueApp = new Vue({ window.vueApp = new Vue({
el : "#attachList", el : "#attachList",

View File

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

View File

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

View File

@@ -30,7 +30,7 @@
<link href="{{cdncss "/static/css/markdown.css"}}" rel="stylesheet"> <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/prettify/themes/atelier-estuary-dark.min.css"}}" rel="stylesheet">
<link href="{{cdncss "/static/css/markdown.preview.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="/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/katex/katex.min.css"}}" rel="stylesheet">
<link href="{{cdncss "/static/quill/quill.core.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/quill/quill.icons.js"}}" type="text/javascript"></script>
<script src="{{cdnjs "/static/layer/layer.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/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/editor.js"}}" type="text/javascript"></script>
<script src="{{cdnjs "/static/js/quill.js"}}" type="text/javascript"></script> <script src="{{cdnjs "/static/js/quill.js"}}" type="text/javascript"></script>
<script type="text/javascript"> <script type="text/javascript">
$(function () { $(function () {
hljs.configure({ // optionally configure hljs
languages: ['javascript', 'ruby', 'python']
});
$(".editor-code").on("dblclick",function () { $(".editor-code").on("dblclick",function () {
var code = $(this).html(); var code = $(this).html();
$("#createCodeToolbarModal").find("textarea").val(code); $("#createCodeToolbarModal").find("textarea").val(code);