增加文章内容外链新窗口打开

This commit is contained in:
lifei6671 2018-07-24 17:54:24 +08:00
parent d0e6eff4b9
commit 4f0f432c34
4 changed files with 86 additions and 44 deletions

View File

@ -7,6 +7,9 @@ import (
"github.com/astaxie/beego"
"github.com/lifei6671/mindoc/cache"
"fmt"
"github.com/PuerkitoBio/goquery"
"strings"
"github.com/qiniu/x/bytes.v7"
)
//博文表
@ -224,6 +227,31 @@ func (b *Blog) Save(cols ...string) error {
b.Created = time.Now()
_,err = o.Insert(b)
}
//如果保存成功,则在后台处理文章中存在的外链问题
if err == nil {
go func(blog *Blog) {
bufio := bytes.NewReader([]byte(b.BlogRelease))
//解析文档中非本站的链接,并设置为新窗口打开
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://") {
//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 {
b.BlogRelease = html
}
}
}
}
})
}
o.Update(blog)
}(b)
}
return err
}

View File

@ -1,6 +1,8 @@
/*************表格样式****************/
.markdown-body{
font-family: "Montserrat", sans-serif !important;
-webkit-backface-visibility: hidden;
font-family: Helvetica, -apple-system, BlinkMacSystemFont, "Montserrat","Helvetica Neue",Helvetica,"Segoe UI",Arial,freesans,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Microsoft Yahei","Helvetica Neue",Helvetica;
}
.editormd-preview-container table {
margin-top: 0;

View File

@ -248,21 +248,31 @@ function formatBytes($size) {
function uploadImage($id,$callback) {
/** 粘贴上传图片 **/
document.getElementById($id).addEventListener('paste', function(e) {
if(e.clipboardData && e.clipboardData.items) {
var clipboard = e.clipboardData;
for (var i = 0, len = clipboard.items.length; i < len; i++) {
if (clipboard.items[i].kind === 'file' || clipboard.items[i].type.indexOf('image') > -1) {
var imageFile = clipboard.items[i].getAsFile();
var fileName = Date.parse(new Date());
var fileName = String((new Date()).valueOf());
switch (imageFile.type) {
case "image/png" : fileName += ".png";break;
case "image/jpg" : fileName += ".jpg";break
case "image/jpeg" : fileName += ".jpeg";break;
case "image/gif" : fileName += ".gif";break;
default : layer.msg("不支持的图片格式");return;
case "image/png" :
fileName += ".png";
break;
case "image/jpg" :
fileName += ".jpg";
break;
case "image/jpeg" :
fileName += ".jpeg";
break;
case "image/gif" :
fileName += ".gif";
break;
default :
layer.msg("不支持的图片格式");
return;
}
var form = new FormData();
@ -297,6 +307,7 @@ function uploadImage($id,$callback) {
e.preventDefault();
}
}
}
});
}

View File

@ -49,6 +49,7 @@
color: #666666;
vertical-align: middle;
}
.blog-footer{
margin: 25px auto;
/*border-top: 1px solid #E5E5E5;*/