package test import ( domainArticle "RenewCMS/internal/domain/article" "RenewCMS/internal/infrastructure/markdown" "testing" ) func TestJsonBlocksConverter(t *testing.T) { converter := markdown.NewConverter() paragraphA := `{"type":"paragraph","children":[{"type":"text","text":"a","marks":[]}]}` listOfA := `[{"type":"list","ordered":false,"items":[[` + paragraphA + `]]}]` examples := []struct { name, html, json string }{ {"h1", "

T

", `[{"type":"heading","level":1,"children":[{"type":"text","text":"T","marks":[]}]}]`}, {"h6", "
S
", `[{"type":"heading","level":6,"children":[{"type":"text","text":"S","marks":[]}]}]`}, {"paragraph", "

plain

", `[{"type":"paragraph","children":[{"type":"text","text":"plain","marks":[]}]}]`}, {"empty paragraph", "

", `[{"type":"paragraph","children":[]}]`}, {"bold", "

b

", `[{"type":"paragraph","children":[{"type":"text","text":"b","marks":[{"type":"bold"}]}]}]`}, {"italic", "

i

", `[{"type":"paragraph","children":[{"type":"text","text":"i","marks":[{"type":"italic"}]}]}]`}, {"link", `

l

`, `[{"type":"paragraph","children":[{"type":"text","text":"l","marks":[{"type":"link","href":"https://example.com/x"}]}]}]`}, {"nested marks outermost first", `

x

`, `[{"type":"paragraph","children":[{"type":"text","text":"x","marks":[{"type":"link","href":"/a"},{"type":"bold"},{"type":"italic"}]}]}]`}, {"line break", "

a
b

", `[{"type":"paragraph","children":[{"type":"text","text":"a\nb","marks":[]}]}]`}, {"bullet list", "", `[{"type":"list","ordered":false,"items":[[` + paragraphA + `],[{"type":"paragraph","children":[{"type":"text","text":"b","marks":[]}]}]]}]`}, {"ordered list", "
  1. a

", `[{"type":"list","ordered":true,"items":[[` + paragraphA + `]]}]`}, {"bare list item", "", listOfA}, {"nested list", "", `[{"type":"list","ordered":false,"items":[[` + paragraphA + `,{"type":"list","ordered":false,"items":[[{"type":"paragraph","children":[{"type":"text","text":"n","marks":[]}]}]]}]]}]`}, {"data-indent ignored", ``, listOfA}, {"blockquote", "

q

", `[{"type":"blockquote","children":[{"type":"paragraph","children":[{"type":"text","text":"q","marks":[]}]}]}]`}, {"code with language", `
x := 1
`, `[{"type":"code","language":"go","text":"x := 1"}]`}, {"code without language", "
raw
", `[{"type":"code","language":"","text":"raw"}]`}, {"rule", "
", `[{"type":"hr"}]`}, {"image with alt", `A`, `[{"type":"image","src":"/static/uploadedImages/a.png","alt":"A"}]`}, {"image without alt", ``, `[{"type":"image","src":"/static/uploadedImages/a.png","alt":""}]`}, {"image splits paragraph", `

xIy

`, `[{"type":"paragraph","children":[{"type":"text","text":"x","marks":[]}]},{"type":"image","src":"/i.png","alt":"I"},{"type":"paragraph","children":[{"type":"text","text":"y","marks":[]}]}]`}, {"span stripped", `

xy

`, `[{"type":"paragraph","children":[{"type":"text","text":"xy","marks":[]}]}]`}, {"inline code degrades", "

use go test

", `[{"type":"paragraph","children":[{"type":"text","text":"use go test","marks":[]}]}]`}, {"script and style removed", "

ok

", `[{"type":"paragraph","children":[{"type":"text","text":"ok","marks":[]}]}]`}, {"empty html", "", `[]`}, {"centered paragraph", `

a

`, `[{"type":"paragraph","align":"center","children":[{"type":"text","text":"a","marks":[]}]}]`}, {"right aligned heading", `

T

`, `[{"type":"heading","level":2,"align":"right","children":[{"type":"text","text":"T","marks":[]}]}]`}, {"justified paragraph", `

a

`, `[{"type":"paragraph","align":"justify","children":[{"type":"text","text":"a","marks":[]}]}]`}, {"left alignment omitted", `

a

`, `[` + paragraphA + `]`}, {"alignment skips the image inside a paragraph", `

xIy

`, `[{"type":"paragraph","align":"center","children":[{"type":"text","text":"x","marks":[]}]},{"type":"image","src":"/i.png","alt":"I"},{"type":"paragraph","align":"center","children":[{"type":"text","text":"y","marks":[]}]}]`}, } for _, example := range examples { t.Run(example.name, func(t *testing.T) { got, err := converter.Render(example.html, domainArticle.BodyFormatJSON) if err != nil { t.Fatalf("unexpected error: %v", err) } if got != example.json { t.Errorf("expected %s, got %s", example.json, got) } }) } }