2023-06-24 20:39:55 +08:00
// SiYuan - Refactor your thinking
2022-05-26 15:18:53 +08:00
// Copyright (c) 2020-present, b3log.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package util
2023-06-06 11:19:24 +08:00
import (
"strings"
"github.com/88250/lute"
"github.com/PuerkitoBio/goquery"
"github.com/siyuan-note/logging"
)
2022-05-26 15:18:53 +08:00
func NewLute ( ) ( ret * lute . Lute ) {
ret = lute . New ( )
2022-09-16 11:46:45 +08:00
ret . SetTextMark ( true )
2022-05-26 15:18:53 +08:00
ret . SetProtyleWYSIWYG ( true )
ret . SetBlockRef ( true )
ret . SetFileAnnotationRef ( true )
ret . SetKramdownIAL ( true )
ret . SetTag ( true )
ret . SetSuperBlock ( true )
ret . SetImgPathAllowSpace ( true )
ret . SetGitConflict ( true )
ret . SetMark ( true )
ret . SetSup ( true )
ret . SetSub ( true )
ret . SetInlineMathAllowDigitAfterOpenMarker ( true )
ret . SetFootnotes ( false )
ret . SetToC ( false )
ret . SetIndentCodeBlock ( false )
ret . SetParagraphBeginningSpace ( true )
ret . SetAutoSpace ( false )
ret . SetHeadingID ( false )
ret . SetSetext ( false )
ret . SetYamlFrontMatter ( false )
ret . SetLinkRef ( false )
ret . SetCodeSyntaxHighlight ( false )
ret . SetSanitize ( true )
return
}
2023-05-25 22:00:56 +08:00
func NewStdLute ( ) ( ret * lute . Lute ) {
ret = lute . New ( )
ret . SetFootnotes ( false )
ret . SetToC ( false )
ret . SetIndentCodeBlock ( false )
ret . SetAutoSpace ( false )
ret . SetHeadingID ( false )
ret . SetSetext ( false )
ret . SetYamlFrontMatter ( false )
ret . SetLinkRef ( false )
ret . SetGFMAutoLink ( false ) // 导入 Markdown 时不自动转换超链接 https://github.com/siyuan-note/siyuan/issues/7682
ret . SetImgPathAllowSpace ( true )
ret . SetInlineMathAllowDigitAfterOpenMarker ( true ) // Formula parsing supports $ followed by numbers when importing Markdown https://github.com/siyuan-note/siyuan/issues/8362
return
}
2023-06-06 11:19:24 +08:00
func LinkTarget ( htmlStr , linkBase string ) ( ret string ) {
doc , err := goquery . NewDocumentFromReader ( strings . NewReader ( htmlStr ) )
if nil != err {
logging . LogErrorf ( "parse HTML failed: %s" , err )
return
}
doc . Find ( "a" ) . Each ( func ( i int , selection * goquery . Selection ) {
if href , ok := selection . Attr ( "href" ) ; ok {
if IsRelativePath ( href ) {
selection . SetAttr ( "href" , linkBase + href )
}
// The hyperlink in the marketplace package README fails to jump to the browser to open https://github.com/siyuan-note/siyuan/issues/8452
selection . SetAttr ( "target" , "_blank" )
}
} )
ret , _ = doc . Find ( "body" ) . Html ( )
return
}