fix(paragraphs): fix ordered list rendering

Signed-off-by: Sphericalkat <amolele@gmail.com>
This commit is contained in:
Amogh Lele 2023-05-28 17:53:19 +05:30
parent f1ec8f7729
commit de151f4895
5 changed files with 54 additions and 29 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-d9a17752.css"> <link rel="stylesheet" href="/assets/index-1eb94859.css">
</head> </head>
<body> <body>
<h1 class="text-2xl">Hello, World!</h1> <h1 class="text-2xl">Hello, World!</h1>

View File

@ -14,13 +14,13 @@
rel="stylesheet"> rel="stylesheet">
<title>{{ .Title }}</title> <title>{{ .Title }}</title>
<link rel="stylesheet" href="/assets/show-9b4da228.css"> <link rel="stylesheet" href="/assets/show-d7d0e271.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">
<article class="prose lg:prose-xl p-20"> <article class="prose lg:prose-lg sm:prose-sm py-20">
<h2 class="text-2xl">{{.Title}}</h2> <h1 class="text-2xl">{{.Title}}</h1>
<p><a href="https://medium.com/u/{{.UserId}}">{{.Author}}</a> on {{.PublishDate}}</p> <p class="pb-8"><a href="https://medium.com/u/{{.UserId}}">{{.Author}}</a> on {{.PublishDate}}</p>
{{.Paragraphs}} {{.Paragraphs}}
</article> </article>
</body> </body>

View File

@ -17,9 +17,9 @@
</head> </head>
<body class="flex flex-col h-full w-full items-center"> <body class="flex flex-col h-full w-full items-center">
<article class="prose lg:prose-xl p-20"> <article class="prose lg:prose-lg sm:prose-sm py-20">
<h2 class="text-2xl">{{.Title}}</h2> <h1 class="text-2xl">{{.Title}}</h1>
<p><a href="https://medium.com/u/{{.UserId}}">{{.Author}}</a> on {{.PublishDate}}</p> <p class="pb-8"><a href="https://medium.com/u/{{.UserId}}">{{.Author}}</a> on {{.PublishDate}}</p>
{{.Paragraphs}} {{.Paragraphs}}
</article> </article>
</body> </body>

8
go.mod
View File

@ -2,13 +2,13 @@ module github.com/medium.rip
go 1.20 go 1.20
require github.com/sirupsen/logrus v1.9.2
require ( require (
github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/gofiber/template v1.8.1
github.com/gofiber/template v1.8.1 // indirect github.com/sirupsen/logrus v1.9.2
) )
require github.com/fsnotify/fsnotify v1.6.0 // indirect
require ( require (
github.com/andybalholm/brotli v1.0.5 // indirect github.com/andybalholm/brotli v1.0.5 // indirect
github.com/gofiber/fiber/v2 v2.46.0 github.com/gofiber/fiber/v2 v2.46.0

View File

@ -62,7 +62,13 @@ func ConvertParagraphs(paragraphs []entities.Paragraph) string {
var ps strings.Builder var ps strings.Builder
skipCount := 0
for i, p := range paragraphs { for i, p := range paragraphs {
if skipCount > 0 {
skipCount--
continue
}
switch p.Type { switch p.Type {
case "BQ", "MIXTAPE_EMBED", "PQ": case "BQ", "MIXTAPE_EMBED", "PQ":
children := ConvertMarkup(p.Text, p.Markups) children := ConvertMarkup(p.Text, p.Markups)
@ -92,10 +98,12 @@ func ConvertParagraphs(paragraphs []entities.Paragraph) string {
case "IMG": case "IMG":
ps.WriteString(convertImg(p)) ps.WriteString(convertImg(p))
case "OLI": case "OLI":
listItems := convertOli(paragraphs[i:]) listItems, skip := convertOli(paragraphs[i:])
skipCount = skip
ps.WriteString(fmt.Sprintf("<ol>%s</ol>", listItems)) ps.WriteString(fmt.Sprintf("<ol>%s</ol>", listItems))
case "ULI": case "ULI":
listItems := convertUli(paragraphs[i:]) listItems, skip := convertUli(paragraphs[i:])
skipCount = skip
ps.WriteString(fmt.Sprintf("<ul>%s</ul>", listItems)) ps.WriteString(fmt.Sprintf("<ul>%s</ul>", listItems))
case "P": case "P":
children := ConvertMarkup(p.Text, p.Markups) children := ConvertMarkup(p.Text, p.Markups)
@ -115,7 +123,7 @@ func ConvertParagraphs(paragraphs []entities.Paragraph) string {
func convertImg(p entities.Paragraph) string { func convertImg(p entities.Paragraph) string {
if p.Metadata != nil { if p.Metadata != nil {
captionMarkup := ConvertMarkup(p.Text, p.Markups) captionMarkup := ConvertMarkup(p.Text, p.Markups)
img := Image{ID : p.Metadata.ID} img := Image{ID: p.Metadata.ID}
img.Initialize(&p.Metadata.OriginalWidth, &p.Metadata.OriginalHeight) img.Initialize(&p.Metadata.OriginalWidth, &p.Metadata.OriginalHeight)
return fmt.Sprintf("<figure><img src=\"%s\" width=\"%d\" /><figcaption>%s</figcaption></figure>", img.src(), img.width(), captionMarkup) return fmt.Sprintf("<figure><img src=\"%s\" width=\"%d\" /><figcaption>%s</figcaption></figure>", img.src(), img.width(), captionMarkup)
} else { } else {
@ -123,22 +131,39 @@ func convertImg(p entities.Paragraph) string {
} }
} }
func convertOli(ps []entities.Paragraph) string { func convertOli(ps []entities.Paragraph) (string, int) {
if len(ps) != 0 && ps[0].Type == "OLI" { var sb strings.Builder
p := ps[0] count := 0
children := ConvertMarkup(p.Text, p.Markups)
return fmt.Sprintf("<li>%s</li>", children) + convertOli(ps[1:]) for _, p := range ps {
} else { if p.Type == "OLI" {
return "" children := ConvertMarkup(p.Text, p.Markups)
sb.WriteString(fmt.Sprintf("<li>%s</li>", children))
count++
} else {
break
}
} }
return sb.String(), count
} }
func convertUli(ps []entities.Paragraph) string { func convertUli(ps []entities.Paragraph) (string, int) {
if len(ps) != 0 && ps[0].Type == "ULI" { var sb strings.Builder
p := ps[0] count := 0
children := ConvertMarkup(p.Text, p.Markups)
return fmt.Sprintf("<li>%s</li>", children) + convertUli(ps[1:]) for _, p := range ps {
} else { if p.Type == "ULI" {
return "" if p.Text == "Rename the example.env to .env." {
fmt.Println("HERE")
}
children := ConvertMarkup(p.Text, p.Markups)
sb.WriteString(fmt.Sprintf("<li>%s</li>", children))
count++
} else {
break
}
} }
return sb.String(), count
} }