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

@@ -57,9 +57,6 @@ const editorTemplate = `
background-color: #ffffff;
}
</style>
<link rel="stylesheet" href="/static/pen.css" />
<script src="/static/markdown.js"></script>
<script src="/static/pen.js"></script>
</head>
<body>
@@ -74,16 +71,15 @@ const editorTemplate = `
<input type="text" name="title" placeholder="Naslov" value="{{.Title}}" />
<input name="ID" type="hidden" value="{{.ID}}" />
<div id="editorcontainer">
<div id="editor">{{.MarkdownContent}}</div>
<textarea id="editor">{{.GemtextContent}}</textarea>
</div>
<input type="submit" value="Sačuvaj" />
</form>
</div>
</content>
<footer>
&nbsp;
</footer>
<script>
const editor = new Pen('#editor');
</script>
</body>
</html>
`

View File

@@ -1,6 +1,7 @@
package cethttp
import (
"database/sql"
"net/http"
"strconv"
@@ -31,7 +32,7 @@ func editHandler(w http.ResponseWriter, r *http.Request) {
editedTitle := r.FormValue("title")
// Update the post content.
post.Content = editedContent
post.GemtextContent = sql.NullString{String: editedContent, Valid: true}
post.Title = editedTitle
// Save the edited post to the database.

BIN
cetvorke

Binary file not shown.

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) {

2
go.sum
View File

@@ -11,3 +11,5 @@ github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
tildegit.org/nihilazo/go-gemtext v0.0.0-20201114202124-890be6eb3742 h1:SE9EroZwQEH3mHxWah4UoHQGgBTa008VIjmh2PqSZXw=
tildegit.org/nihilazo/go-gemtext v0.0.0-20201114202124-890be6eb3742/go.mod h1:KOdDbr9CNSpgmfNfCEjpDhaGPCd86Mp5+XGrZDBKZvo=

2
vendor/modules.txt vendored
View File

@@ -16,3 +16,5 @@ github.com/mattn/go-sqlite3
# github.com/russross/blackfriday/v2 v2.1.0
## explicit
github.com/russross/blackfriday/v2
# tildegit.org/nihilazo/go-gemtext v0.0.0-20201114202124-890be6eb3742
## explicit; go 1.15