Skip to content

Commit f135a81

Browse files
zeripathsilverwindlunny
authored
Make Mermaid.js limit configurable (go-gitea#16519)
* Make Mermaid.js limit configurable Add `MERMAID_MAX_SOURCE_CHARACTERS` to `[markup]` settings to make the maximum size of a mermaid render configurable. Fix go-gitea#16513 Signed-off-by: Andrew Thornton <art27@cantab.net> * fixup! Make Mermaid.js limit configurable * Update custom/conf/app.example.ini Co-authored-by: silverwind <me@silverwind.io> * Update docs/content/doc/advanced/config-cheat-sheet.en-us.md Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
1 parent 342f338 commit f135a81

File tree

6 files changed

+22
-5
lines changed

6 files changed

+22
-5
lines changed

custom/conf/app.example.ini

+9
Original file line numberDiff line numberDiff line change
@@ -1985,6 +1985,15 @@ PATH =
19851985
;; Show template execution time in the footer
19861986
;SHOW_FOOTER_TEMPLATE_LOAD_TIME = true
19871987

1988+
1989+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1990+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1991+
;[markup]
1992+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1993+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1994+
;; Set the maximum number of characters in a mermaid source. (Set to -1 to disable limits)
1995+
;MERMAID_MAX_SOURCE_CHARACTERS = 5000
1996+
19881997
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19891998
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19901999
;[markup.sanitizer.1]

docs/content/doc/advanced/config-cheat-sheet.en-us.md

+2
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,8 @@ NB: You must have `DISABLE_ROUTER_LOG` set to `false` for this option to take ef
882882

883883
## Markup (`markup`)
884884

885+
- `MERMAID_MAX_SOURCE_CHARACTERS`: **5000**: Set the maximum size of a Mermaid source. (Set to -1 to disable)
886+
885887
Gitea can support Markup using external tools. The example below will add a markup named `asciidoc`.
886888

887889
```ini

modules/setting/markup.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ import (
1515

1616
// ExternalMarkupRenderers represents the external markup renderers
1717
var (
18-
ExternalMarkupRenderers []*MarkupRenderer
19-
ExternalSanitizerRules []MarkupSanitizerRule
18+
ExternalMarkupRenderers []*MarkupRenderer
19+
ExternalSanitizerRules []MarkupSanitizerRule
20+
MermaidMaxSourceCharacters int
2021
)
2122

2223
// MarkupRenderer defines the external parser configured in ini
@@ -40,6 +41,7 @@ type MarkupSanitizerRule struct {
4041
}
4142

4243
func newMarkup() {
44+
MermaidMaxSourceCharacters = Cfg.Section("markup").Key("MERMAID_MAX_SOURCE_CHARACTERS").MustInt(5000)
4345
ExternalMarkupRenderers = make([]*MarkupRenderer, 0, 10)
4446
ExternalSanitizerRules = make([]MarkupSanitizerRule, 0, 10)
4547

modules/templates/helper.go

+3
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,9 @@ func NewFuncMap() []template.FuncMap {
390390
html += "</span>"
391391
return template.HTML(html)
392392
},
393+
"MermaidMaxSourceCharacters": func() int {
394+
return setting.MermaidMaxSourceCharacters
395+
},
393396
}}
394397
}
395398

templates/base/head.tmpl

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
{{ end }}
6161
]).values()),
6262
{{end}}
63+
MermaidMaxSourceCharacters: {{MermaidMaxSourceCharacters}},
6364
};
6465
</script>
6566
<link rel="icon" href="{{AssetUrlPrefix}}/img/logo.svg" type="image/svg+xml">

web_src/js/markup/mermaid.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const MAX_SOURCE_CHARACTERS = 5000;
1+
const {MermaidMaxSourceCharacters} = window.config;
22

33
function displayError(el, err) {
44
el.closest('pre').classList.remove('is-loading');
@@ -26,8 +26,8 @@ export async function renderMermaid(els) {
2626
});
2727

2828
for (const el of els) {
29-
if (el.textContent.length > MAX_SOURCE_CHARACTERS) {
30-
displayError(el, new Error(`Mermaid source of ${el.textContent.length} characters exceeds the maximum allowed length of ${MAX_SOURCE_CHARACTERS}.`));
29+
if (MermaidMaxSourceCharacters >= 0 && el.textContent.length > MermaidMaxSourceCharacters) {
30+
displayError(el, new Error(`Mermaid source of ${el.textContent.length} characters exceeds the maximum allowed length of ${MermaidMaxSourceCharacters}.`));
3131
continue;
3232
}
3333

0 commit comments

Comments
 (0)