+// telegramEscapeTags puts a backslash before each < of markdown outside inline
+// code and fenced code blocks, so that rich Markdown shows a tag name as text
+// and reads no raw HTML. A < that a backslash already makes plain stays as it
+// is. A code block that does not close runs to the end, as in Markdown.
+func telegramEscapeTags(markdown string) string {
+ var out strings.Builder
+ lines := strings.Split(markdown, "\n")
+ for index := 0; index < len(lines); index++ {
+ if index > 0 {
+ out.WriteString("\n")
+ }
+
+ line := lines[index]
+ _, fence := telegramCodeFence(line)
+ if fence == "" {
+ telegramEscapeLineTags(&out, line)
+ continue
+ }
+
+ end := telegramClosingFence(lines, index, fence)
+ if end < 0 {
+ end = len(lines) - 1
+ }
+
+ out.WriteString(strings.Join(lines[index:end+1], "\n"))
+ index = end
+ }
+
+ return out.String()
+}
+