summaryrefslogtreecommitdiffstats
path: root/modules/markup
diff options
context:
space:
mode:
authorsilverwind <me@silverwind.io>2023-05-20 23:02:52 +0200
committerGitHub <noreply@github.com>2023-05-20 23:02:52 +0200
commit32d9c47ec7706d8f06e09b42e09a28d7a0e3c526 (patch)
tree36eeecea9af4a8200c2d8b49e835c88be60cbf2e /modules/markup
parent1698c15cba8f9eedfcb6af8226f3e97950480aca (diff)
downloadgitea-32d9c47ec7706d8f06e09b42e09a28d7a0e3c526.tar.gz
gitea-32d9c47ec7706d8f06e09b42e09a28d7a0e3c526.zip
Add RTL rendering support to Markdown (#24816)
Support RTL content in Markdown: ![image](https://github.com/go-gitea/gitea/assets/115237/dedb1b0c-2f05-40dc-931a-0d9dc81f7c97) Example document: https://try.gitea.io/silverwind/symlink-test/src/branch/master/bidi-text.md Same on GitHub: https://github.com/silverwind/symlink-test/blob/master/bidi-text.md `dir=auto` enables a browser heuristic that sets the text direction automatically. It is the only way to get automatic text direction. Ref: https://codeberg.org/Codeberg/Community/issues/1021 --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Diffstat (limited to 'modules/markup')
-rw-r--r--modules/markup/html.go2
-rw-r--r--modules/markup/markdown/goldmark.go10
-rw-r--r--modules/markup/renderer.go6
3 files changed, 15 insertions, 3 deletions
diff --git a/modules/markup/html.go b/modules/markup/html.go
index 11888b8536..da16bcd3cb 100644
--- a/modules/markup/html.go
+++ b/modules/markup/html.go
@@ -630,7 +630,7 @@ func mentionProcessor(ctx *RenderContext, node *html.Node) {
}
mentionedUsername := mention[1:]
- if processorHelper.IsUsernameMentionable != nil && processorHelper.IsUsernameMentionable(ctx.Ctx, mentionedUsername) {
+ if DefaultProcessorHelper.IsUsernameMentionable != nil && DefaultProcessorHelper.IsUsernameMentionable(ctx.Ctx, mentionedUsername) {
replaceContent(node, loc.Start, loc.End, createLink(util.URLJoin(setting.AppURL, mentionedUsername), mention, "mention"))
node = node.NextSibling.NextSibling
} else {
diff --git a/modules/markup/markdown/goldmark.go b/modules/markup/markdown/goldmark.go
index 816e93b700..f03a780900 100644
--- a/modules/markup/markdown/goldmark.go
+++ b/modules/markup/markdown/goldmark.go
@@ -47,6 +47,12 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
tocMode = rc.TOC
}
+ applyElementDir := func(n ast.Node) {
+ if markup.DefaultProcessorHelper.ElementDir != "" {
+ n.SetAttributeString("dir", []byte(markup.DefaultProcessorHelper.ElementDir))
+ }
+ }
+
attentionMarkedBlockquotes := make(container.Set[*ast.Blockquote])
_ = ast.Walk(node, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering {
@@ -69,6 +75,9 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
header.ID = util.BytesToReadOnlyString(id.([]byte))
}
tocList = append(tocList, header)
+ applyElementDir(v)
+ case *ast.Paragraph:
+ applyElementDir(v)
case *ast.Image:
// Images need two things:
//
@@ -171,6 +180,7 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
v.AppendChild(v, newChild)
}
}
+ applyElementDir(v)
case *ast.Text:
if v.SoftLineBreak() && !v.HardLineBreak() {
renderMetas := pc.Get(renderMetasKey).(map[string]string)
diff --git a/modules/markup/renderer.go b/modules/markup/renderer.go
index f2477f1e9e..0331c3742a 100644
--- a/modules/markup/renderer.go
+++ b/modules/markup/renderer.go
@@ -30,14 +30,16 @@ const (
type ProcessorHelper struct {
IsUsernameMentionable func(ctx context.Context, username string) bool
+
+ ElementDir string // the direction of the elements, eg: "ltr", "rtl", "auto", default to no direction attribute
}
-var processorHelper ProcessorHelper
+var DefaultProcessorHelper ProcessorHelper
// Init initialize regexps for markdown parsing
func Init(ph *ProcessorHelper) {
if ph != nil {
- processorHelper = *ph
+ DefaultProcessorHelper = *ph
}
NewSanitizer()