Documentation
¶
Overview ¶
Package format contains utilities for working with Matrix HTML, specifically methods to parse Markdown into HTML and to parse Matrix HTML into text or markdown.
https://spec.matrix.org/v1.2/client-server-api/#mroommessage-msgtypes
Index ¶
- Constants
- Variables
- func DefaultPillConverter(displayname, mxid, eventID string, ctx Context) string
- func Digits(num int) int
- func EscapeMarkdown(text string) string
- func HTMLToContent(html string) event.MessageEventContent
- func HTMLToContentFull(renderer *HTMLParser, html string) event.MessageEventContent
- func HTMLToMarkdown(html string) string
- func HTMLToMarkdownFull(parser *HTMLParser, html string) (parsed string, mentions *event.Mentions)
- func HTMLToText(html string) string
- func MarkdownLink(name string, url string) string
- func MarkdownMention(id uriAble) string
- func MarkdownMentionRoomID(name string, id id.RoomID, via ...string) string
- func MarkdownMentionWithName(name string, id uriAble) string
- func RenderMarkdown(text string, allowMarkdown, allowHTML bool) event.MessageEventContent
- func RenderMarkdownCustom(text string, renderer goldmark.Markdown) event.MessageEventContent
- func SafeMarkdownCode[T ~string](textInput T) string
- func TextToContent(text string) event.MessageEventContent
- func UnwrapSingleParagraph(html string) string
- type CodeBlockConverter
- type ColorConverter
- type Context
- type HTMLParser
- type ImageConverter
- type LinkConverter
- type PillConverter
- type SpoilerConverter
- type TagStack
- type TaggedString
- type TextConverter
Constants ¶
View Source
const ContextKeyMentions = "_mentions"
Variables ¶
View Source
var BlockTags = []string{"p", "h1", "h2", "h3", "h4", "h5", "h6", "ol", "ul", "pre", "blockquote", "div", "hr", "table"}
View Source
var Extensions = goldmark.WithExtensions(extension.Strikethrough, extension.Table, mdext.Spoiler)
View Source
var HTMLOptions = goldmark.WithRendererOptions(html.WithHardWraps(), html.WithUnsafe())
View Source
var MarkdownHTMLParser = &HTMLParser{ TabsToSpaces: 4, Newline: "\n", HorizontalLine: "\n---\n", PillConverter: DefaultPillConverter, LinkConverter: func(text, href string, ctx Context) string { if text == href { return fmt.Sprintf("<%s>", href) } return fmt.Sprintf("[%s](%s)", text, href) }, MathConverter: func(s string, c Context) string { return fmt.Sprintf("$%s$", s) }, MathBlockConverter: func(s string, c Context) string { return fmt.Sprintf("$$\n%s\n$$", s) }, UnderlineConverter: func(s string, c Context) string { return fmt.Sprintf("<u>%s</u>", s) }, }
View Source
var TextHTMLParser = &HTMLParser{ TabsToSpaces: 4, Newline: "\n", HorizontalLine: "\n---\n", PillConverter: DefaultPillConverter, }
Functions ¶
func DefaultPillConverter ¶
func EscapeMarkdown ¶
func HTMLToContent ¶
func HTMLToContent(html string) event.MessageEventContent
func HTMLToContentFull ¶
func HTMLToContentFull(renderer *HTMLParser, html string) event.MessageEventContent
func HTMLToMarkdown ¶
HTMLToMarkdown converts Matrix HTML into markdown with the default settings.
Currently, the only difference to HTMLToText is how links are formatted.
func HTMLToMarkdownFull ¶
func HTMLToMarkdownFull(parser *HTMLParser, html string) (parsed string, mentions *event.Mentions)
func HTMLToText ¶
HTMLToText converts Matrix HTML into text with the default settings.
func MarkdownLink ¶
func MarkdownMention ¶
func MarkdownMention(id uriAble) string
func MarkdownMentionRoomID ¶ added in v0.25.1
func MarkdownMentionWithName ¶ added in v0.25.1
func RenderMarkdown ¶
func RenderMarkdown(text string, allowMarkdown, allowHTML bool) event.MessageEventContent
func RenderMarkdownCustom ¶
func RenderMarkdownCustom(text string, renderer goldmark.Markdown) event.MessageEventContent
func SafeMarkdownCode ¶
func TextToContent ¶
func TextToContent(text string) event.MessageEventContent
func UnwrapSingleParagraph ¶
UnwrapSingleParagraph removes paragraph tags surrounding a string if the string only contains a single paragraph.
Types ¶
type CodeBlockConverter ¶
type ColorConverter ¶
type Context ¶
type Context struct {
Ctx context.Context
ReturnData map[string]any
TagStack TagStack
PreserveWhitespace bool
}
func NewContext ¶
func (Context) WithWhitespace ¶
type HTMLParser ¶
type HTMLParser struct {
PillConverter PillConverter
TabsToSpaces int
Newline string
HorizontalLine string
BoldConverter TextConverter
ItalicConverter TextConverter
StrikethroughConverter TextConverter
UnderlineConverter TextConverter
MathConverter TextConverter
MathBlockConverter TextConverter
LinkConverter LinkConverter
SpoilerConverter SpoilerConverter
ColorConverter ColorConverter
MonospaceBlockConverter CodeBlockConverter
MonospaceConverter TextConverter
TextConverter TextConverter
ImageConverter ImageConverter
}
HTMLParser is a somewhat customizable Matrix HTML parser.
type ImageConverter ¶
type LinkConverter ¶
type PillConverter ¶
type SpoilerConverter ¶
type TaggedString ¶
type TaggedString struct {
// contains filtered or unexported fields
}
TaggedString is a string that also contains a HTML tag.
type TextConverter ¶
Click to show internal directories.
Click to hide internal directories.