mirror of
https://github.com/safedep/vet.git
synced 2025-12-10 00:22:08 -06:00
Fixed plaintext in sarif messages (#435)
* Fixed plaintext in sarif messages Signed-off-by: Omkar Phansopkar <omkarphansopkar@gmail.com> * Fixed extra group items in regex replace Signed-off-by: Omkar Phansopkar <omkarphansopkar@gmail.com> * Updated testcase with misc symbols Signed-off-by: Omkar Phansopkar <omkarphansopkar@gmail.com> --------- Signed-off-by: Omkar Phansopkar <omkarphansopkar@gmail.com>
This commit is contained in:
parent
c156f54274
commit
828467309c
@ -3,6 +3,7 @@ package markdown
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -84,3 +85,32 @@ func (mb *MarkdownBuilder) AddCollapsibleSection(section *MarkdownCollapsibleSec
|
||||
func (mb *MarkdownBuilder) Build() string {
|
||||
return mb.content.String()
|
||||
}
|
||||
|
||||
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 (mb *MarkdownBuilder) BuildPlainText() string {
|
||||
content := mb.content.String()
|
||||
content = headerBulletRegex.ReplaceAllString(content, "")
|
||||
content = inlineCodeRegex.ReplaceAllString(content, "$1")
|
||||
content = horizontalRuleRegex.ReplaceAllString(content, "")
|
||||
content = boldItalicRegex.ReplaceAllString(content, "$1")
|
||||
content = boldRegex.ReplaceAllString(content, "$1")
|
||||
content = italicRegex.ReplaceAllString(content, "$1")
|
||||
content = strikethroughRegex.ReplaceAllString(content, "$1")
|
||||
content = imageRegex.ReplaceAllString(content, "$1")
|
||||
content = inlineLinkRegex.ReplaceAllString(content, "$1")
|
||||
content = extraSpacesRegex.ReplaceAllString(content, " ")
|
||||
|
||||
return strings.Trim(content, " \n")
|
||||
}
|
||||
|
||||
@ -63,3 +63,26 @@ func TestMarkdownBuilder(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkdownBuildPlainText(t *testing.T) {
|
||||
markdownText := "# Heading 1\n## Heading 2\n### Heading 3\n#### Heading 4\n" +
|
||||
"##### Heading 5\n###### Heading 6\n- Bullet list item 1 \n* Bullet list item 2\n" +
|
||||
"- Another >bullet\n1. Numbered list - item 1 \n2. Numbered list *item 2\n" +
|
||||
"> Blockquote text \n---\n___\n**Bold text1**\n__Bold text2__ \n*Italic text1*\n" +
|
||||
"_Italic text2_ \n***Bold and Italic text*** \n___Bold and Italic text___\n" +
|
||||
"~~Strikethrough text~~ \n Line #has `Inline code` betwee # n\n" +
|
||||
"Line has ``Inline code with backticks`` betwee_n \n ```Inline code in triple backticks```\n" +
|
||||
"```\nmultiline code\n```\n```python\nprint('Hello, Markdown!')\n```\n" +
|
||||
"[Link text](https://example.com)\n\n" +
|
||||
"Extra spaces in line. \n*This should be ##italic* and **this should # be #bold**.\n" +
|
||||
"~~This is crossed out~~ and `this is inline code`.\nEmoji: 🎉 🚀"
|
||||
|
||||
expectedPlainText := "Heading 1 Heading 2 Heading 3 Heading 4 Heading 5 Heading 6 Bullet list item 1 Bullet list item 2 Another >bullet Numbered list - item 1 Numbered list *item 2 Blockquote text Bold text1 Bold text2 Italic text1 Italic text2 Bold and Italic text Bold and Italic text Strikethrough text Line #has Inline code betwee # n Line has Inline code with backticks betwee_n Inline code in triple backticks multiline code python print('Hello, Markdown!') Link text Image alt-text Extra spaces in line. This should be ##italic and this should # be #bold. This is crossed out and this is inline code. Emoji: 🎉 🚀"
|
||||
|
||||
builder := NewMarkdownBuilder()
|
||||
assert.NotNil(t, builder)
|
||||
|
||||
builder.AddRaw(markdownText)
|
||||
plainText := builder.BuildPlainText()
|
||||
assert.Equal(t, expectedPlainText, plainText)
|
||||
}
|
||||
|
||||
@ -162,7 +162,7 @@ func (b *sarifBuilder) buildFilterResultMessageMarkdown(event *analyzer.Analyzer
|
||||
// SARIF spec mandates that we provide text in addition to markdown
|
||||
msg := sarif.NewMessage().
|
||||
WithMarkdown(md.Build()).
|
||||
WithText(md.Build())
|
||||
WithText(md.BuildPlainText())
|
||||
|
||||
return msg
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user