|
| 1 | +package com.vladsch.flexmark.ext.attributes; |
| 2 | + |
| 3 | +import com.vladsch.flexmark.ast.Heading; |
| 4 | +import com.vladsch.flexmark.ast.Text; |
| 5 | +import com.vladsch.flexmark.html.HtmlRenderer; |
| 6 | +import com.vladsch.flexmark.parser.Parser; |
| 7 | +import com.vladsch.flexmark.util.ast.Node; |
| 8 | +import com.vladsch.flexmark.util.data.MutableDataSet; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.util.Arrays; |
| 13 | + |
| 14 | +import static org.junit.Assert.*; |
| 15 | + |
| 16 | +public class HtmlRendererHeadingIdsTest { |
| 17 | + |
| 18 | + @Test |
| 19 | + public void parserShouldReadHeadingIdsFromMarkdown() throws IOException { |
| 20 | + String title = "Heading with ID"; |
| 21 | + String id = "heading-id-1"; |
| 22 | + String markdownSource = "# " + title + " {#" + id + "}"; |
| 23 | + Node parsed = parse(markdownSource); |
| 24 | + |
| 25 | + assertTrue(parsed.hasChildren()); |
| 26 | + Node heading = parsed.getFirstChild(); |
| 27 | + assertEquals(Heading.class, heading.getClass()); |
| 28 | + assertEquals(id, ((Heading) heading).getAnchorRefId()); |
| 29 | + assertTrue(((Heading) heading).isExplicitAnchorRefId()); |
| 30 | + assertTrue(heading.hasChildren()); |
| 31 | + |
| 32 | + Node headingText = heading.getFirstChild(); |
| 33 | + assertEquals(Text.class, headingText.getClass()); |
| 34 | + assertEquals(title, headingText.getChars().toString()); |
| 35 | + |
| 36 | + Node headingAttributes = headingText.getNext(); |
| 37 | + assertEquals(AttributesNode.class, headingAttributes.getClass()); |
| 38 | + assertEquals("#" + id, ((AttributesNode) headingAttributes).getText().toString()); |
| 39 | + assertTrue(headingAttributes.hasChildren()); |
| 40 | + |
| 41 | + Node headingId = headingAttributes.getFirstChild(); |
| 42 | + assertEquals(AttributeNode.class, headingId.getClass()); |
| 43 | + assertEquals(id, ((AttributeNode) headingId).getValue().toString()); |
| 44 | + assertFalse(headingId.hasChildren()); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void headingWithoutIdShouldNotGetGeneratedIdInHtml() { |
| 49 | + String markdownSource = "# Heading without ID"; |
| 50 | + Node parsed = parse(markdownSource); |
| 51 | + String rendered = createRendererNotGeneratingButRenderingIds().render(parsed); |
| 52 | + assertEquals("<h1>Heading without ID</h1>\n", rendered); |
| 53 | + } |
| 54 | + @Test |
| 55 | + public void headingWithExplicitlySetIdShouldGetThatIdInHtml() { |
| 56 | + String markdownSource = "# Heading with ID {#heading-id-1}"; |
| 57 | + Node parsed = parse(markdownSource); |
| 58 | + String rendered = createRendererNotGeneratingButRenderingIds().render(parsed); |
| 59 | + assertEquals("<h1 id=\"heading-id-1\">Heading with ID</h1>\n", rendered); |
| 60 | + } |
| 61 | + |
| 62 | + private static HtmlRenderer createRendererNotGeneratingButRenderingIds() { |
| 63 | + return HtmlRenderer.builder(getSettings()).build(); |
| 64 | + } |
| 65 | + |
| 66 | + private static Node parse(String source) { |
| 67 | + return Parser.builder(getSettings()).build().parse(source); |
| 68 | + } |
| 69 | + |
| 70 | + private static MutableDataSet getSettings() { |
| 71 | + MutableDataSet options = new MutableDataSet(); |
| 72 | + options.set(Parser.BLANK_LINES_IN_AST, false); |
| 73 | + options.set(HtmlRenderer.RENDER_HEADER_ID, true); |
| 74 | + options.set(HtmlRenderer.GENERATE_HEADER_ID, false); |
| 75 | + options.set(Parser.EXTENSIONS, Arrays.asList( |
| 76 | + AttributesExtension.create())); |
| 77 | + return options; |
| 78 | + } |
| 79 | + |
| 80 | +} |
0 commit comments