mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	Make Mermaid.js limit configurable (#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 #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>
This commit is contained in:
		@@ -1985,6 +1985,15 @@ PATH =
 | 
				
			|||||||
;; Show template execution time in the footer
 | 
					;; Show template execution time in the footer
 | 
				
			||||||
;SHOW_FOOTER_TEMPLATE_LOAD_TIME = true
 | 
					;SHOW_FOOTER_TEMPLATE_LOAD_TIME = true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 | 
				
			||||||
 | 
					;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 | 
				
			||||||
 | 
					;[markup]
 | 
				
			||||||
 | 
					;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 | 
				
			||||||
 | 
					;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 | 
				
			||||||
 | 
					;; Set the maximum number of characters in a mermaid source. (Set to -1 to disable limits)
 | 
				
			||||||
 | 
					;MERMAID_MAX_SOURCE_CHARACTERS = 5000
 | 
				
			||||||
 | 
					
 | 
				
			||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 | 
					;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 | 
				
			||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 | 
					;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 | 
				
			||||||
;[markup.sanitizer.1]
 | 
					;[markup.sanitizer.1]
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -882,6 +882,8 @@ NB: You must have `DISABLE_ROUTER_LOG` set to `false` for this option to take ef
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
## Markup (`markup`)
 | 
					## Markup (`markup`)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					- `MERMAID_MAX_SOURCE_CHARACTERS`: **5000**: Set the maximum size of a Mermaid source. (Set to -1 to disable)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Gitea can support Markup using external tools. The example below will add a markup named `asciidoc`.
 | 
					Gitea can support Markup using external tools. The example below will add a markup named `asciidoc`.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```ini
 | 
					```ini
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -17,6 +17,7 @@ import (
 | 
				
			|||||||
var (
 | 
					var (
 | 
				
			||||||
	ExternalMarkupRenderers    []*MarkupRenderer
 | 
						ExternalMarkupRenderers    []*MarkupRenderer
 | 
				
			||||||
	ExternalSanitizerRules     []MarkupSanitizerRule
 | 
						ExternalSanitizerRules     []MarkupSanitizerRule
 | 
				
			||||||
 | 
						MermaidMaxSourceCharacters int
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// MarkupRenderer defines the external parser configured in ini
 | 
					// MarkupRenderer defines the external parser configured in ini
 | 
				
			||||||
@@ -40,6 +41,7 @@ type MarkupSanitizerRule struct {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func newMarkup() {
 | 
					func newMarkup() {
 | 
				
			||||||
 | 
						MermaidMaxSourceCharacters = Cfg.Section("markup").Key("MERMAID_MAX_SOURCE_CHARACTERS").MustInt(5000)
 | 
				
			||||||
	ExternalMarkupRenderers = make([]*MarkupRenderer, 0, 10)
 | 
						ExternalMarkupRenderers = make([]*MarkupRenderer, 0, 10)
 | 
				
			||||||
	ExternalSanitizerRules = make([]MarkupSanitizerRule, 0, 10)
 | 
						ExternalSanitizerRules = make([]MarkupSanitizerRule, 0, 10)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -390,6 +390,9 @@ func NewFuncMap() []template.FuncMap {
 | 
				
			|||||||
			html += "</span>"
 | 
								html += "</span>"
 | 
				
			||||||
			return template.HTML(html)
 | 
								return template.HTML(html)
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
 | 
							"MermaidMaxSourceCharacters": func() int {
 | 
				
			||||||
 | 
								return setting.MermaidMaxSourceCharacters
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
	}}
 | 
						}}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -60,6 +60,7 @@
 | 
				
			|||||||
				{{ end }}
 | 
									{{ end }}
 | 
				
			||||||
			]).values()),
 | 
								]).values()),
 | 
				
			||||||
			{{end}}
 | 
								{{end}}
 | 
				
			||||||
 | 
								MermaidMaxSourceCharacters: {{MermaidMaxSourceCharacters}},
 | 
				
			||||||
		};
 | 
							};
 | 
				
			||||||
	</script>
 | 
						</script>
 | 
				
			||||||
	<link rel="icon" href="{{AssetUrlPrefix}}/img/logo.svg" type="image/svg+xml">
 | 
						<link rel="icon" href="{{AssetUrlPrefix}}/img/logo.svg" type="image/svg+xml">
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,4 +1,4 @@
 | 
				
			|||||||
const MAX_SOURCE_CHARACTERS = 5000;
 | 
					const {MermaidMaxSourceCharacters} = window.config;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function displayError(el, err) {
 | 
					function displayError(el, err) {
 | 
				
			||||||
  el.closest('pre').classList.remove('is-loading');
 | 
					  el.closest('pre').classList.remove('is-loading');
 | 
				
			||||||
@@ -26,8 +26,8 @@ export async function renderMermaid(els) {
 | 
				
			|||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  for (const el of els) {
 | 
					  for (const el of els) {
 | 
				
			||||||
    if (el.textContent.length > MAX_SOURCE_CHARACTERS) {
 | 
					    if (MermaidMaxSourceCharacters >= 0 && el.textContent.length > MermaidMaxSourceCharacters) {
 | 
				
			||||||
      displayError(el, new Error(`Mermaid source of ${el.textContent.length} characters exceeds the maximum allowed length of ${MAX_SOURCE_CHARACTERS}.`));
 | 
					      displayError(el, new Error(`Mermaid source of ${el.textContent.length} characters exceeds the maximum allowed length of ${MermaidMaxSourceCharacters}.`));
 | 
				
			||||||
      continue;
 | 
					      continue;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user