nip54: djot parser.

This commit is contained in:
fiatjaf
2025-11-23 18:34:31 -03:00
parent bb5413c0ed
commit 45f45fafaa
4 changed files with 47 additions and 20 deletions

3
go.mod
View File

@@ -24,7 +24,6 @@ require (
github.com/puzpuzpuz/xsync/v3 v3.5.1
github.com/rs/cors v1.11.1
github.com/rs/zerolog v1.33.0
github.com/segmentio/encoding v0.5.3
github.com/stretchr/testify v1.10.0
github.com/tidwall/gjson v1.18.0
github.com/tyler-smith/go-bip32 v1.0.0
@@ -43,6 +42,7 @@ require (
require (
github.com/dgraph-io/ristretto/v2 v2.3.0
github.com/go-git/go-git/v5 v5.16.3
github.com/sivukhin/godjot v1.0.6
github.com/templexxx/cpu v0.0.1
github.com/templexxx/xhex v0.0.0-20200614015412-aed53437177b
)
@@ -93,7 +93,6 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.14.1 // indirect
github.com/savsgio/gotils v0.0.0-20240704082632-aef3928b8a38 // indirect
github.com/segmentio/asm v1.1.3 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect

6
go.sum
View File

@@ -207,10 +207,8 @@ github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/savsgio/gotils v0.0.0-20240704082632-aef3928b8a38 h1:D0vL7YNisV2yqE55+q0lFuGse6U8lxlg7fYTctlT5Gc=
github.com/savsgio/gotils v0.0.0-20240704082632-aef3928b8a38/go.mod h1:sM7Mt7uEoCeFSCBM+qBrqvEo+/9vdmj19wzp3yzUhmg=
github.com/segmentio/asm v1.1.3 h1:WM03sfUOENvvKexOLp+pCqgb/WDjsi7EK8gIsICtzhc=
github.com/segmentio/asm v1.1.3/go.mod h1:Ld3L4ZXGNcSLRg4JBsZ3//1+f/TjYl0Mzen/DQy1EJg=
github.com/segmentio/encoding v0.5.3 h1:OjMgICtcSFuNvQCdwqMCv9Tg7lEOXGwm1J5RPQccx6w=
github.com/segmentio/encoding v0.5.3/go.mod h1:HS1ZKa3kSN32ZHVZ7ZLPLXWvOVIiZtyJnO1gPH1sKt0=
github.com/sivukhin/godjot v1.0.6 h1:yoRD+hlcDbSxP9Gd/KRVlEFXgtGyZyt0CHwhY6Gk3EQ=
github.com/sivukhin/godjot v1.0.6/go.mod h1:wA6KdR4Z+XpwdwyViPDLWYYxT72pKjNc6XGA9I025gM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.1.5-0.20170601210322-f6abca593680/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=

View File

@@ -4,6 +4,8 @@ import (
"strings"
"unicode"
"github.com/sivukhin/godjot/djot_parser"
"github.com/sivukhin/godjot/html_writer"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
@@ -24,3 +26,13 @@ func NormalizeIdentifier(name string) string {
return string(b)
}
func ArticleAsHTML(content string) string {
ast := djot_parser.BuildDjotAst([]byte(content))
context := djot_parser.NewConversionContext("html", djot_parser.DefaultConversionRegistry)
writer := &html_writer.HtmlWriter{}
for _, node := range ast {
context.ConvertDjotToHtml(writer, node)
}
return writer.String()
}

View File

@@ -1,23 +1,41 @@
package nip54
import (
"fmt"
"strings"
"testing"
)
func TestNormalization(t *testing.T) {
for _, vector := range []struct {
before string
after string
func TestArticleAsHTML(t *testing.T) {
tests := []struct {
name string
input string
contains []string
}{
{" hello ", "hello"},
{"Goodbye", "goodbye"},
{"the long and winding road / that leads to your door", "the-long-and-winding-road---that-leads-to-your-door"},
{"it's 平仮名", "it-s-平仮名"},
} {
if norm := NormalizeIdentifier(vector.before); norm != vector.after {
fmt.Println([]byte(vector.after), []byte(norm))
t.Fatalf("%s: %s != %s", vector.before, norm, vector.after)
}
{
name: "simple paragraph",
input: "Hello world",
contains: []string{"<p>", "Hello world", "</p>"},
},
{
name: "emphasis",
input: "*Hello* _world_",
contains: []string{"<strong>", "Hello", "</strong>", "<em>", "world", "</em>"},
},
{
name: "heading",
input: "# Title",
contains: []string{"<h1>", "Title", "</h1>"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ArticleAsHTML(tt.input)
for _, expected := range tt.contains {
if !strings.Contains(result, expected) {
t.Errorf("ArticleAsHTML() output does not contain %q\nGot: %s", expected, result)
}
}
})
}
}