From a269d29b194e7a36f1449a1ac7c32c2dd0b25274 Mon Sep 17 00:00:00 2001 From: varphi-online Date: Fri, 13 Sep 2024 15:43:19 -0700 Subject: [PATCH] codeblocks work inline as well --- lib/markdown.go | 102 ++++++++++++++++++++++------------------ test/another-article.md | 2 + 2 files changed, 59 insertions(+), 45 deletions(-) diff --git a/lib/markdown.go b/lib/markdown.go index 5a0f03f..affb486 100644 --- a/lib/markdown.go +++ b/lib/markdown.go @@ -64,7 +64,8 @@ func ConvertMarkdownToRSS(text string) string { markdownLinks := regexp.MustCompile(`\[(.*)\]\((.*)\)`) markdownUnorderedLists := regexp.MustCompile(`^(\s*)(-|\*|\+)[\s](.*)`) markdownOrderedLists := regexp.MustCompile(`^(\s*)(-?\d+)(\.\s+)(.*)$`) - fencedCodeBlock := regexp.MustCompile("^```") + fencedCodeBlock := regexp.MustCompile("^\x60\x60\x60") + inlineCodeBlock := regexp.MustCompile(`([\x60]+)([^\x60]+)([\x60]+)`) // We don't want to format inside of a codeblock, so return early if codeBlockOpen && !fencedCodeBlock.MatchString(text) { @@ -116,60 +117,71 @@ func ConvertMarkdownToRSS(text string) string { return out + "" } } + if inlineCodeBlock.Match([]byte(text)) { + out := inlineCodeBlock.ReplaceAllFunc([]byte(text), func(b []byte) []byte { + return []byte("" + inlineCodeBlock.FindStringSubmatch(string(b))[2] + "") + }) + return string(out) + } return "

" + text + "

" - /* // Same code as above, written as a switch statement, makes more sense to me personally - - switch { - // We don't want to format inside of a codeblock, so return early - case codeBlockOpen && !fencedCodeBlock.MatchString(text): - if codeBlockAggregate != "" { - codeBlockAggregate += "
" - } - codeBlockAggregate += text - return "" + // Same code as above, written as a switch statement, makes more sense to me personally + /* + switch { + // We don't want to format inside of a codeblock, so return early + case codeBlockOpen && !fencedCodeBlock.MatchString(text): + if codeBlockAggregate != "" { + codeBlockAggregate += "
" + } + codeBlockAggregate += text + return "" + + case markdownLinks.MatchString(text): + if strings.Contains(text, "audio/mpeg") { + return convertMarkdownEnclosure(text, markdownLinks) + } else { + return convertMarkdownLink(text, markdownLinks) + } - case markdownLinks.MatchString(text): - if strings.Contains(text, "audio/mpeg") { - return convertMarkdownEnclosure(text, markdownLinks) - } else { - return convertMarkdownLink(text, markdownLinks) - } + case markdownUnorderedLists.MatchString(text): + return convertMarkdownUlList(text) - case markdownUnorderedLists.MatchString(text): - return convertMarkdownUlList(text) + case markdownUlListActive: + markdownUlListActive = false + return "

" + text + "

" - case markdownUlListActive: - markdownUlListActive = false - return "

" + text + "

" + case markdownOrderedLists.MatchString(text): + entryIndex, entryErr := strconv.ParseInt(markdownOrderedLists.FindStringSubmatch(text)[2], 10, 64) + entryText := markdownOrderedLists.FindStringSubmatch(text)[4] + if entryErr != nil { + return "

" + text + "

" + } + return convertMarkdownOlList(entryText, entryIndex) - case markdownOrderedLists.MatchString(text): - entryIndex, entryErr := strconv.ParseInt(markdownOrderedLists.FindStringSubmatch(text)[2], 10, 64) - entryText := markdownOrderedLists.FindStringSubmatch(text)[4] - if entryErr != nil { - return "

" + text + "

" - } - return convertMarkdownOlList(entryText, entryIndex) + case markdownOlIndex != 0: + markdownOlIndex = 0 + return "" + ConvertMarkdownToRSS(text) - case markdownOlIndex != 0: - markdownOlIndex = 0 - return "" + ConvertMarkdownToRSS(text) + case fencedCodeBlock.MatchString(text): + if !codeBlockOpen { + codeBlockOpen = true + codeBlockAggregate = "" + text[3:] + "
" + return "" + "
"
+			} else {
+				out := codeBlockAggregate
+				codeBlockAggregate, codeBlockOpen = "", false
+				return out + "
" + } + case inlineCodeBlock.Match([]byte(text)): + out := inlineCodeBlock.ReplaceAllFunc([]byte(text), func(b []byte) []byte { + return []byte("" + inlineCodeBlock.FindStringSubmatch(string(b))[2] + "") + }) + return string(out) - case fencedCodeBlock.MatchString(text): - if !codeBlockOpen { - codeBlockOpen = true - codeBlockAggregate = "" + text[3:] + "
" - return "" + "
"
-		} else {
-			out := codeBlockAggregate
-			codeBlockAggregate, codeBlockOpen = "", false
-			return out + "
" + default: + return "

" + text + "

" } - - default: - return "

" + text + "

" - } */ } diff --git a/test/another-article.md b/test/another-article.md index 864a756..b27c4c0 100644 --- a/test/another-article.md +++ b/test/another-article.md @@ -36,6 +36,8 @@ fencedCodeBlock := regexp.MustCompile("^```") // Show off your tabwidth in style! ``` +This feature also works `inline` as well! + And back to text again