mirror of
https://github.com/SphericalKat/medium.rip.git
synced 2024-12-27 10:05:58 +00:00
feat: finish markup converters
Signed-off-by: Sphericalkat <amolele@gmail.com>
This commit is contained in:
parent
4b8607b12a
commit
3de9bfc58c
2
frontend/dist/index.html
vendored
2
frontend/dist/index.html
vendored
@ -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>
|
||||||
|
10
frontend/dist/show.html
vendored
10
frontend/dist/show.html
vendored
@ -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>
|
||||||
|
@ -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
|
||||||
}
|
}
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user