|
| 1 | +package diagram |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +// Cases mirror mermaid's own frontmatter.spec.ts semantics. |
| 6 | +func TestStripFrontmatter(t *testing.T) { |
| 7 | + cases := []struct { |
| 8 | + name string |
| 9 | + in string |
| 10 | + wantRest string |
| 11 | + wantTitle string |
| 12 | + }{ |
| 13 | + {"no frontmatter", "graph TD\nA-->B", "graph TD\nA-->B", ""}, |
| 14 | + {"title extracted", "---\ntitle: foo\n---\ndiagram", "diagram", "foo"}, |
| 15 | + {"empty frontmatter", "---\n\n---\ndiagram", "diagram", ""}, |
| 16 | + {"unclosed is not frontmatter", "---\ntitle: foo\ndiagram", "---\ntitle: foo\ndiagram", ""}, |
| 17 | + {"only at document start", "diagram\n---\ntitle: foo\n---", "diagram\n---\ntitle: foo\n---", ""}, |
| 18 | + {"delimiter inside a value line", "---\ntitle: foo---bar\n---\ndiagram", "diagram", "foo---bar"}, |
| 19 | + {"unknown keys ignored", "---\nconfig:\n theme: dark\ninvalid: x\n---\ndiagram", "diagram", ""}, |
| 20 | + {"quoted title", "---\ntitle: \"Customers service\"\n---\ndiagram", "diagram", "Customers service"}, |
| 21 | + {"boolean-looking title stays text", "---\ntitle: true\n---\ndiagram", "diagram", "true"}, |
| 22 | + {"leading blank lines", "\n\n---\ntitle: t\n---\ndiagram", "diagram", "t"}, |
| 23 | + {"matching indented delimiters", " ---\n title: t\n ---\ndiagram", "diagram", "t"}, |
| 24 | + {"mismatched closing indent not a close", "---\ntitle: t\n ---\ndiagram", "---\ntitle: t\n ---\ndiagram", ""}, |
| 25 | + {"indented title is not the title", "---\nconfig:\n title: nested\n---\ndiagram", "diagram", ""}, |
| 26 | + {"crlf input", "---\r\ntitle: t\r\n---\r\ndiagram", "diagram", "t"}, |
| 27 | + {"multiline themeCSS config", "---\nconfig:\n themeCSS: |\n rect { fill: red; }\n---\nerDiagram", "erDiagram", ""}, |
| 28 | + {"inline comment stripped from title", "---\ntitle: hi # note\n---\ndiagram", "diagram", "hi"}, |
| 29 | + {"hash kept inside quoted title", "---\ntitle: \"a # b\"\n---\ndiagram", "diagram", "a # b"}, |
| 30 | + {"no space after colon is not a mapping", "---\ntitle:xyz\n---\ndiagram", "diagram", ""}, |
| 31 | + } |
| 32 | + for _, c := range cases { |
| 33 | + t.Run(c.name, func(t *testing.T) { |
| 34 | + rest, title := StripFrontmatter(c.in) |
| 35 | + if rest != c.wantRest { |
| 36 | + t.Errorf("rest = %q, want %q", rest, c.wantRest) |
| 37 | + } |
| 38 | + if title != c.wantTitle { |
| 39 | + t.Errorf("title = %q, want %q", title, c.wantTitle) |
| 40 | + } |
| 41 | + }) |
| 42 | + } |
| 43 | +} |
0 commit comments