|
| 1 | +package org.commonmark.ext.front.matter; |
| 2 | + |
| 3 | +import org.commonmark.Extension; |
| 4 | +import org.commonmark.node.Document; |
| 5 | +import org.commonmark.node.Node; |
| 6 | +import org.commonmark.node.Paragraph; |
| 7 | +import org.commonmark.node.Text; |
| 8 | +import org.commonmark.parser.Parser; |
| 9 | +import org.commonmark.renderer.markdown.MarkdownRenderer; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +import static org.assertj.core.api.Assertions.assertThat; |
| 15 | + |
| 16 | +public class YamlFrontMatterMarkdownRendererTest { |
| 17 | + |
| 18 | + private static final List<Extension> EXTENSIONS = List.of(YamlFrontMatterExtension.create()); |
| 19 | + private static final Parser PARSER = Parser.builder().extensions(EXTENSIONS).build(); |
| 20 | + private static final MarkdownRenderer RENDERER = MarkdownRenderer.builder().extensions(EXTENSIONS).build(); |
| 21 | + |
| 22 | + // ===== Round-trip tests (parse string -> render -> compare to input) ===== |
| 23 | + |
| 24 | + @Test |
| 25 | + public void testRoundTripSimple() { |
| 26 | + assertRoundTrip("---\ntitle: My Document\n---\n\nMarkdown content\n"); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void testRoundTripEmptyValue() { |
| 31 | + assertRoundTrip("---\nkey:\n---\n\nMarkdown content\n"); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void testRoundTripMultipleKeys() { |
| 36 | + assertRoundTrip("---\ntitle: My Document\nauthor: John Doe\n---\n\nMarkdown content\n"); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testRoundTripListValues() { |
| 41 | + assertRoundTrip("---\ntags:\n - java\n - markdown\n---\n\nMarkdown content\n"); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testRoundTripLiteralBlock() { |
| 46 | + assertRoundTrip("---\ndescription: |\n first line\n second line\n---\n\nMarkdown content\n"); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testRoundTripSingleQuotedValue() { |
| 51 | + assertRoundTrip("---\nkey: 'value with ''single quotes'''\n---\n\nMarkdown content\n"); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testRoundTripDoubleQuotedValue() { |
| 56 | + /* |
| 57 | + * NOTE: We don't know what the original escape character was and the markdown renderer always uses single |
| 58 | + * quote, hence why this technically doesn't round-trip. |
| 59 | + */ |
| 60 | + var input = "---\nkey: \"value with \\\"double quotes\\\"\"\n---\n\nMarkdown content\n"; |
| 61 | + var rendered = RENDERER.render(PARSER.parse(input)); |
| 62 | + var expected = "---\nkey: 'value with \"double quotes\"'\n---\n\nMarkdown content\n"; |
| 63 | + assertThat(rendered).isEqualTo(expected); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testRoundTripFlowList() { |
| 68 | + // Flow-style list is stored as a single value - "[java, markdown]" - rendered back unquoted |
| 69 | + assertRoundTrip("---\ntags: [java, markdown]\n---\n\nMarkdown content\n"); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testRoundTripFlowMapping() { |
| 74 | + // Flow-style mapping is stored as a single value - "{key: value}" - rendered back unquoted |
| 75 | + assertRoundTrip("---\ndata: {key: value}\n---\n\nMarkdown content\n"); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testRoundTripEmptyFrontmatter() { |
| 80 | + assertRoundTrip("---\n---\n\nMarkdown content\n"); |
| 81 | + } |
| 82 | + |
| 83 | + // ===== Programmatic construction tests ===== |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testProgrammaticallyBuilt() { |
| 87 | + var doc = buildDocumentWithFrontMatter(List.of(new YamlFrontMatterNode("title", List.of("My Document")))); |
| 88 | + |
| 89 | + assertRenderedEquals(doc, "---\ntitle: My Document\n---\n\nMarkdown content\n"); |
| 90 | + } |
| 91 | + |
| 92 | + // ===== Quoting tests (values needing special treatment) ===== |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testValueWithColonSpace() { |
| 96 | + var doc = buildDocumentWithFrontMatter(List.of(new YamlFrontMatterNode("key", List.of("value with a: colon inside")))); |
| 97 | + |
| 98 | + assertRenderedEquals(doc, "---\nkey: 'value with a: colon inside'\n---\n\nMarkdown content\n"); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testValueWithColonNoSpace() { |
| 103 | + // Colon without trailing space is fine unquoted (e.g. timestamps, URLs) |
| 104 | + var doc = buildDocumentWithFrontMatter(List.of(new YamlFrontMatterNode("time", List.of("12:30:00")))); |
| 105 | + |
| 106 | + assertRenderedEquals(doc, "---\ntime: 12:30:00\n---\n\nMarkdown content\n"); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testValueStartingWithDash() { |
| 111 | + var doc = buildDocumentWithFrontMatter(List.of(new YamlFrontMatterNode("key", List.of("- not a list")))); |
| 112 | + |
| 113 | + assertRenderedEquals(doc, "---\nkey: '- not a list'\n---\n\nMarkdown content\n"); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testValueStartingWithUnmatchedBracket() { |
| 118 | + var doc = buildDocumentWithFrontMatter(List.of(new YamlFrontMatterNode("key", List.of("[broken")))); |
| 119 | + |
| 120 | + assertRenderedEquals(doc, "---\nkey: '[broken'\n---\n\nMarkdown content\n"); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testValueStartingWithMatchedBrackets() { |
| 125 | + // Valid flow list - should NOT be quoted |
| 126 | + var doc = buildDocumentWithFrontMatter(List.of(new YamlFrontMatterNode("flowList", List.of("[1, 2, 3]")))); |
| 127 | + |
| 128 | + assertRenderedEquals(doc, "---\nflowList: [1, 2, 3]\n---\n\nMarkdown content\n"); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testValueStartingWithUnmatchedBrace() { |
| 133 | + var doc = buildDocumentWithFrontMatter(List.of(new YamlFrontMatterNode("key", List.of("{broken")))); |
| 134 | + |
| 135 | + assertRenderedEquals(doc, "---\nkey: '{broken'\n---\n\nMarkdown content\n"); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void testValueStartingWithMatchedBraces() { |
| 140 | + // Valid flow mapping - should NOT be quoted |
| 141 | + var doc = buildDocumentWithFrontMatter(List.of(new YamlFrontMatterNode("flowMapping", List.of("{key: val}")))); |
| 142 | + |
| 143 | + assertRenderedEquals(doc, "---\nflowMapping: {key: val}\n---\n\nMarkdown content\n"); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void testValueContainingHashComment() { |
| 148 | + var doc = buildDocumentWithFrontMatter(List.of(new YamlFrontMatterNode("key", List.of("value # not a comment")))); |
| 149 | + |
| 150 | + assertRenderedEquals(doc, "---\nkey: 'value # not a comment'\n---\n\nMarkdown content\n"); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void testValueContainingApostrophe() { |
| 155 | + var doc = buildDocumentWithFrontMatter(List.of(new YamlFrontMatterNode("key", List.of("it's a test")))); |
| 156 | + |
| 157 | + assertRenderedEquals(doc, "---\nkey: 'it''s a test'\n---\n\nMarkdown content\n"); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + public void testEmptyStringValue() { |
| 162 | + var doc = buildDocumentWithFrontMatter(List.of(new YamlFrontMatterNode("empty", List.of("")))); |
| 163 | + |
| 164 | + assertRenderedEquals(doc, "---\nempty: ''\n---\n\nMarkdown content\n"); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + public void testValueStartingWithDoubleQuote() { |
| 169 | + var doc = buildDocumentWithFrontMatter(List.of(new YamlFrontMatterNode("key", List.of("\"quotes within value\"")))); |
| 170 | + |
| 171 | + assertRenderedEquals(doc, "---\nkey: '\"quotes within value\"'\n---\n\nMarkdown content\n"); |
| 172 | + } |
| 173 | + |
| 174 | + private void assertRoundTrip(String input) { |
| 175 | + String rendered = RENDERER.render(PARSER.parse(input)); |
| 176 | + assertThat(rendered).isEqualTo(input); |
| 177 | + } |
| 178 | + |
| 179 | + private void assertRenderedEquals(Node inputNode, String expectedOutput) { |
| 180 | + var renderedOutput = RENDERER.render(inputNode); |
| 181 | + assertThat(renderedOutput).isEqualTo(expectedOutput); |
| 182 | + } |
| 183 | + |
| 184 | + private Document buildDocumentWithFrontMatter(List<YamlFrontMatterNode> frontMatterNodes) { |
| 185 | + var doc = new Document(); |
| 186 | + |
| 187 | + var frontmatter = new YamlFrontMatterBlock(); |
| 188 | + for (var frontMatterNode : frontMatterNodes) { |
| 189 | + frontmatter.appendChild(frontMatterNode); |
| 190 | + } |
| 191 | + doc.appendChild(frontmatter); |
| 192 | + |
| 193 | + var para = new Paragraph(); |
| 194 | + para.appendChild(new Text("Markdown content")); |
| 195 | + doc.appendChild(para); |
| 196 | + |
| 197 | + return doc; |
| 198 | + } |
| 199 | +} |
0 commit comments