parse

package
v1.7.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2026 License: MulanPSL-2.0 Imports: 16 Imported by: 36

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EmojiAliasUnicode = map[string]string{}/* 2126 elements not displayed */
View Source
var EmojiLock = sync.Mutex{}
View Source
var EmojiSitePlaceholder = util.StrToBytes("${emojiSite}")
View Source
var EmojiUnicodeAlias map[string]string
View Source
var MathBlockMarker = util.StrToBytes("$$")
View Source
var MathBlockMarkerCaret = util.StrToBytes("$$" + editor.Caret)
View Source
var YamlFrontMatterMarker = util.StrToBytes("---")
View Source
var YamlFrontMatterMarkerCaret = util.StrToBytes("---" + editor.Caret)
View Source
var YamlFrontMatterMarkerCaretNewline = util.StrToBytes("---" + editor.Caret + "\n")
View Source
var YamlFrontMatterMarkerNewline = util.StrToBytes("---\n")

Functions

func ATXHeadingStart

func ATXHeadingStart(t *Tree, container *ast.Node) int

ATXHeadingStart 判断 ATX 标题(#)是否开始。

func AddAutoLinkDomainSuffix

func AddAutoLinkDomainSuffix(suffix string)

AddAutoLinkDomainSuffix 添加自动链接解析域名后缀 suffix。

func ApplyHTMLTextSemantics added in v1.7.8

func ApplyHTMLTextSemantics(node *ast.Node, dom *html.Node)

ApplyHTMLTextSemantics 保留样式文本的 CSS 强调及祖先链接,避免平铺后丢失组合格式。

func ApplyHTMLTextStyle added in v1.7.8

func ApplyHTMLTextStyle(n *ast.Node, style string)

ApplyHTMLTextStyle 将样式附着到原生文本标记,保留链接及组合格式的数据。

func ApplyTableCellRichProjection added in v1.7.8

func ApplyTableCellRichProjection(cell *ast.Node) error

ApplyTableCellRichProjection 仅在源成功解析后刷新派生的行内内容。

func BlockQueryEmbedStart

func BlockQueryEmbedStart(t *Tree, container *ast.Node) int

BlockQueryEmbedStart 判断内容块查询嵌入({{ SELECT * FROM blocks WHERE content LIKE '%待办%' }})是否开始。

func BlockquoteContinue

func BlockquoteContinue(blockquote *ast.Node, context *Context) int

func BlockquoteStart

func BlockquoteStart(t *Tree, container *ast.Node) int

BlockquoteStart 判断引述(>)是否开始。

func CalloutContinue added in v1.7.7

func CalloutContinue(callout *ast.Node, context *Context) int

func CalloutStart added in v1.7.7

func CalloutStart(t *Tree, container *ast.Node) int

CalloutStart 判断提示块(> [!Note])是否开始。

func CloneTableCellInline added in v1.7.8

func CloneTableCellInline(node *ast.Node) *ast.Node

CloneTableCellInline 复制行内节点及其属性,避免投影和源树共享可变节点。

func CodeBlockContinue

func CodeBlockContinue(codeBlock *ast.Node, context *Context) int

func ContainTextMark added in v1.7.5

func ContainTextMark(node *ast.Node, dataTypes ...string) bool

func CustomBlockContinue added in v1.7.6

func CustomBlockContinue(customBlock *ast.Node, context *Context) int

func CustomBlockStart added in v1.7.6

func CustomBlockStart(t *Tree, container *ast.Node) int

CustomBlockStart 判断围栏自定义块(;;;info)是否开始。

func FenceCodeBlockStart

func FenceCodeBlockStart(t *Tree, container *ast.Node) int

FenceCodeBlockStart 判断围栏代码块(```)是否开始。

func FootnotesContinue

func FootnotesContinue(footnotesDef *ast.Node, context *Context) int

func FootnotesStart

func FootnotesStart(t *Tree, container *ast.Node) int

FootnotesStart 判断脚注定义([^label])是否开始。

func GitConflictContinue

func GitConflictContinue(gitConflictBlock *ast.Node, context *Context) int

func GitConflictStart

func GitConflictStart(t *Tree, container *ast.Node) int

判断 Git 冲突标记是否开始。

<<<<<<< HEAD
这里是本地原来的内容
=======
这里是拉取下来的内容
>>>>>>> feebfeb6bef44cf1384d51cdd7aef7e4197b8180

func HTMLStyleDeclarations added in v1.7.8

func HTMLStyleDeclarations(style string) map[string]string

HTMLStyleDeclarations 在引号和括号之外拆分声明,保留同一属性的优先级。

func HtmlBlockContinue

func HtmlBlockContinue(html *ast.Node, context *Context) int

func HtmlBlockStart

func HtmlBlockStart(t *Tree, container *ast.Node) int

HtmlBlockStart 判断 HTML 块(<)是否开始。

func IAL2Map

func IAL2Map(ial [][]string) (ret map[string]string)

func IAL2MapUnEsc

func IAL2MapUnEsc(ial [][]string) (ret map[string]string)

func IAL2Tokens

func IAL2Tokens(ial [][]string) []byte

func IALStart

func IALStart(t *Tree, container *ast.Node) int

IALStart 判断 kramdown 块级内联属性列表({: attrs})是否开始。

func IALVal

func IALVal(ial *ast.Node, name string) string

func IALValMap

func IALValMap(ial *ast.Node) (ret map[string]string)

func IndentCodeBlockStart

func IndentCodeBlockStart(t *Tree, container *ast.Node) int

IndentCodeBlockStart 判断缩进代码块( code)是否开始。

func IsEmoji added in v1.7.7

func IsEmoji(str string) bool

func ListItemContinue

func ListItemContinue(listItem *ast.Node, context *Context) int

func ListStart

func ListStart(t *Tree, container *ast.Node) int

ListStart 判断列表、列表项(* - + 1.)或者任务列表项是否开始。

func Map2IAL

func Map2IAL(properties map[string]string) (ret [][]string)

func MathBlockContinue

func MathBlockContinue(mathBlock *ast.Node, context *Context) int

func MathBlockStart

func MathBlockStart(t *Tree, container *ast.Node) int

MathBlockStart 判断数学公式块($$)是否开始。

func NestedInlines2FlattedSpans added in v1.7.5

func NestedInlines2FlattedSpans(tree *Tree, isExportMd bool)

NestedInlines2FlattedSpans 将嵌套的行级节点转换为平铺的文本标记节点。 该函数会移除转义节点。

func NestedInlines2FlattedSpansHybrid added in v1.7.6

func NestedInlines2FlattedSpansHybrid(tree *Tree, isExportMd bool)

NestedInlines2FlattedSpansHybrid 将嵌套的行级节点转换为平铺的文本标记节点。 该函数不会移除转义节点。

func NormalizeInlineHTMLTextStyles added in v1.7.8

func NormalizeInlineHTMLTextStyles(tree *Tree)

NormalizeInlineHTMLTextStyles 只转换配对的行内标签,代码块和未闭合标签保持原样。

func ParagraphContinue

func ParagraphContinue(p *ast.Node, context *Context) int

func ProjectTableCellRich added in v1.7.8

func ProjectTableCellRich(tree *Tree) *ast.Node

ProjectTableCellRich 将片段转换成行内节点,保留图片、引用、样式以及可读的列表标记。

func ProjectTableCellRichWithSources added in v1.7.8

func ProjectTableCellRichWithSources(tree *Tree) (*ast.Node, map[*ast.Node]*ast.Node)

ProjectTableCellRichWithSources 同时记录行内节点的来源,供资源和引用改写同步回富文本源。

func SetSpanIAL added in v1.7.5

func SetSpanIAL(node *ast.Node, n *html.Node)

func SetTextMarkNode added in v1.7.5

func SetTextMarkNode(node *ast.Node, n *html.Node, options *Options)

func SetextHeadingStart

func SetextHeadingStart(t *Tree, container *ast.Node) int

SetextHeadingStart 判断 Setext 标题(- =)是否开始。

func StyleValue added in v1.7.5

func StyleValue(style string) (ret string)

func SuperBlockContinue

func SuperBlockContinue(superBlock *ast.Node, context *Context) int

func SuperBlockStart

func SuperBlockStart(t *Tree, container *ast.Node) int

SuperBlockStart 判断超级块({{{ blocks }}})是否开始。

func TableCellAlignValue added in v1.7.8

func TableCellAlignValue(align, style string) int

TableCellAlignValue 兼容旧的 align 属性,并以行内样式为准。

func TableCellStyleAlign added in v1.7.8

func TableCellStyleAlign(style string) (align int, found bool)

TableCellStyleAlign 读取单元格行内样式中的水平对齐方式。

func TabsStart added in v1.7.8

func TabsStart(t *Tree, container *ast.Node) int

TabsStart 解析页签围栏和分隔符,代码等叶子块由块解析流程保护。

func TagAttr

func TagAttr(tokens []byte) (valid bool, remains, attr, name, val []byte)

func TextMarks2Inlines added in v1.7.7

func TextMarks2Inlines(tree *Tree)

func TextMarks2InlinesWithNestedSyntax added in v1.7.8

func TextMarks2InlinesWithNestedSyntax(tree *Tree)

TextMarks2InlinesWithNestedSyntax 仅转换包含可平铺 Markdown 行级语法的文本标记。

func ThematicBreakStart

func ThematicBreakStart(t *Tree, container *ast.Node) int

判断分隔线(--- ***)是否开始。

func Tokens2IAL

func Tokens2IAL(tokens []byte) (ret [][]string)

func YamlFrontMatterContinue

func YamlFrontMatterContinue(node *ast.Node, context *Context) int

func YamlFrontMatterStart

func YamlFrontMatterStart(t *Tree, container *ast.Node) int

判断 YAML Front Matter(---)是否开始。

Types

type Context

type Context struct {
	Tree        *Tree    // 关联的语法树
	ParseOption *Options // 解析选项

	Tip *ast.Node // 末梢节点
	// contains filtered or unexported fields
}

Context 用于维护块级元素解析过程中使用到的公共数据。

func (*Context) ParentTip

func (context *Context) ParentTip()

func (*Context) TipAppendChild

func (context *Context) TipAppendChild(child *ast.Node)

type HTMLTextStyle added in v1.7.8

type HTMLTextStyle struct {
	Color, Background, Family string
	Size                      float64
	SizeUnit                  string
}

HTMLTextStyle 只保存可映射到文本标记的外部样式,不携带页面布局或资源引用。

func HTMLNodeTextStyle added in v1.7.8

func HTMLNodeTextStyle(n *html.Node) HTMLTextStyle

func ResolveHTMLTextStyle added in v1.7.8

func ResolveHTMLTextStyle(n *html.Node, parent HTMLTextStyle) HTMLTextStyle

ResolveHTMLTextStyle 将相对字号按父级计算;没有绝对基准时保留相对编辑器字号的 em 值。

func (HTMLTextStyle) CSS added in v1.7.8

func (s HTMLTextStyle) CSS() string

type InlineContext

type InlineContext struct {
	// contains filtered or unexported fields
}

InlineContext 描述了行级元素解析上下文。

type Options

type Options struct {
	// DisableTableCellRich 禁止在富文本片段内部递归展开单元格富文本。
	DisableTableCellRich bool
	// GFMTable 设置是否打开“GFM 表”支持。
	GFMTable bool
	// GFMTaskListItem 设置是否打开“GFM 任务列表项”支持。
	GFMTaskListItem bool
	// GFMStrikethrough 设置是否打开“GFM 删除线”支持。
	GFMStrikethrough bool
	// GFMStrikethrough1 设置是否打开“GFM 删除线”一个标记符 ~ 支持。
	// GFM 删除线支持两个标记符 ~~,这个选项用于支持一个标记符的删除线。
	GFMStrikethrough1 bool
	// FullWidthStrikethrough 设置是否将两个全角波浪号作为删除线标记符支持。
	FullWidthStrikethrough bool
	// GFMAutoLink 设置是否打开“GFM 自动链接”支持。
	GFMAutoLink bool
	// Footnotes 设置是否打开“脚注”支持。
	Footnotes bool
	// HeadingID 设置是否打开“自定义标题 ID”支持。
	HeadingID bool
	// ToC 设置是否打开“目录”支持。
	ToC bool
	// Emoji 设置是否对 Emoji 别名替换为原生 Unicode 字符。
	Emoji bool
	// AliasEmoji 存储 ASCII 别名到表情 Unicode 映射。
	AliasEmoji map[string]string
	// EmojiAlias 存储表情 Unicode 到 ASCII 别名映射。
	EmojiAlias map[string]string
	// EmojiSite 设置图片 Emoji URL 的路径前缀。
	EmojiSite string
	// Vditor 所见即所得支持。
	VditorWYSIWYG bool
	// Vditor 即时渲染支持。
	VditorIR bool
	// Vditor 分屏预览支持。
	VditorSV bool
	// Protyle 所见即所得支持。
	ProtyleWYSIWYG bool
	// ProtyleWYSIWYGAutoLink 设置是否在 Protyle 所见即所得模式下启用自动链接解析。
	ProtyleWYSIWYGAutoLink bool
	// InlineMath 设置是否开启行级公式 $foo$ 支持。
	InlineMath bool
	// InlineMathAllowDigitAfterOpenMarker 设置内联数学公式是否允许起始 $ 后紧跟数字 https://github.com/b3log/lute/issues/38
	InlineMathAllowDigitAfterOpenMarker bool
	// Setext 设置是否解析 Setext 标题 https://github.com/88250/lute/issues/50
	Setext bool
	// YamlFrontMatter 设置是否开启 YAML Front Matter 支持。
	YamlFrontMatter bool
	// BlockRef 设置是否开启内容块引用支持。
	BlockRef bool
	// FileAnnotationRef 设置是否开启文件注解引用支持。
	FileAnnotationRef bool
	// Mark 设置是否打开 ==标记== 支持。
	Mark bool
	// KramdownBlockIAL 设置是否打开 kramdown 块级内联属性列表支持。 https://kramdown.gettalong.org/syntax.html#inline-attribute-lists
	KramdownBlockIAL bool
	// KramdownSpanIAL 设置是否打开 kramdown 行级内联属性列表支持。
	KramdownSpanIAL bool
	// Tag 设置是否开启 #标签# 支持。
	Tag bool
	// ImgPathAllowSpace 设置是否支持图片路径带空格。
	ImgPathAllowSpace bool
	// SuperBlock 设置是否支持超级块。 https://github.com/88250/lute/issues/111
	SuperBlock bool
	// CustomBlock 设置是否支持自定义块。 https://github.com/siyuan-note/siyuan/issues/8418
	CustomBlock bool
	// Sup 设置是否打开 ^上标^ 支持。
	Sup bool
	// Sub 设置是否打开 ~下标~ 支持。
	Sub bool
	// InlineAsterisk 设置是否打开行级 * 语法支持(*foo* 和 **foo**)。
	InlineAsterisk bool
	// InlineUnderscore 设置是否打开行级 _ 语法支持(_foo_ 和 __foo__)。
	InlineUnderscore bool
	// GitConflict 设置是否打开 Git 冲突标记支持。
	GitConflict bool
	// LinkRef 设置是否打开“链接引用”支持。
	LinkRef bool
	// IndentCodeBlock 设置是否打开“缩进代码块”支持。
	IndentCodeBlock bool
	// ParagraphBeginningSpace 设置是否打开“段首空格”支持。
	ParagraphBeginningSpace bool
	// DataImage 设置是否打开 ![foo](data:image...) 形式的图片支持。
	DataImage bool
	// TextMark 设置是否打开通用行级节点解析支持。
	TextMark bool
	// HTMLTag2TextMark 设置是否打开 HTML 某些标签解析为 TextMark 节点支持。
	// 目前仅支持 <u>、<kbd>、<sub>、<sup>、<strong>/<b>、<em>/<i>、<s>/<del>/<strike> 和 <mark>。
	//
	// * h2m 时就算禁用行级语法,只要启用 HTMLTag2TextMark 则依然转换为 md 标记符,因为 h2m 不走 parse
	// * 走 parse 时如果遇到 html 标签且 HTMLTag2TextMark 开启则转换为 TextMark 节点
	// 所以最终以上两种情况都相当于启用行级语法渲染 Protyle DOM
	HTMLTag2TextMark bool
	// Spin 设置是否打开自旋解析支持,该选项仅用于 Spin 内部过程,设置时请注意使用场景。
	//
	// 该选项的引入主要为了解决 finalParseBlockIAL 过程中是否需要移动 IAL 节点的问题,只有处于自旋过程中才需要移动 IAL 节点
	// 其他情况,比如标题块软换行分块 https://github.com/siyuan-note/siyuan/issues/5723 以及软换行空行分块 https://ld246.com/article/1703839312585
	// 的场景需要移动 IAL 节点,但是 API 输入 markdown https://github.com/siyuan-note/siyuan/issues/6725)无需移动
	Spin bool
	// HTML2MarkdownAttrs 设置将 HTML 转换为 Markdown 时保留的属性列表
	HTML2MarkdownAttrs []string
	// Callout 设置是否开启提示块支持。
	Callout bool

	// Tabs 设置是否开启页签容器支持。
	Tabs bool
	// KeepEscaped 设置是否保留转义内容(不进行反转义)。
	KeepEscaped bool
	// ArbitraryTaskListItemMarker 设置是否打开"任务列表任意标记符"支持。
	// 默认仅支持 [ ]、[x] 和 [X],开启后支持 [/]、[>]、[-]、[!] 等任意标记符。
	ArbitraryTaskListItemMarker bool
	// EnsureListItemParagraph 为 true 时,空列表项下创建子列表前会补一个空段落,
	// 避免出现列表项下直接挂列表的结构 https://github.com/siyuan-note/siyuan/issues/17890
	EnsureListItemParagraph bool
}

Options 描述了解析选项。

func NewOptions

func NewOptions() *Options

func TableCellRichOptions added in v1.7.8

func TableCellRichOptions() *Options

TableCellRichOptions 固定富文本存储语法,不受当前编辑器的 Markdown 开关影响。

func (*Options) IsValidTaskListItemMarker added in v1.7.7

func (options *Options) IsValidTaskListItemMarker(marker byte) bool

IsValidTaskListItemMarker 判断 marker 是否是合法的任务列表项标记符。 当 ArbitraryTaskListItemMarker 开启时接受任意非 [] 字符,否则仅接受 ' '、'x' 和 'X'。

type Tree

type Tree struct {
	Root    *ast.Node // 根节点
	Context *Context  // 块级解析上下文

	Name    string   // 名称
	ID      string   // ID
	Box     string   // 容器
	Path    string   // 路径
	HPath   string   // 人类可读的路径
	Marks   []string // 文本标记
	Created int64    // 创建时间
	Updated int64    // 更新时间
	Hash    string   // 内容哈希
	// contains filtered or unexported fields
}

Tree 描述了 Markdown 抽象语法树结构。

func Block

func Block(name string, markdown []byte, options *Options) (tree *Tree)

Block 会将 markdown 原始文本字节数组解析为一棵语法树,该语法树的第一个块级子节点是段落节点。

func Inline

func Inline(name string, markdown []byte, options *Options) (tree *Tree)

Inline 会将 markdown 原始文本字节数组解析为一棵语法树,该语法树的第一个块级子节点是段落节点。

func Parse

func Parse(name string, markdown []byte, options *Options) (tree *Tree)

Parse 会将 markdown 原始文本字节数组解析为一棵语法树。

func ParseTableCellRich added in v1.7.8

func ParseTableCellRich(rich *ast.TableCellRich) (tree *Tree, err error)

ParseTableCellRich 解析受支持的片段;片段中的块身份不进入文档块树。

func (*Tree) BlockCount

func (t *Tree) BlockCount() (ret int)

func (*Tree) DocBlockCount

func (t *Tree) DocBlockCount() (ret int)

func (*Tree) EmojiImgTokens

func (t *Tree) EmojiImgTokens(alias, src string) []byte

func (*Tree) ExistFootnotesDef added in v1.7.7

func (t *Tree) ExistFootnotesDef() (ret bool)

func (*Tree) FindFootnotesDef

func (t *Tree) FindFootnotesDef(label []byte) (pos int, def *ast.Node)
func (t *Tree) FindLinkRefDefLink(label []byte) (link *ast.Node)

func (*Tree) MergeText added in v1.7.6

func (t *Tree) MergeText()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL