mdast

package module
v0.0.0-...-2b5d36d Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2024 License: MIT Imports: 5 Imported by: 0

README

mdast

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FlowToMarkdown

func FlowToMarkdown(ctx context.Context, n *Node) (string, error)

FlowToMarkdown 将流式内容转换为 Markdown

func InlineToMarkdown

func InlineToMarkdown(ctx context.Context, n *Node) (string, error)

InlineToMarkdown 将内联元素转换为 Markdown

func IsInlineHTML

func IsInlineHTML(n *Node) bool

IsInlineHTML 判断给定的 HTML 节点是否为内联元素

func ListToMarkdown

func ListToMarkdown(ctx context.Context, n *Node) (string, error)

ListToMarkdown 将列表内容转换为 Markdown

func RunTestCases

func RunTestCases(t *testing.T, testCases []TestCase)

RunTestCases 运行一组测试用例

func TableCellToMarkdown

func TableCellToMarkdown(ctx context.Context, n *Node) (string, error)

func TableRowToMarkdown

func TableRowToMarkdown(ctx context.Context, n *Node) (string, error)

func TableToMarkdown

func TableToMarkdown(ctx context.Context, n *Node) (string, error)

TableToMarkdown 将表格内容转换为 Markdown

Types

type AlignType

type AlignType string

AlignType 表示表格列的对齐方式

const (
	AlignNone   AlignType = ""
	AlignLeft   AlignType = "left"
	AlignRight  AlignType = "right"
	AlignCenter AlignType = "center"
)

type Content

type Content interface {
	ToMarkdown(ctx context.Context) (string, error)
	GetType() NodeType
}

Content 接口定义了所有内容节点的基本方法

type DataKey

type DataKey string

DataKey 定义 Data 中的键

const (
	NDK_Alt           DataKey = "alt"
	NDK_URL           DataKey = "url"
	NDK_Title         DataKey = "title"
	NDK_Identifier    DataKey = "identifier"
	NDK_Label         DataKey = "label"
	NDK_ReferenceType DataKey = "referenceType"
	NDK_Depth         DataKey = "depth"
	NDK_Lang          DataKey = "lang"
	NDK_Meta          DataKey = "meta"
	NDK_Ordered       DataKey = "ordered"
	NDK_Start         DataKey = "start"
	NDK_Spread        DataKey = "spread"
	NDK_Checked       DataKey = "checked"
	NDK_Align         DataKey = "align"
)

定义 Data 中的常量

type DataTable

type DataTable map[DataKey]any

DataTable 定义 Data 类型

func (DataTable) GetAlignType

func (dt DataTable) GetAlignType(key DataKey) (AlignType, bool)

GetAlignType 从 DataTable 中获取 AlignType 值

func (DataTable) GetBool

func (dt DataTable) GetBool(key DataKey) (bool, bool)

GetBool 从 DataTable 中获取布尔值

func (DataTable) GetInt

func (dt DataTable) GetInt(key DataKey) (int, bool)

GetInt 从 DataTable 中获取整数值

func (DataTable) GetReferenceType

func (dt DataTable) GetReferenceType(key DataKey) (ReferenceType, bool)

GetReferenceType 从 DataTable 中获取 ReferenceType 值

func (DataTable) GetString

func (dt DataTable) GetString(key DataKey) (string, bool)

GetString 从 DataTable 中获取字符串值

type FlowContent

type FlowContent interface {
	Content
	IsFlow()
}

FlowContent 接口定义了流式内容节点

type ListContent

type ListContent interface {
	Content
	IsList()
}

ListContent 接口定义了列表内容节点

type Node

type Node struct {
	Type             NodeType
	Value            string
	FlowChildren     []FlowContent
	PhrasingChildren []PhrasingContent
	ListChildren     []ListContent
	TableChildren    []TableContent
	Data             DataTable
	// contains filtered or unexported fields
}

Node 结构体现在包含特定类型的子节点字段

func NewNode

func NewNode(nodeType NodeType) *Node

NewNode 创建一个新的 Node

func (*Node) AddFlowChild

func (n *Node) AddFlowChild(child FlowContent)

AddFlowChild 添加一个流式子节点

func (*Node) AddListChild

func (n *Node) AddListChild(child ListContent)

AddListChild 添加一个列表子节点

func (*Node) AddPhrasingChild

func (n *Node) AddPhrasingChild(child PhrasingContent)

AddPhrasingChild 添加一个短语子节点

func (*Node) AddTableChild

func (n *Node) AddTableChild(child TableContent)

AddTableChild 添加一个表格子节点

func (*Node) GetData

func (n *Node) GetData(key DataKey) (any, bool)

GetData 获取节点数据

func (*Node) GetType

func (n *Node) GetType() NodeType

GetType 实现 Content 接口

func (*Node) IsFlow

func (n *Node) IsFlow()

IsFlow 实现 FlowContent 接口

func (*Node) IsList

func (n *Node) IsList()

IsList 实现 ListContent 接口

func (*Node) IsPhrasing

func (n *Node) IsPhrasing()

IsPhrasing 实现 PhrasingContent 接口

func (*Node) IsTable

func (n *Node) IsTable()

IsTable 实现 TableContent 接口

func (*Node) SetData

func (n *Node) SetData(key DataKey, value any)

SetData 设置节点数据

func (*Node) ToMarkdown

func (n *Node) ToMarkdown(ctx context.Context) (string, error)

ToMarkdown 将节点转换为 Markdown 文本

Example (ComplexList)
node := createComplexList()
result, err := node.ToMarkdown(context.Background())
if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}
fmt.Print(result)
Output:

1. First item
2. Second item with *emphasis*
   - Subitem
Example (ComplexStructure)
root := NewNode(NodeRoot)
root.AddFlowChild(createHeadingNode(1, "Complex Example"))
para := NewNode(NodeParagraph)
para.AddPhrasingChild(&Node{Type: NodeText, Value: "This is a "})
para.AddPhrasingChild(&Node{Type: NodeEmphasis, PhrasingChildren: []PhrasingContent{&Node{Type: NodeText, Value: "complex"}}})
para.AddPhrasingChild(&Node{Type: NodeText, Value: " example with "})
para.AddPhrasingChild(&Node{Type: NodeStrong, PhrasingChildren: []PhrasingContent{&Node{Type: NodeText, Value: "nested"}}})
para.AddPhrasingChild(&Node{Type: NodeText, Value: " elements."})
root.AddFlowChild(para)

// 添加调试信息
fmt.Fprintf(os.Stderr, "Root node type: %s\n", root.Type)
fmt.Fprintf(os.Stderr, "Number of children: %d\n", len(root.FlowChildren))
for i, child := range root.FlowChildren {
	fmt.Fprintf(os.Stderr, "Child %d type: %s\n", i, child.GetType())
}

markdown, err := root.ToMarkdown(context.Background())
if err != nil {
	fmt.Fprintf(os.Stderr, "Error: %v\n", err)
	return
}
fmt.Print(markdown)
Output:

# Complex Example

This is a *complex* example with **nested** elements.
Example (Definition)
node := createDefinitionNode("example", "https://example.com", "Example Title")
markdown, err := node.ToMarkdown(context.Background())
if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}
fmt.Print(markdown)
Output:

[example]: https://example.com "Example Title"
Example (Emphasis)
node := &Node{Type: NodeEmphasis, PhrasingChildren: []PhrasingContent{&Node{Type: NodeText, Value: "emphasized text"}}}
result, err := node.ToMarkdown(context.Background())
if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}
fmt.Print(result)
Output:

*emphasized text*
Example (Heading)
node := createHeadingNode(2, "Example Heading")
result, err := node.ToMarkdown(context.Background())
if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}
fmt.Print(result)
Output:

## Example Heading
Example (ImageReference)
node := createImageReferenceNode("example", "Alt Text", "full")
markdown, err := node.ToMarkdown(context.Background())
if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}
fmt.Print(markdown)
Output:

![Alt Text][example]
Example (NestedInline)
node := createNestedInlineNode()
result, err := node.ToMarkdown(context.Background())
if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}
fmt.Print(result)
Output:

This is *emphasized and **strong** text* with `code`

type NodeType

type NodeType string

NodeType 表示 Markdown AST 节点的类型

const (
	NodeRoot               NodeType = "root"
	NodeParagraph          NodeType = "paragraph"
	NodeHeading            NodeType = "heading"
	NodeText               NodeType = "text"
	NodeEmphasis           NodeType = "emphasis"
	NodeStrong             NodeType = "strong"
	NodeDelete             NodeType = "delete"
	NodeLink               NodeType = "link"
	NodeImage              NodeType = "image"
	NodeCode               NodeType = "code"
	NodeInlineCode         NodeType = "inlineCode"
	NodeBlockquote         NodeType = "blockquote"
	NodeList               NodeType = "list"
	NodeListItem           NodeType = "listItem"
	NodeTable              NodeType = "table"
	NodeTableRow           NodeType = "tableRow"
	NodeTableCell          NodeType = "tableCell"
	NodeThematicBreak      NodeType = "thematicBreak"
	NodeBreak              NodeType = "break"
	NodeHTML               NodeType = "html"
	NodeDefinition         NodeType = "definition"
	NodeImageReference     NodeType = "imageReference"
	NodeLinkReference      NodeType = "linkReference"
	NodeFootnote           NodeType = "footnote"
	NodeFootnoteReference  NodeType = "footnoteReference"
	NodeFootnoteDefinition NodeType = "footnoteDefinition"
	NodeYaml               NodeType = "yaml"
)

定义所有的节点类型

func (NodeType) IsBlock

func (nt NodeType) IsBlock() bool

IsBlock 检查节点是否为块级元素

func (NodeType) IsInline

func (nt NodeType) IsInline() bool

IsInline 检查节点是否为内联元素

func (NodeType) IsListContent

func (nt NodeType) IsListContent() bool

IsListContent 检查节点是否为列表内容

func (NodeType) IsRowContent

func (nt NodeType) IsRowContent() bool

IsRowContent 检查节点是否为行内容

func (NodeType) IsTableContent

func (nt NodeType) IsTableContent() bool

IsTableContent 检查节点是否为表格内容

type PhrasingContent

type PhrasingContent interface {
	Content
	IsPhrasing()
}

PhrasingContent 接口定义了短语内容节点

type Point

type Point struct {
	Line   int
	Column int
	Offset int
}

Point 表示位置的具体点

type Position

type Position struct {
	Start  Point
	End    Point
	Indent []int
}

Position 表示节点在源文件中的位置

type ReferenceType

type ReferenceType string

ReferenceType 表示引用的类型

const (
	ReferenceShortcut  ReferenceType = "shortcut"
	ReferenceCollapsed ReferenceType = "collapsed"
	ReferenceFull      ReferenceType = "full"
)

type TableContent

type TableContent interface {
	Content
	IsTable()
}

TableContent 接口定义了表格内容节点

type TestCase

type TestCase struct {
	Name      string
	Node      *Node
	Expected  string
	ExpectErr bool // 新增字段,表示是否期望错误
}

TestCase 定义了一个通用的测试用例结构

Jump to

Keyboard shortcuts

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