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

View File

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

BIN
cetvorke

Binary file not shown.

View File

@@ -3,9 +3,8 @@ package database
import ( import (
"database/sql" "database/sql"
"fmt" "fmt"
"strings"
"time" "time"
"github.com/russross/blackfriday/v2"
) )
type Post struct { type Post struct {
@@ -25,10 +24,49 @@ func (p *Post) GemtextPage() string {
} }
func (p *Post) HTMLPage() string { func (p *Post) HTMLPage() string {
extensions := blackfriday.CommonExtensions | blackfriday.HardLineBreak var content string
result := fmt.Sprintf("<h1>%s</h1>\n%s", p.Title, blackfriday.Run([]byte(p.MarkdownContent), blackfriday.WithExtensions(extensions))) if p.GemtextContent.Valid {
result += fmt.Sprintf("%s", p.Date) content = p.GemtextContent.String
return result 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) { 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/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 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= 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 # github.com/russross/blackfriday/v2 v2.1.0
## explicit ## explicit
github.com/russross/blackfriday/v2 github.com/russross/blackfriday/v2
# tildegit.org/nihilazo/go-gemtext v0.0.0-20201114202124-890be6eb3742
## explicit; go 1.15