From 482043cf9caf508fcbdd2fd634084216735c1864 Mon Sep 17 00:00:00 2001 From: Emad Elsaid Date: Fri, 6 Jan 2023 13:25:00 +0100 Subject: [PATCH] add dir=auto server side instead of JS thank god --- extensions/rtl/widgets.go | 42 +++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/extensions/rtl/widgets.go b/extensions/rtl/widgets.go index 4e75d50e..61df4278 100644 --- a/extensions/rtl/widgets.go +++ b/extensions/rtl/widgets.go @@ -1,23 +1,39 @@ package rtl import ( - "html/template" - . "github.com/emad-elsaid/xlog" + "github.com/yuin/goldmark/ast" + "github.com/yuin/goldmark/parser" + "github.com/yuin/goldmark/text" + "github.com/yuin/goldmark/util" ) -const script template.HTML = ` - -` - func init() { - RegisterWidget(AFTER_VIEW_WIDGET, 1, scriptWidget) + MarkDownRenderer.Parser().AddOptions( + parser.WithASTTransformers( + util.Prioritized(addDirAuto(0), 0), + ), + ) } -func scriptWidget(_ Page) template.HTML { - return script +type addDirAuto int + +func (t addDirAuto) Transform(doc *ast.Document, reader text.Reader, pc parser.Context) { + tags := []ast.Node{} + + ast.Walk(doc, func(node ast.Node, entering bool) (ast.WalkStatus, error) { + kind := node.Kind() + if kind == ast.KindParagraph || + kind == ast.KindHeading || + kind == ast.KindList || + kind == ast.KindBlockquote { + tags = append(tags, node) + } + + return ast.WalkContinue, nil + }) + + for _, t := range tags { + t.SetAttributeString("dir", []byte("auto")) + } }