Skip to content

Commit

Permalink
add markdown editor
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanfaerman committed Jan 30, 2024
1 parent 1509c58 commit 25b8dd4
Show file tree
Hide file tree
Showing 24 changed files with 1,247 additions and 233 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/jellydator/ttlcache/v3 v3.1.1
github.com/justinas/nosurf v1.1.1
github.com/magefile/mage v1.15.0
github.com/microcosm-cc/bluemonday v1.0.26
github.com/mrz1836/postmark v1.6.1
github.com/oklog/ulid/v2 v2.1.0
github.com/pkg/errors v0.9.1
Expand All @@ -34,6 +35,7 @@ require (
github.com/unrolled/render v1.6.0
github.com/vearutop/statigz v1.4.0
github.com/webview/webview_go v0.0.0-20230901181450-5a14030a9070
github.com/yuin/goldmark v1.4.13
golang.org/x/sync v0.3.0
gopkg.in/yaml.v2 v2.4.0
)
Expand All @@ -43,6 +45,7 @@ require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230321174746-8dcc6526cfb1 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/benbjohnson/clock v1.1.0 // indirect
github.com/bytecodealliance/wasmtime-go/v8 v8.0.0 // indirect
github.com/cenk/backoff v2.2.1+incompatible // indirect
Expand All @@ -59,6 +62,7 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/cel-go v0.17.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0 h1:jfIu9sQUG6Ig
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdKJDJF9BV14lnkjHmOQgcvEKgtqs5a1N3LNdJhGE=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/bool64/dev v0.2.28 h1:6ayDfrB/jnNr2iQAZHI+uT3Qi6rErSbJYQs1y8rSrwM=
Expand Down Expand Up @@ -100,6 +102,8 @@ github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbu
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
Expand Down Expand Up @@ -143,6 +147,8 @@ github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/microcosm-cc/bluemonday v1.0.26 h1:xbqSvqzQMeEHCqMi64VAs4d8uy6Mequs3rQ0k/Khz58=
github.com/microcosm-cc/bluemonday v1.0.26/go.mod h1:JyzOCs9gkyQyjs+6h10UEVSe02CGwkhd72Xdqh78TWs=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mrz1836/postmark v1.6.1 h1:UHAs9WuZEBZj12MdZ/iVRyoC4tq3ODTdYhE17OhJeJ4=
Expand Down Expand Up @@ -223,6 +229,8 @@ github.com/webview/webview_go v0.0.0-20230901181450-5a14030a9070 h1:imZLWyo1onde
github.com/webview/webview_go v0.0.0-20230901181450-5a14030a9070/go.mod h1:yE65LFCeWf4kyWD5re+h4XNvOHJEXOCOuJZ4v8l5sgk=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
Expand Down
32 changes: 23 additions & 9 deletions internal/dao/accounts.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/dao/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions internal/events/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package events

type (
AccountCreated struct {
ID int64
Email string
}

AccountProfileUpdated struct {
ID int64
Name string
About string
Callsign string
}

AccountSessionOpened struct {
ID int64
UserAgent string
IP string
}
)

func (AccountCreated) Event() string { return "account.created" }
func (AccountProfileUpdated) Event() string { return "account.profile_updated" }
func (AccountSessionOpened) Event() string { return "account.session_opened" }
29 changes: 2 additions & 27 deletions internal/events/event.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,5 @@
package events

import "fmt"

type subscriber struct {
connection chan any
}

type Bus struct {
subscribers map[string][]subscriber
}

func NewBus() *Bus {
return &Bus{
subscribers: make(map[string][]subscriber),
}
}

func (b *Bus) Subscribe(event any) chan any {
c := make(chan any)
s := subscriber{connection: c}
b.subscribers[fmt.Sprintf("%T", event)] = append(b.subscribers[fmt.Sprintf("%T", event)], s)
return c
}

