Fix cookie refresh and reconnection bugs
This commit is contained in:
@@ -5,6 +5,27 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Pre-compiled regex patterns (Go 1.19 compatible)
|
||||
var (
|
||||
imgPattern = regexp.MustCompile(`(?i)\[img\](.*?)\[/img\]`)
|
||||
videoPattern = regexp.MustCompile(`(?i)\[video\](.*?)\[/video\]`)
|
||||
urlPattern = regexp.MustCompile(`(?i)\[url=(.*?)\](.*?)\[/url\]`)
|
||||
urlSimplePattern = regexp.MustCompile(`(?i)\[url\](.*?)\[/url\]`)
|
||||
boldPattern = regexp.MustCompile(`(?i)\[(?:b|strong)\](.*?)\[/\s*(?:b|strong)\]`)
|
||||
italicPattern = regexp.MustCompile(`(?i)\[(?:i|em)\](.*?)\[/\s*(?:i|em)\]`)
|
||||
underlinePattern = regexp.MustCompile(`(?i)\[u\](.*?)\[/\s*u\]`)
|
||||
strikePattern = regexp.MustCompile(`(?i)\[(?:s|strike)\](.*?)\[/\s*(?:s|strike)\]`)
|
||||
codePattern = regexp.MustCompile(`(?i)\[code\](.*?)\[/code\]`)
|
||||
codeBlockPattern = regexp.MustCompile(`(?i)\[(?:php|plain|code=\w+)\](.*?)\[/(?:php|plain|code)\]`)
|
||||
quotePattern = regexp.MustCompile(`(?i)\[quote\](.*?)\[/quote\]`)
|
||||
spoilerPattern = regexp.MustCompile(`(?i)\[spoiler\](.*?)\[/spoiler\]`)
|
||||
colorSizePattern = regexp.MustCompile(`(?i)\[(?:color|size)=.*?\](.*?)\[/\s*(?:color|size)\]`)
|
||||
listItemPattern = regexp.MustCompile(`(?m)^\[\*\]\s*`)
|
||||
listTagPattern = regexp.MustCompile(`(?i)\[/?list\]`)
|
||||
genericTagPattern = regexp.MustCompile(`\[/?[A-Za-z0-9\-=_]+\]`)
|
||||
httpPattern = regexp.MustCompile(`(?i)^https?://`)
|
||||
)
|
||||
|
||||
func BBCodeToMarkdown(text string) string {
|
||||
if text == "" {
|
||||
return ""
|
||||
@@ -12,10 +33,9 @@ func BBCodeToMarkdown(text string) string {
|
||||
text = strings.ReplaceAll(text, "\r\n", "\n")
|
||||
text = strings.ReplaceAll(text, "\r", "\n")
|
||||
|
||||
text = regexp.MustCompile(`(?i)\[img\](.*?)\[/img\]`).ReplaceAllString(text, "$1")
|
||||
text = regexp.MustCompile(`(?i)\[video\](.*?)\[/video\]`).ReplaceAllString(text, "$1")
|
||||
text = imgPattern.ReplaceAllString(text, "$1")
|
||||
text = videoPattern.ReplaceAllString(text, "$1")
|
||||
|
||||
urlPattern := regexp.MustCompile(`(?i)\[url=(.*?)\](.*?)\[/url\]`)
|
||||
text = urlPattern.ReplaceAllStringFunc(text, func(match string) string {
|
||||
parts := urlPattern.FindStringSubmatch(match)
|
||||
if len(parts) < 3 {
|
||||
@@ -23,21 +43,20 @@ func BBCodeToMarkdown(text string) string {
|
||||
}
|
||||
link := strings.TrimSpace(parts[1])
|
||||
txt := strings.TrimSpace(parts[2])
|
||||
if regexp.MustCompile(`(?i)^https?://`).MatchString(txt) {
|
||||
if httpPattern.MatchString(txt) {
|
||||
return txt
|
||||
}
|
||||
return "[" + txt + "](" + link + ")"
|
||||
})
|
||||
|
||||
text = regexp.MustCompile(`(?i)\[url\](.*?)\[/url\]`).ReplaceAllString(text, "$1")
|
||||
text = regexp.MustCompile(`(?i)\[(?:b|strong)\](.*?)\[/\s*(?:b|strong)\]`).ReplaceAllString(text, "**$1**")
|
||||
text = regexp.MustCompile(`(?i)\[(?:i|em)\](.*?)\[/\s*(?:i|em)\]`).ReplaceAllString(text, "*$1*")
|
||||
text = regexp.MustCompile(`(?i)\[u\](.*?)\[/\s*u\]`).ReplaceAllString(text, "__$1__")
|
||||
text = regexp.MustCompile(`(?i)\[(?:s|strike)\](.*?)\[/\s*(?:s|strike)\]`).ReplaceAllString(text, "~~$1~~")
|
||||
text = regexp.MustCompile(`(?i)\[code\](.*?)\[/code\]`).ReplaceAllString(text, "`$1`")
|
||||
text = regexp.MustCompile(`(?i)\[(?:php|plain|code=\w+)\](.*?)\[/(?:php|plain|code)\]`).ReplaceAllString(text, "```$1```")
|
||||
text = urlSimplePattern.ReplaceAllString(text, "$1")
|
||||
text = boldPattern.ReplaceAllString(text, "**$1**")
|
||||
text = italicPattern.ReplaceAllString(text, "*$1*")
|
||||
text = underlinePattern.ReplaceAllString(text, "__$1__")
|
||||
text = strikePattern.ReplaceAllString(text, "~~$1~~")
|
||||
text = codePattern.ReplaceAllString(text, "`$1`")
|
||||
text = codeBlockPattern.ReplaceAllString(text, "```$1```")
|
||||
|
||||
quotePattern := regexp.MustCompile(`(?i)\[quote\](.*?)\[/quote\]`)
|
||||
text = quotePattern.ReplaceAllStringFunc(text, func(match string) string {
|
||||
parts := quotePattern.FindStringSubmatch(match)
|
||||
if len(parts) < 2 {
|
||||
@@ -51,11 +70,11 @@ func BBCodeToMarkdown(text string) string {
|
||||
return strings.Join(lines, "\n")
|
||||
})
|
||||
|
||||
text = regexp.MustCompile(`(?i)\[spoiler\](.*?)\[/spoiler\]`).ReplaceAllString(text, "||$1||")
|
||||
text = regexp.MustCompile(`(?i)\[(?:color|size)=.*?\](.*?)\[/\s*(?:color|size)\]`).ReplaceAllString(text, "$1")
|
||||
text = regexp.MustCompile(`(?m)^\[\*\]\s*`).ReplaceAllString(text, "• ")
|
||||
text = regexp.MustCompile(`(?i)\[/?list\]`).ReplaceAllString(text, "")
|
||||
text = regexp.MustCompile(`\[/?[A-Za-z0-9\-=_]+\]`).ReplaceAllString(text, "")
|
||||
text = spoilerPattern.ReplaceAllString(text, "||$1||")
|
||||
text = colorSizePattern.ReplaceAllString(text, "$1")
|
||||
text = listItemPattern.ReplaceAllString(text, "• ")
|
||||
text = listTagPattern.ReplaceAllString(text, "")
|
||||
text = genericTagPattern.ReplaceAllString(text, "")
|
||||
|
||||
return strings.TrimSpace(text)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user