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