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

View File

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

View File

@ -1,7 +1,9 @@
package converters package converters
import ( import (
"fmt"
"sort" "sort"
"strings"
"github.com/medium.rip/pkg/entities" "github.com/medium.rip/pkg/entities"
) )
@ -67,20 +69,40 @@ func ranges(text string, markups []entities.Markup) []RangeWithMarkup {
return ranges return ranges
} }
func Convert(text string, markups []entities.Markup) { func Convert(text string, markups []entities.Markup) string {
// for _, m := range markups { var markedUp strings.Builder
// switch m.Type { for _, r := range ranges(text, markups) {
// case "A": textToWrap := string(text[r.Range[0]:r.Range[1]])
// if m.Href != nil { markedUp.WriteString(WrapInMarkups(textToWrap, r.Markups))
}
// } else if {
// m.UserID != nil { return markedUp.String()
}
// }
// } func WrapInMarkups(child string, markups []entities.Markup) string {
// case "CODE": if len(markups) == 0 {
// case "EM": return child
// case "STRONG": }
// } 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) 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)
}
}