adds title derivation
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"regexp"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
@@ -56,6 +57,21 @@ type Note struct {
|
|||||||
Content template.HTML
|
Content template.HTML
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n Note) withTitle() Note {
|
||||||
|
fstLine := rexpNewLine.Split(n.Text, -1)[0]
|
||||||
|
maxLength := 25
|
||||||
|
if len(fstLine) < 25 {
|
||||||
|
maxLength = len(fstLine)
|
||||||
|
}
|
||||||
|
n.Title = rexpNonAlphaNum.ReplaceAllString(fstLine[:maxLength], "")
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
rexpNewLine = regexp.MustCompile("[\n\r]")
|
||||||
|
rexpNonAlphaNum = regexp.MustCompile("[`~!@#$%^&*_|+=?;:'\",.<>{}\\/]")
|
||||||
|
)
|
||||||
|
|
||||||
func note(c echo.Context, db *sql.DB) (Note, int) {
|
func note(c echo.Context, db *sql.DB) (Note, int) {
|
||||||
stmt, err := db.Prepare("select id, text, strftime('%s', published) as published," +
|
stmt, err := db.Prepare("select id, text, strftime('%s', published) as published," +
|
||||||
" strftime('%s',edited) as edited, password, views from notes where id = ?")
|
" strftime('%s',edited) as edited, password, views from notes where id = ?")
|
||||||
@@ -71,12 +87,11 @@ func note(c echo.Context, db *sql.DB) (Note, int) {
|
|||||||
c.Logger().Error(err)
|
c.Logger().Error(err)
|
||||||
return note404, http.StatusNotFound
|
return note404, http.StatusNotFound
|
||||||
}
|
}
|
||||||
// cand := regexp.MustCompile("[\n\r]").Split(text, 1)
|
|
||||||
// fmt.Println("CANDIDATE", cand[0])
|
|
||||||
return Note{
|
return Note{
|
||||||
ID: id,
|
ID: id,
|
||||||
|
Text: text,
|
||||||
Content: mdTmplHTML([]byte(text)),
|
Content: mdTmplHTML([]byte(text)),
|
||||||
}, http.StatusOK
|
}.withTitle(), http.StatusOK
|
||||||
}
|
}
|
||||||
|
|
||||||
func md2html(c echo.Context, name string) (Note, int) {
|
func md2html(c echo.Context, name string) (Note, int) {
|
||||||
|
|||||||
Reference in New Issue
Block a user