Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

6.2 KB
Raw Blame
// SiYuan - https://github.com/siyuan-note/siyuan
// Copyright (c) 2020-present, b3log.org
// SPDX-License-Identifier: AGPL-3.0-or-later

package util

import (
	"html"
	"strings"

	"github.com/88250/lute"
	"github.com/PuerkitoBio/goquery"
	"github.com/siyuan-note/logging"
)

// MarkdownSettings 运行时 Markdown 配置。
var MarkdownSettings = NewAtomicValue(NewMarkdown())

// NewMarkdown 返回独立的默认语法配置,不引用运行时设置。
func NewMarkdown() *Markdown {
	return &Markdown{
		InlineAsterisk:               true,
		InlineUnderscore:             true,
		InlineSup:                    true,
		InlineSub:                    true,
		InlineTag:                    true,
		InlineMath:                   true,
		InlineStrikethrough:          true,
		InlineFullWidthStrikethrough: false,
		BlockFullWidthTaskList:       new(true),
		InlineMark:                   true,
		CodeBlockMiddleDot:           new(true),
	}
}

type Markdown struct {
	InlineAsterisk               bool  `json:"inlineAsterisk"`               // 是否启用行级 * 语法
	InlineUnderscore             bool  `json:"inlineUnderscore"`             // 是否启用行级 _ 语法
	InlineSup                    bool  `json:"inlineSup"`                    // 是否启用行级上标
	InlineSub                    bool  `json:"inlineSub"`                    // 是否启用行级下标
	InlineTag                    bool  `json:"inlineTag"`                    // 是否启用行级标签
	InlineMath                   bool  `json:"inlineMath"`                   // 是否启用行级公式
	InlineStrikethrough          bool  `json:"inlineStrikethrough"`          // 是否启用行级删除线
	InlineFullWidthStrikethrough bool  `json:"inlineFullWidthStrikethrough"` // 是否启用全角行级删除线
	BlockFullWidthTaskList       *bool `json:"blockFullWidthTaskList"`       // 是否启用全角任务列表快捷输入
	InlineMark                   bool  `json:"inlineMark"`                   // 是否启用行级标记
	CodeBlockMiddleDot           *bool `json:"codeBlockMiddleDot"`           // 是否启用中点代码块快捷输入
}

func NewLute() (ret *lute.Lute) {
	ret = lute.New()
	// 文本标记以 HTML 实体保存正文,解析时保留转义,避免渲染时将尖括号正文识别为标签。
	ret.ParseOptions.KeepEscaped = true
	ret.SetTextMark(true)
	ret.SetProtyleWYSIWYG(true)
	ret.SetBlockRef(true)
	ret.SetFileAnnotationRef(true)
	ret.SetKramdownIAL(true)
	ret.SetTag(true)
	ret.SetSuperBlock(true)
	ret.SetCustomBlock(true)
	ret.SetImgPathAllowSpace(true)
	ret.SetGitConflict(true)
	markdown := MarkdownSettings.Load()
	ret.SetInlineAsterisk(markdown.InlineAsterisk)
	ret.SetInlineUnderscore(markdown.InlineUnderscore)
	ret.SetSup(markdown.InlineSup)
	ret.SetSub(markdown.InlineSub)
	ret.SetTag(markdown.InlineTag)
	ret.SetInlineMath(markdown.InlineMath)
	ret.SetGFMStrikethrough(markdown.InlineStrikethrough)
	ret.SetFullWidthStrikethrough(markdown.InlineFullWidthStrikethrough)
	ret.SetMark(markdown.InlineMark)
	ret.SetInlineMathAllowDigitAfterOpenMarker(true)
	ret.SetGFMStrikethrough1(false)
	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)
	ret.SetUnorderedListMarker("-")
	ret.SetCallout(true)
	ret.SetTabs(true)
	ret.SetDataTask(true)
	ret.SetArbitraryTaskListItemMarker(true)
	ret.SetExportNormalizeTaskListMarker(false) // 只有导出 Markdown 的场景才将其设置为 true
	ret.SetEnsureListItemParagraph(true)        // 空列表项下创建子列表前补一个空段落
	return
}

func NewStdLute() (ret *lute.Lute) {
	ret = lute.New()
	ret.SetCustomBlock(true)
	ret.SetFootnotes(false)
	ret.SetToC(false)
	ret.SetIndentCodeBlock(true) // 导入 Markdown 时支持缩进代码块语法 Support indented code block syntax when importing Markdown https://github.com/siyuan-note/siyuan/issues/14429
	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

	// 导入 Markdown 时遵循编辑器 Markdown 语法设置
	// Follow editor Markdown syntax settings when importing Markdown https://github.com/siyuan-note/siyuan/issues/14731
	markdown := MarkdownSettings.Load()
	ret.SetInlineAsterisk(markdown.InlineAsterisk)
	ret.SetInlineUnderscore(markdown.InlineUnderscore)
	ret.SetSup(markdown.InlineSup)
	ret.SetSub(markdown.InlineSub)
	ret.SetTag(markdown.InlineTag)
	ret.SetInlineMath(markdown.InlineMath)
	ret.SetGFMStrikethrough(markdown.InlineStrikethrough)
	ret.SetFullWidthStrikethrough(markdown.InlineFullWidthStrikethrough)
	ret.SetGFMStrikethrough1(false)
	return
}

func ConvertIframeToLink(htmlStr string) string {
	doc, err := goquery.NewDocumentFromReader(strings.NewReader(htmlStr))
	if err != nil {
		logging.LogErrorf("parse HTML for iframe conversion failed: %s", err)
		return htmlStr
	}

	doc.Find("iframe").Each(func(i int, s *goquery.Selection) {
		if src, exists := s.Attr("src"); exists && strings.TrimSpace(src) != "" {
			escapedSrc := html.EscapeString(src)
			s.AfterHtml(`<a href="` + escapedSrc + `" target="_blank">` + escapedSrc + `</a>`)
		}
		s.Remove()
	})

	ret, _ := doc.Find("body").Html()
	return ret
}

func LinkTarget(htmlStr, linkBase string) (ret string) {
	doc, err := goquery.NewDocumentFromReader(strings.NewReader(htmlStr))
	if err != nil {
		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
}