Editor to gemtext half way

This commit is contained in:
Senad Uka
2023-09-16 16:59:16 +02:00
parent 1765a150bb
commit 201579ad87
6 changed files with 53 additions and 14 deletions

View File

@@ -3,9 +3,8 @@ package database
import (
"database/sql"
"fmt"
"strings"
"time"
"github.com/russross/blackfriday/v2"
)
type Post struct {
@@ -25,10 +24,49 @@ func (p *Post) GemtextPage() string {
}
func (p *Post) HTMLPage() string {
extensions := blackfriday.CommonExtensions | blackfriday.HardLineBreak
result := fmt.Sprintf("<h1>%s</h1>\n%s", p.Title, blackfriday.Run([]byte(p.MarkdownContent), blackfriday.WithExtensions(extensions)))
result += fmt.Sprintf("%s", p.Date)
return result
var content string
if p.GemtextContent.Valid {
content = p.GemtextContent.String
if strings.HasPrefix(content, "{") {
content = strings.TrimPrefix(content, "{")
}
if strings.HasSuffix(content, "true}") {
content = strings.TrimSuffix(content, "true}")
}
} else {
content = ""
}
lines := strings.Split(content, "\n")
var htmlLines []string
for _, line := range lines {
if strings.HasPrefix(line, "#") {
htmlLines = append(htmlLines, "<h1>"+line[1:]+"</h1>")
} else if strings.HasPrefix(line, "##") {
htmlLines = append(htmlLines, "<h2>"+line[2:]+"</h2>")
} else if strings.HasPrefix(line, "###") {
htmlLines = append(htmlLines, "<h3>"+line[3:]+"</h3>")
} else if strings.HasPrefix(line, "* ") {
htmlLines = append(htmlLines, "<ul><li>"+line[2:]+"</li></ul>")
} else if strings.HasPrefix(line, ">") {
htmlLines = append(htmlLines, "<blockquote>"+line[1:]+"</blockquote>")
} else if strings.HasPrefix(line, "=>") {
parts := strings.Fields(line[2:])
if len(parts) > 1 {
url := parts[0]
text := strings.Join(parts[1:], " ")
htmlLines = append(htmlLines, fmt.Sprintf(`<a href="%s.html">%s</a>`, url, text))
} else if len(parts) == 1 {
url := parts[0]
htmlLines = append(htmlLines, fmt.Sprintf(`<a href="%s.html">%s</a>`, url, url))
}
} else {
htmlLines = append(htmlLines, "<p>"+line+"</p>")
}
}
return strings.Join(htmlLines, "\n")
}
func GetPost(id int) (*Post, error) {