func (b *Bus) Publish(event any) {
for _, s := range b.subscribers[fmt.Sprintf("%T", event)] {
s.connection <- event
}
type Event interface {
Event() string
}
9 changes: 9 additions & 0 deletions internal/events/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,12 @@ type (
// NetSessionClosed occurs when a net session is closed.
NetSessionClosed struct{}
)

func (NetCheckinHeard) Event() string { return "checkin.heard" }
func (NetCheckinVerified) Event() string { return "checkin.verified" }
func (NetCheckinAcked) Event() string { return "checkin.acked" }
func (NetCheckinCorrected) Event() string { return "checkin.corrected" }
func (NetCheckinRevoked) Event() string { return "checkin.revoked" }
func (NetSessionScheduled) Event() string { return "session.scheduled" }
func (NetSessionOpened) Event() string { return "session.opened" }
func (NetSessionClosed) Event() string { return "session.closed" }
8 changes: 7 additions & 1 deletion internal/handlers/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ func (h account) Show(w http.ResponseWriter, r *http.Request) {
return
}

a.About = services.Markdown.MustRenderString(a.About)

v := views.Account{
Account: a,
}
Expand Down Expand Up @@ -132,16 +134,20 @@ func (h account) Update(w http.ResponseWriter, r *http.Request) {

inputErrs := views.AccountEditFormErrors{}
input := views.AccountEditFormInput{
Name: strings.TrimSpace(r.Form.Get("name")),
Name: strings.TrimSpace(r.Form.Get("name")),
About: strings.TrimSpace(r.Form.Get("about")),
}
a.Name = input.Name
a.About = input.About
err = services.Account.Update(r.Context(), a)
if err != nil {
if errs, ok := err.(services.ValidationError); ok {
for field, e := range errs {
switch field {
case "Account.Name":
inputErrs.Name = e
case "Account.About":
inputErrs.Name = e
}
}
v.EditFormWithErrors(input, inputErrs).Render(ctx, w)
Expand Down
72 changes: 72 additions & 0 deletions internal/handlers/markdown.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package handlers

import (
"errors"
"fmt"
"net/http"

"github.com/go-chi/chi"
"github.com/ryanfaerman/netctl/internal/middleware"
"github.com/ryanfaerman/netctl/internal/services"
"github.com/ryanfaerman/netctl/internal/views"
"github.com/ryanfaerman/netctl/web"
"github.com/ryanfaerman/netctl/web/named"
)

type Markdown struct{}

func init() {
global.handlers = append(global.handlers, Markdown{})
}

func (h Markdown) Routes(r chi.Router) {
r.Use(middleware.HTMXOnly)
web.CSRFExempt("/-/tools/markdown-render/*")
r.Post(named.Route("markdown-preview", "/-/tools/markdown-render/{name}"), h.Preview)
r.Post(named.Route("markdown-editor", "/-/tools/markdown-editor/{name}"), h.Editor)
}

func (h Markdown) Preview(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
// ctx := services.CSRF.GetContext(r.Context(), r)
field := chi.URLParam(r, "name")
if field == "" {
ErrorHandler(errors.New("no field name provided"))(w, r)
return
}

attrs, err := views.DecodeInputAttrs(r.Form.Get(fmt.Sprintf("_%s-config", field)))
if err != nil {
ErrorHandler(err)(w, r)
return
}

attrs.Value = r.Form.Get(field)
attrs.MarkdownModePreview = true
if attrs.Value != "" {
attrs.MarkdownPreviewBody = services.Markdown.MustRenderString(attrs.Value)
}

views.InputTextArea(field, attrs).Render(r.Context(), w)
}

func (h Markdown) Editor(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
// ctx := services.CSRF.GetContext(r.Context(), r)
field := chi.URLParam(r, "name")
if field == "" {
ErrorHandler(errors.New("no field name provided"))(w, r)
return
}

attrs, err := views.DecodeInputAttrs(r.Form.Get(fmt.Sprintf("_%s-config", field)))
if err != nil {
ErrorHandler(err)(w, r)
return
}

attrs.Value = r.Form.Get(field)
attrs.MarkdownModePreview = false

views.InputTextArea(field, attrs).Render(r.Context(), w)
}
Loading

0 comments on commit 25b8dd4

Please sign in to comment.