tg_md2html

package module
v0.0.0-...-3cf1fcf Latest Latest
Warning

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

Go to latest
Published: Feb 29, 2024 License: MIT Imports: 7 Imported by: 13

README

gotg_md2html

Having played with telegram bots for a while, I've always hated how inconsistent the markdown support was. It has never seemed stable, consistent, or supported. Sending an "unterminated" entity would result in the message failing to send, even if it was an underscore in a url!

I have however had... better experiences with the HTML support, even if HTML5 symbols (such as ') aren't supported.

This is the reason for this project!

I wanted a way to reliably parse messages from basic markdown, to HTML, in a way that telegram would understand.

How to:

Simply use the MD2HTML function.

htmlText := tg_md2html.MD2HTML("_hello_ `there` *stranger*! I even support [links [with square brackets!]](github.com)")

I am also a fan of url buttons, so I added support for those too - using the following buttonurl: syntax:

htmlText, btnNames, btnLinks := tg_md2html.MD2HTMLButtons("_hello_ [this is a button](buttonurl:link.com)")

Simply prepending buttonurl: to any link will make the parser detect it as a button, and convert it appropriately. The function will return two new lists; button names and their respective links, mapped 1:1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AllMarkdownV2Chars = func() []rune {
	var outString []string
	for k := range chars {
		outString = append(outString, k)
	}
	sort.Strings(outString)
	var out []rune
	for _, x := range outString {
		out = append(out, []rune(x)[0])
	}
	return out
}()
View Source
var ErrNoButtonContent = errors.New("no button contents")

Functions

func EscapeMarkdown

func EscapeMarkdown(r []rune, toEscape []rune) string

func EscapeMarkdownV2

func EscapeMarkdownV2(r []rune) string

func IsEscaped

func IsEscaped(input []rune, pos int) bool

func MD2HTML

func MD2HTML(input string) string

func MD2HTMLV2

func MD2HTMLV2(in string) string

func Reverse

func Reverse(s string, buttons []Button) string

func ReverseV2

func ReverseV2(in string, bs []ButtonV2) (string, error)

func StripHTML

func StripHTML(s string) string

func StripHTMLV2

func StripHTMLV2(s string) string

func StripMD

func StripMD(s string) string

func StripMDV2

func StripMDV2(s string) string

Types

type Button

type Button struct {
	Name     string
	Content  string
	SameLine bool
}

func MD2HTMLButtons

func MD2HTMLButtons(input string) (string, []Button)

type ButtonV2

type ButtonV2 struct {
	Name     string
	Type     string
	Content  string
	SameLine bool
}

ButtonV2 identifies a button. It can contain either a URL, or Text, depending on whether it is a buttonURL: or a buttonText:

func MD2HTMLButtonsV2

func MD2HTMLButtonsV2(in string) (string, []ButtonV2)

type Converter

type Converter struct {
	BtnPrefix      string
	SameLineSuffix string
}

func New

func New() *Converter

func (*Converter) MD2HTML

func (cv *Converter) MD2HTML(input string) string

func (*Converter) MD2HTMLButtons

func (cv *Converter) MD2HTMLButtons(input string) (string, []Button)

func (*Converter) Reverse

func (cv *Converter) Reverse(s string, buttons []Button) string

func (*Converter) StripHTML

func (cv *Converter) StripHTML(s string) string

func (*Converter) StripMD

func (cv *Converter) StripMD(s string) string

type ConverterV2

type ConverterV2 struct {
	Prefixes       map[string]string
	SameLineSuffix string
}

func NewV2

func NewV2(prefixes map[string]string) *ConverterV2

func (ConverterV2) ButtonToMarkdown

func (cv ConverterV2) ButtonToMarkdown(btn ButtonV2) (string, error)

func (ConverterV2) MD2HTML

func (cv ConverterV2) MD2HTML(in string) string

func (ConverterV2) MD2HTMLButtons

func (cv ConverterV2) MD2HTMLButtons(in string) (string, []ButtonV2)

func (ConverterV2) Reverse

func (cv ConverterV2) Reverse(in string, bs []ButtonV2) (string, error)

func (ConverterV2) StripHTMLV2

func (cv ConverterV2) StripHTMLV2(s string) string

func (ConverterV2) StripMDV2

func (cv ConverterV2) StripMDV2(s string) string

Jump to

Keyboard shortcuts

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