Merge pull request '[BUG] Don't panic on empty blockquote' (#4602) from gusted/forgejo-md-panic into forgejo
Some checks are pending
/ release (push) Waiting to run
testing / test-remote-cacher (map[image:redis:7.2 port:6379]) (push) Blocked by required conditions
testing / backend-checks (push) Waiting to run
testing / frontend-checks (push) Waiting to run
testing / test-unit (push) Blocked by required conditions
testing / test-remote-cacher (map[image:docker.io/valkey/valkey:7.2.5-alpine3.19 port:6379]) (push) Blocked by required conditions
testing / test-remote-cacher (map[image:ghcr.io/microsoft/garnet-alpine:1.0.14 port:6379]) (push) Blocked by required conditions
testing / test-remote-cacher (map[image:registry.redict.io/redict:7.3.0-scratch port:6379]) (push) Blocked by required conditions
testing / test-mysql (push) Blocked by required conditions
testing / test-pgsql (push) Blocked by required conditions
testing / test-sqlite (push) Blocked by required conditions
testing / security-check (push) Blocked by required conditions

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4602
Reviewed-by: Gergely Nagy <algernon@noreply.codeberg.org>
This commit is contained in:
Gusted 2024-07-21 00:09:56 +00:00
commit 15540445d9
3 changed files with 22 additions and 0 deletions

View file

@ -36,6 +36,10 @@ func (g *GitHubCalloutTransformer) Transform(node *ast.Document, reader text.Rea
switch v := n.(type) { switch v := n.(type) {
case *ast.Blockquote: case *ast.Blockquote:
if v.ChildCount() == 0 {
return ast.WalkContinue, nil
}
// We only want attention blockquotes when the AST looks like: // We only want attention blockquotes when the AST looks like:
// Text: "[" // Text: "["
// Text: "!TYPE" // Text: "!TYPE"

View file

@ -25,6 +25,10 @@ func (g *GitHubLegacyCalloutTransformer) Transform(node *ast.Document, reader te
switch v := n.(type) { switch v := n.(type) {
case *ast.Blockquote: case *ast.Blockquote:
if v.ChildCount() == 0 {
return ast.WalkContinue, nil
}
// The first paragraph contains the callout type. // The first paragraph contains the callout type.
firstParagraph := v.FirstChild() firstParagraph := v.FirstChild()
if firstParagraph.ChildCount() < 1 { if firstParagraph.ChildCount() < 1 {

View file

@ -1342,3 +1342,17 @@ key: value
</tbody> </tbody>
</table>`) </table>`)
} }
func TestCallout(t *testing.T) {
setting.AppURL = AppURL
test := func(input, expected string) {
buffer, err := markdown.RenderString(&markup.RenderContext{
Ctx: git.DefaultContext,
}, input)
assert.NoError(t, err)
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
}
test(">\n0", "<blockquote>\n</blockquote>\n<p>0</p>")
}