mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
* 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>
55 lines
1.8 KiB
Go
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 
|
|
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)
|
|
}
|