Skip to content

Commit

Permalink
[MI-3554] Did some minor improvements in the ConvertToMD function and…
Browse files Browse the repository at this point in the history
… added new unit tests (#328)

* [MI-3554] Did some minor improvements in the ConvertToMD function and added new unit tests

* [MI-3554] Review fix: converted map to array

* [MI-3554] Review fixes: Changed variable name
  • Loading branch information
manojmalik20 authored Oct 5, 2023
1 parent 185c422 commit 18b11de
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
25 changes: 18 additions & 7 deletions server/markdown/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,24 @@ import (
"github.com/mattn/godown"
)

var stringsToCheckForHTML = []string{
"<div",
"<p ",
"<p>",
"<img ",
}

func ConvertToMD(text string) string {
if !(strings.Contains(text, "<div") || strings.Contains(text, "<p") || strings.Contains(text, "<img")) {
return text
}
var sb strings.Builder
if err := godown.Convert(&sb, strings.NewReader(text), nil); err != nil {
return text
for _, tag := range stringsToCheckForHTML {
if strings.Contains(text, tag) {
var sb strings.Builder
if err := godown.Convert(&sb, strings.NewReader(text), nil); err != nil {
return text
}

return sb.String()
}
}
return sb.String()

return text
}
9 changes: 7 additions & 2 deletions server/markdown/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ func TestConvertToMD(t *testing.T) {
}{
{
description: "Text does not contain tags",
text: "This is text area",
expectedOutput: "This is text area",
text: "This is text area <></>",
expectedOutput: "This is text area <></>",
},
{
description: "Text contains div and paragraph tags",
text: "This is text area with <div> and <p> tags",
expectedOutput: "This is text area with \n and \n tags\n\n\n\n\n",
},
{
description: "Text contains paragraph and image tags",
text: "This is text area with <img src=''> and <p class=''>Paragraph</p> tags",
expectedOutput: "This is text area with ![]() and \nParagraph\n\n\n tags\n",
},
} {
t.Run(testCase.description, func(t *testing.T) {
text := ConvertToMD(testCase.text)
Expand Down

0 comments on commit 18b11de

Please sign in to comment.