Skip to content

Commit

Permalink
add dir=auto server side instead of JS thank god
Browse files Browse the repository at this point in the history
  • Loading branch information
emad-elsaid committed Jan 6, 2023
1 parent ec1e492 commit 482043c
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions extensions/rtl/widgets.go
Original file line number Diff line number Diff line change
@@ -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 = `
<script>
// This is a hack to display right to left text correctly
document.querySelectorAll('.content *')
.forEach( ele => ele.setAttribute("dir", "auto") );
</script>
`

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"))
}
}

0 comments on commit 482043c

Please sign in to comment.