diff --git a/frontend/dist/index.html b/frontend/dist/index.html index 6e19d7a..b737067 100644 --- a/frontend/dist/index.html +++ b/frontend/dist/index.html @@ -6,7 +6,7 @@ {{ .Title }} - +

Hello, World!

diff --git a/frontend/dist/show.html b/frontend/dist/show.html index 0560098..fa61c49 100644 --- a/frontend/dist/show.html +++ b/frontend/dist/show.html @@ -14,7 +14,7 @@ rel="stylesheet"> {{ .Title }} - + @@ -22,11 +22,11 @@

{{.Title}}

{{.Author}} on {{.PublishDate}}

{{range .Nodes}} - {{if eq .type "H3"}} -

.text

+ {{if eq .Type "H3"}} +

{{.Text}}

{{end}} - {{if eq .type "IMG"}} -

.text

+ {{if eq .Type "IMG"}} + {{end}} {{end}} diff --git a/pkg/converters/markup_converter.go b/pkg/converters/markup_converter.go index e1119d1..88665f2 100644 --- a/pkg/converters/markup_converter.go +++ b/pkg/converters/markup_converter.go @@ -1,7 +1,9 @@ package converters import ( + "fmt" "sort" + "strings" "github.com/medium.rip/pkg/entities" ) @@ -67,20 +69,40 @@ func ranges(text string, markups []entities.Markup) []RangeWithMarkup { return ranges } -func Convert(text string, markups []entities.Markup) { - // for _, m := range markups { - // switch m.Type { - // case "A": - // if m.Href != nil { +func Convert(text string, markups []entities.Markup) string { + var markedUp strings.Builder + for _, r := range ranges(text, markups) { + textToWrap := string(text[r.Range[0]:r.Range[1]]) + markedUp.WriteString(WrapInMarkups(textToWrap, r.Markups)) + } - // } else if { - // m.UserID != nil { + return markedUp.String() +} - // } - // } - // case "CODE": - // case "EM": - // case "STRONG": - // } - // } +func WrapInMarkups(child string, markups []entities.Markup) string { + if len(markups) == 0 { + return child + } + markedUp := markupNodeInContainer(child, markups[0]) + return WrapInMarkups(markedUp, markups[1:]) +} + +func markupNodeInContainer(child string, markup entities.Markup) string { + switch markup.Type { + case "A": + if markup.Href != nil { + return fmt.Sprintf(`%s`, *markup.Href, child) + } else if markup.UserID != nil { + return fmt.Sprintf(`%s`, markup.UserID, child) + } + case "CODE": + return fmt.Sprintf(`%s`, child) + case "EM": + return fmt.Sprintf(`%s`, child) + case "STRONG": + return fmt.Sprintf(`%s`, child) + default: + return fmt.Sprintf(`%s`, child) + } + return child } \ No newline at end of file diff --git a/pkg/converters/markup_converter_test.go b/pkg/converters/markup_converter_test.go index 372ce32..69e26d5 100644 --- a/pkg/converters/markup_converter_test.go +++ b/pkg/converters/markup_converter_test.go @@ -55,4 +55,23 @@ func TestRanges(t *testing.T) { if len(ranges[3].Markups) != 0 { t.Errorf("Expected markup to be empty, got %v", ranges[3].Markups) } +} + +func TestConvert(t *testing.T) { + markup := Convert("strong and emphasized only", []entities.Markup{ + { + Type: "STRONG", + Start: 0, + End: 10, + }, + { + Type: "EM", + Start: 7, + End: 21, + }, + }) + + if markup != "strong and emphasized only" { + t.Errorf("Expected markup to be strong and emphasized only, got %s", markup) + } } \ No newline at end of file