feat: finish markup converters

Signed-off-by: Sphericalkat <amolele@gmail.com>
This commit is contained in:
Amogh Lele 2023-05-28 14:40:23 +05:30
parent 4b8607b12a
commit 3de9bfc58c
4 changed files with 61 additions and 20 deletions

View File

@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ .Title }}</title>
<link rel="stylesheet" href="/assets/index-bab4abd8.css">
<link rel="stylesheet" href="/assets/index-544d9ea3.css">
</head>
<body>
<h1 class="text-2xl">Hello, World!</h1>

View File

@ -14,7 +14,7 @@
rel="stylesheet">
<title>{{ .Title }}</title>
<link rel="stylesheet" href="/assets/show-1b4526c3.css">
<link rel="stylesheet" href="/assets/show-7a43d2b5.css">
</head>
<body class="flex flex-col h-full w-full items-center">
@ -22,11 +22,11 @@
<h2 class="text-2xl">{{.Title}}</h2>
<p><a href="https://medium.com/u/{{.UserId}}">{{.Author}}</a> on {{.PublishDate}}</p>
{{range .Nodes}}
{{if eq .type "H3"}}
<h3>.text</h3>
{{if eq .Type "H3"}}
<h3>{{.Text}}</h3>
{{end}}
{{if eq .type "IMG"}}
<h3>.text</h3>
{{if eq .Type "IMG"}}
{{end}}
{{end}}
</article>

View File

@ -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 {
// }
// }
// case "CODE":
// case "EM":
// case "STRONG":
// }
// }
return markedUp.String()
}
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(`<a href="%s">%s</a>`, *markup.Href, child)
} else if markup.UserID != nil {
return fmt.Sprintf(`<a href="https://medium.com/u/%s">%s</a>`, markup.UserID, child)
}
case "CODE":
return fmt.Sprintf(`<code>%s</code>`, child)
case "EM":
return fmt.Sprintf(`<em>%s</em>`, child)
case "STRONG":
return fmt.Sprintf(`<strong>%s</strong>`, child)
default:
return fmt.Sprintf(`<code>%s</code>`, child)
}
return child
}

View File

@ -56,3 +56,22 @@ func TestRanges(t *testing.T) {
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>strong </strong><em><strong>and</strong></em><em> emphasized</em> only" {
t.Errorf("Expected markup to be <strong>strong </strong><em><strong>and</strong></em><em> emphasized</em> only, got %s", markup)
}
}