Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve codeblock markdown handling #121

Merged
merged 1 commit into from
Nov 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions formatting.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,18 @@ func getOrigMsgMD(utf16Data []uint16, ents []MessageEntity) string {
prevText := string(utf16.Decode(utf16Data[prev:ent.Offset]))

text := utf16.Decode(utf16Data[ent.Offset:newPrev])
pre, cleanCntnt, post := splitEdgeWhitespace(string(text))
pre, cleanCntnt, post := splitEdgeWhitespace(string(text), ent)
cleanCntntRune := []rune(cleanCntnt)

switch ent.Type {
case "bold", "italic", "code":
out.WriteString(prevText + pre + mdMap[ent.Type] + escapeContainedMDV1(cleanCntntRune, []rune(mdMap[ent.Type])) + mdMap[ent.Type] + post)
case "pre":
if ent.Language == "" {
out.WriteString(prevText + pre + mdMap[ent.Type] + escapeContainedMDV1(cleanCntntRune, []rune(mdMap[ent.Type])) + mdMap[ent.Type] + post)
} else {
out.WriteString(prevText + pre + mdMap[ent.Type] + ent.Language + "\n" + escapeContainedMDV1(cleanCntntRune, []rune(mdMap[ent.Type])) + mdMap[ent.Type] + post)
}
case "text_mention":
out.WriteString(prevText + pre + "[" + escapeContainedMDV1(cleanCntntRune, []rune("[]()")) + "](tg://user?id=" + strconv.FormatInt(ent.User.Id, 10) + ")" + post)
case "text_link":
Expand Down Expand Up @@ -214,10 +220,15 @@ func closeHTMLTag(s string) string {

func writeFinalMarkdownV2(data []uint16, ent MessageEntity, start int64, cntnt string) string {
prevText := string(utf16.Decode(data[start:ent.Offset]))
pre, cleanCntnt, post := splitEdgeWhitespace(cntnt)
pre, cleanCntnt, post := splitEdgeWhitespace(cntnt, ent)
switch ent.Type {
case "bold", "italic", "code", "underline", "strikethrough", "pre", "spoiler":
case "bold", "italic", "code", "underline", "strikethrough", "spoiler":
return prevText + pre + mdV2Map[ent.Type] + cleanCntnt + mdV2Map[ent.Type] + post
case "pre":
if ent.Language == "" {
return prevText + pre + mdV2Map[ent.Type] + "\n" + cleanCntnt + mdV2Map[ent.Type] + post
}
return prevText + pre + mdV2Map[ent.Type] + ent.Language + "\n" + cleanCntnt + mdV2Map[ent.Type] + post
case "custom_emoji":
// Yes, custom emoji have a weird little ! at the front
// https://core.telegram.org/bots/api#markdownv2-style
Expand Down Expand Up @@ -259,15 +270,17 @@ func getChildEntities(ent MessageEntity, ents []MessageEntity) []MessageEntity {
return children
}

func splitEdgeWhitespace(text string) (pre string, cntnt string, post string) {
func splitEdgeWhitespace(text string, ent MessageEntity) (pre string, cntnt string, post string) {
keepNewLines := ent.Type == "pre"

bd := strings.Builder{}
rText := []rune(text)
for i := 0; i < len(rText) && unicode.IsSpace(rText[i]); i++ {
for i := 0; i < len(rText) && unicode.IsSpace(rText[i]) && (!keepNewLines || rText[i] != '\n'); i++ {
bd.WriteRune(rText[i])
}
pre = bd.String()
text = strings.TrimPrefix(text, pre)

text = strings.TrimPrefix(text, pre)
bd.Reset()
for i := len(rText) - 1; i >= 0 && unicode.IsSpace(rText[i]); i-- {
bd.WriteRune(rText[i])
Expand Down
Loading