diff --git a/public/parsing.wasm b/public/parsing.wasm index eb7f349c..43fbc82d 100644 Binary files a/public/parsing.wasm and b/public/parsing.wasm differ diff --git a/src/config/config.go.example b/src/config/config.go.example index 393452bd..ec2a3762 100644 --- a/src/config/config.go.example +++ b/src/config/config.go.example @@ -1,16 +1,23 @@ package config +import ( + "github.com/jackc/pgx/v4" + "github.com/rs/zerolog" +) + var Config = HMNConfig{ - Env: Dev, - Addr: ":9001", - BaseUrl: "http://handmade.local:9001", - LogLevel: zerolog.TraceLevel, + Env: Dev, + Addr: ":9001", + PrivateAddr: ":9002", + BaseUrl: "http://handmade.local:9001", + LogLevel: zerolog.TraceLevel, Postgres: PostgresConfig{ User: "hmn", Password: "password", Hostname: "handmade.local", Port: 5454, DbName: "hmn", + LogLevel: pgx.LogLevelTrace, MinConn: 2, // Keep these low for dev, high for production MaxConn: 2, }, diff --git a/src/parsing/parsing.go b/src/parsing/parsing.go index d433f269..9e81ded3 100644 --- a/src/parsing/parsing.go +++ b/src/parsing/parsing.go @@ -9,37 +9,23 @@ import ( "github.com/yuin/goldmark/util" ) -var previewMarkdown = goldmark.New( - goldmark.WithExtensions( - extension.GFM, - highlightExtension, - SpoilerExtension{}, - EmbedExtension{ - Preview: true, - }, - MathjaxExtension{}, - BBCodeExtension{ - Preview: true, - }, - ), -) -var realMarkdown = goldmark.New( - goldmark.WithExtensions( - extension.GFM, - highlightExtension, - SpoilerExtension{}, - EmbedExtension{}, - MathjaxExtension{}, - BBCodeExtension{}, - ), +// Used for rendering real-time previews of post content. +var PreviewMarkdown = goldmark.New( + goldmark.WithExtensions(makeGoldmarkExtensions(true)...), ) -func ParsePostInput(source string, preview bool) string { - md := realMarkdown - if preview { - md = previewMarkdown - } +// Used for generating the final HTML for a post. +var RealMarkdown = goldmark.New( + goldmark.WithExtensions(makeGoldmarkExtensions(false)...), +) +// Used for generating plain-text previews of posts. +var PlaintextMarkdown = goldmark.New( + goldmark.WithExtensions(makeGoldmarkExtensions(false)...), + goldmark.WithRenderer(plaintextRenderer{}), +) + +func ParsePostInput(source string, md goldmark.Markdown) string { var buf bytes.Buffer if err := md.Convert([]byte(source), &buf); err != nil { panic(err) @@ -48,6 +34,21 @@ func ParsePostInput(source string, preview bool) string { return buf.String() } +func makeGoldmarkExtensions(preview bool) []goldmark.Extender { + return []goldmark.Extender{ + extension.GFM, + highlightExtension, + SpoilerExtension{}, + EmbedExtension{ + Preview: preview, + }, + MathjaxExtension{}, + BBCodeExtension{ + Preview: preview, + }, + } +} + var highlightExtension = highlighting.NewHighlighting( highlighting.WithFormatOptions(HMNChromaOptions...), highlighting.WithWrapperRenderer(func(w util.BufWriter, context highlighting.CodeBlockContext, entering bool) { diff --git a/src/parsing/parsing_test.go b/src/parsing/parsing_test.go index 546ee748..ebfc30b7 100644 --- a/src/parsing/parsing_test.go +++ b/src/parsing/parsing_test.go @@ -10,14 +10,14 @@ import ( func TestMarkdown(t *testing.T) { t.Run("fenced code blocks", func(t *testing.T) { t.Run("multiple lines", func(t *testing.T) { - html := ParsePostInput("```\nmultiple lines\n\tof code\n```", false) + html := ParsePostInput("```\nmultiple lines\n\tof code\n```", RealMarkdown) t.Log(html) assert.Equal(t, 1, strings.Count(html, " {{ end }} @@ -110,6 +111,9 @@ MathJax.typeset(); } + goLoaded.then(() => { + updatePreview(); + }); tf.addEventListener('input', () => { updatePreview(); }); diff --git a/src/website/forums.go b/src/website/forums.go index 2ab5ff1f..a46006ce 100644 --- a/src/website/forums.go +++ b/src/website/forums.go @@ -581,11 +581,17 @@ func ForumNewThreadSubmit(c *RequestContext) ResponseData { sticky = true } - parsed := parsing.ParsePostInput(unparsed, false) + parsed := parsing.ParsePostInput(unparsed, parsing.RealMarkdown) now := time.Now() - ip := net.ParseIP(c.Req.RemoteAddr) + const previewMaxLength = 100 + parsedPlaintext := parsing.ParsePostInput(unparsed, parsing.PlaintextMarkdown) + preview := parsedPlaintext + if len(preview) > previewMaxLength-1 { + preview = preview[:previewMaxLength-1] + "…" + } + // Create thread var threadId int err = tx.QueryRow(c.Context(), @@ -615,7 +621,7 @@ func ForumNewThreadSubmit(c *RequestContext) ResponseData { now, currentCatId, threadId, - "lol", // TODO: Actual previews + preview, -1, c.CurrentUser.ID, models.CatKindForum,