mirror of
				https://github.com/go-gitea/gitea
				synced 2025-10-31 19:38:23 +00:00 
			
		
		
		
	Allow different HardBreaks settings for documents and comments (#11515)
GH has different HardBreaks behaviour for markdown comments and documents. Comments have hard breaks and documents have soft breaks - therefore Gitea's rendering will always be different from GH's if we only provide one setting. Here we split the setting in to two - one for documents and one for comments and other things. Signed-off-by: Andrew Thornton art27@cantab.net Changes to index.js as per @silverwind Co-authored-by: silverwind <me@silverwind.io> Changes to docs as per @guillep2k Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com>
This commit is contained in:
		| @@ -151,6 +151,16 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa | ||||
| 					v.AppendChild(v, newChild) | ||||
| 				} | ||||
| 			} | ||||
| 		case *ast.Text: | ||||
| 			if v.SoftLineBreak() && !v.HardLineBreak() { | ||||
| 				renderMetas := pc.Get(renderMetasKey).(map[string]string) | ||||
| 				mode := renderMetas["mode"] | ||||
| 				if mode != "document" { | ||||
| 					v.SetHardLineBreak(setting.Markdown.EnableHardLineBreakInComments) | ||||
| 				} else { | ||||
| 					v.SetHardLineBreak(setting.Markdown.EnableHardLineBreakInDocuments) | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 		return ast.WalkContinue, nil | ||||
| 	}) | ||||
|   | ||||
| @@ -29,17 +29,19 @@ var once = sync.Once{} | ||||
|  | ||||
| var urlPrefixKey = parser.NewContextKey() | ||||
| var isWikiKey = parser.NewContextKey() | ||||
| var renderMetasKey = parser.NewContextKey() | ||||
|  | ||||
| // NewGiteaParseContext creates a parser.Context with the gitea context set | ||||
| func NewGiteaParseContext(urlPrefix string, isWiki bool) parser.Context { | ||||
| func NewGiteaParseContext(urlPrefix string, metas map[string]string, isWiki bool) parser.Context { | ||||
| 	pc := parser.NewContext(parser.WithIDs(newPrefixedIDs())) | ||||
| 	pc.Set(urlPrefixKey, urlPrefix) | ||||
| 	pc.Set(isWikiKey, isWiki) | ||||
| 	pc.Set(renderMetasKey, metas) | ||||
| 	return pc | ||||
| } | ||||
|  | ||||
| // RenderRaw renders Markdown to HTML without handling special links. | ||||
| func RenderRaw(body []byte, urlPrefix string, wikiMarkdown bool) []byte { | ||||
| // render renders Markdown to HTML without handling special links. | ||||
| func render(body []byte, urlPrefix string, metas map[string]string, wikiMarkdown bool) []byte { | ||||
| 	once.Do(func() { | ||||
| 		converter = goldmark.New( | ||||
| 			goldmark.WithExtensions(extension.Table, | ||||
| @@ -75,12 +77,9 @@ func RenderRaw(body []byte, urlPrefix string, wikiMarkdown bool) []byte { | ||||
| 			), | ||||
| 		) | ||||
|  | ||||
| 		if setting.Markdown.EnableHardLineBreak { | ||||
| 			converter.Renderer().AddOptions(html.WithHardWraps()) | ||||
| 		} | ||||
| 	}) | ||||
|  | ||||
| 	pc := NewGiteaParseContext(urlPrefix, wikiMarkdown) | ||||
| 	pc := NewGiteaParseContext(urlPrefix, metas, wikiMarkdown) | ||||
| 	var buf bytes.Buffer | ||||
| 	if err := converter.Convert(giteautil.NormalizeEOL(body), &buf, parser.WithContext(pc)); err != nil { | ||||
| 		log.Error("Unable to render: %v", err) | ||||
| @@ -112,7 +111,7 @@ func (Parser) Extensions() []string { | ||||
|  | ||||
| // Render implements markup.Parser | ||||
| func (Parser) Render(rawBytes []byte, urlPrefix string, metas map[string]string, isWiki bool) []byte { | ||||
| 	return RenderRaw(rawBytes, urlPrefix, isWiki) | ||||
| 	return render(rawBytes, urlPrefix, metas, isWiki) | ||||
| } | ||||
|  | ||||
| // Render renders Markdown to HTML with all specific handling stuff. | ||||
| @@ -120,6 +119,11 @@ func Render(rawBytes []byte, urlPrefix string, metas map[string]string) []byte { | ||||
| 	return markup.Render("a.md", rawBytes, urlPrefix, metas) | ||||
| } | ||||
|  | ||||
| // RenderRaw renders Markdown to HTML without handling special links. | ||||
| func RenderRaw(body []byte, urlPrefix string, wikiMarkdown bool) []byte { | ||||
| 	return render(body, urlPrefix, map[string]string{}, wikiMarkdown) | ||||
| } | ||||
|  | ||||
| // RenderString renders Markdown to HTML with special links and returns string type. | ||||
| func RenderString(raw, urlPrefix string, metas map[string]string) string { | ||||
| 	return markup.RenderString("a.md", raw, urlPrefix, metas) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user