Files
pmg/pkg/common/utils/markdown.go
T
df754ccc82 Add colorful outputs & remove md text (#13)
* fix: parsePackageInfo to handle pkg names with special character

* Enhance pmg outputs by adding colors and removing markdown notions

* Update pkg/wrapper/npm_base.go

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Sahil Bansal <bansalsahil315@gmail.com>

* Update npm_base.go

Signed-off-by: Sahil Bansal <bansalsahil315@gmail.com>

* test: add tests for removeMarkdown

* chore: remove duplicate code

* refactor: convert TerminalColors to global var and split markdown utils

---------

Signed-off-by: Sahil Bansal <bansalsahil315@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-05-05 23:32:34 +05:30

55 lines
1.8 KiB
Go

package utils
import (
"regexp"
"strings"
)
var (
headerBulletRegex = regexp.MustCompile(`(?m)^(#{1,6}\s+|[-*]\s{1,}|\d+\.\s+|>\s+)`)
inlineCodeRegex = regexp.MustCompile("`{1,3}([^`]*)`{1,3}")
horizontalRuleRegex = regexp.MustCompile(`(?m)^\s*(-{3,}|\*{3,}|\_{3,})\s*$`)
boldItalicRegex = regexp.MustCompile(`(?:\*\*\*|___)(.*?)(?:\*\*\*|___)`)
boldRegex = regexp.MustCompile(`(?:\*\*|__)(.*?)(?:\*\*|__)`)
italicRegex = regexp.MustCompile(`(?:\*|_)(.*?)(?:\*|_)`)
strikethroughRegex = regexp.MustCompile(`~~([^~]+)~~`)
inlineLinkRegex = regexp.MustCompile(`\[([^\]]+)\]\((\S+?)\)`)
imageRegex = regexp.MustCompile(`!\[([^\]]*)\]\((\S+?)\)`)
extraSpacesRegex = regexp.MustCompile(`\s+`)
)
func removeMarkdown(text string) string {
// Remove bold italic (***bolditalic*** or ___bolditalic___)
text = boldItalicRegex.ReplaceAllString(text, "$1")
// Remove bold (**bold** or __bold__)
text = boldRegex.ReplaceAllString(text, "$1")
// Remove italic (*italic* or _italic_)
text = italicRegex.ReplaceAllString(text, "$1")
// Remove strikethrough (~~text~~)
text = strikethroughRegex.ReplaceAllString(text, "$1")
// Remove inline code (`code`)
text = inlineCodeRegex.ReplaceAllString(text, "$1")
// Remove links [text](url)
text = inlineLinkRegex.ReplaceAllString(text, "$1")
// Remove images ![alt](url)
text = imageRegex.ReplaceAllString(text, "$1")
// Remove horizontal rules
text = horizontalRuleRegex.ReplaceAllString(text, "")
// Remove headers, blockquotes, bullets (e.g., ### Heading, > Quote, - Item)
text = headerBulletRegex.ReplaceAllString(text, "")
// Normalize extra spaces
text = extraSpacesRegex.ReplaceAllString(text, " ")
// Trim leading/trailing whitespace
return strings.TrimSpace(text)
}