mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
refactor: convert TerminalColors to global var and split markdown utils
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
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)
|
||||
}
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
)
|
||||
|
||||
func ConfirmInstallation(maliciousPkgs map[string]string) bool {
|
||||
colors := NewTerminalColors()
|
||||
|
||||
fmt.Printf("\n%s\n", colors.Red("⚠️ WARNING: %d potentially malicious packages detected!", len(maliciousPkgs)))
|
||||
fmt.Println(colors.Yellow("The following packages have been flagged:"))
|
||||
|
||||
@@ -9,12 +9,9 @@ type TerminalColors struct {
|
||||
Green func(format string, a ...interface{}) string
|
||||
}
|
||||
|
||||
// NewTerminalColors initializes and returns TerminalColors
|
||||
func NewTerminalColors() *TerminalColors {
|
||||
return &TerminalColors{
|
||||
Red: color.New(color.FgRed, color.Bold).SprintfFunc(),
|
||||
Yellow: color.New(color.FgYellow).SprintfFunc(),
|
||||
Cyan: color.New(color.FgCyan).SprintfFunc(),
|
||||
Green: color.New(color.FgGreen).SprintfFunc(),
|
||||
}
|
||||
var colors = TerminalColors{
|
||||
Red: color.New(color.FgRed, color.Bold).SprintfFunc(),
|
||||
Yellow: color.New(color.FgYellow).SprintfFunc(),
|
||||
Cyan: color.New(color.FgCyan).SprintfFunc(),
|
||||
Green: color.New(color.FgGreen).SprintfFunc(),
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -66,25 +65,3 @@ func IsInstallCommand(pkgManager, cmd string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func removeMarkdown(text string) string {
|
||||
// Remove bold (**bold** or __bold__)
|
||||
text = regexp.MustCompile(`\*\*(.*?)\*\*`).ReplaceAllString(text, "$1")
|
||||
text = regexp.MustCompile(`__(.*?)__`).ReplaceAllString(text, "$1")
|
||||
|
||||
// Remove italic (*italic* or _italic_)
|
||||
text = regexp.MustCompile(`\*(.*?)\*`).ReplaceAllString(text, "$1")
|
||||
text = regexp.MustCompile(`_(.*?)_`).ReplaceAllString(text, "$1")
|
||||
|
||||
// Remove inline code (`code`)
|
||||
text = regexp.MustCompile("`([^`]*)`").ReplaceAllString(text, "$1")
|
||||
|
||||
// Remove links [text](url)
|
||||
text = regexp.MustCompile(`\[(.*?)\]\(.*?\)`).ReplaceAllString(text, "$1")
|
||||
|
||||
// Remove headings (e.g., ### Heading)
|
||||
text = regexp.MustCompile(`(?m)^#{1,6}\s*`).ReplaceAllString(text, "")
|
||||
|
||||
// Trim leading/trailing whitespace
|
||||
return strings.TrimSpace(text)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user