Skip to content

Commit f47c486

Browse files
committed
ext-heading-anchor: Minor cleanups for consistency
1 parent 8766fa4 commit f47c486

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

commonmark-ext-heading-anchor/src/main/java/org/commonmark/ext/heading/anchor/HeadingAnchorExtension.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
/**
1111
* Extension for adding auto generated IDs to headings.
1212
* <p>
13-
* Create it with {@link #create()} and then configure it on the builder
14-
* {@link HtmlRenderer.Builder#extensions(Iterable)}).
13+
* Create it with {@link #create()} or {@link #builder()} and then configure it on the
14+
* renderer builder ({@link HtmlRenderer.Builder#extensions(Iterable)}).
1515
* <p>
1616
* The heading text will be used to create the id. Multiple headings with the
1717
* same text will result in appending a hyphen and number. For example:
@@ -33,20 +33,22 @@ public class HeadingAnchorExtension implements HtmlRenderer.HtmlRendererExtensio
3333
private final String idPrefix;
3434
private final String idSuffix;
3535

36-
private HeadingAnchorExtension(String defaultId, String idPrefix, String idSuffix) {
37-
this.defaultId = defaultId;
38-
this.idPrefix = idPrefix;
39-
this.idSuffix = idSuffix;
36+
private HeadingAnchorExtension(Builder builder) {
37+
this.defaultId = builder.defaultId;
38+
this.idPrefix = builder.idPrefix;
39+
this.idSuffix = builder.idSuffix;
4040
}
4141

42+
/**
43+
* @return the extension built with default settings
44+
*/
4245
public static Extension create() {
43-
return create(builder());
44-
}
45-
46-
private static Extension create(Builder builder) {
47-
return new HeadingAnchorExtension(builder.defaultId, builder.idPrefix, builder.idSuffix);
46+
return new HeadingAnchorExtension(builder());
4847
}
4948

49+
/**
50+
* @return a builder to configure the extension settings
51+
*/
5052
public static Builder builder() {
5153
return new Builder();
5254
}
@@ -62,15 +64,9 @@ public AttributeProvider create(AttributeProviderContext context) {
6264
}
6365

6466
public static class Builder {
65-
private String defaultId;
66-
private String idPrefix;
67-
private String idSuffix;
68-
69-
public Builder() {
70-
defaultId = "id";
71-
idPrefix = "";
72-
idSuffix = "";
73-
}
67+
private String defaultId = "id";
68+
private String idPrefix = "";
69+
private String idSuffix = "";
7470

7571
/**
7672
* @param value Default value for the id to take if no generated id can be extracted. Default "id"
@@ -92,15 +88,18 @@ public Builder idPrefix(String value) {
9288

9389
/**
9490
* @param value Set the value to be appended to every id generated. Default ""
95-
* @return
91+
* @return {@code this}
9692
*/
9793
public Builder idSuffix(String value) {
9894
this.idSuffix = value;
9995
return this;
10096
}
10197

98+
/**
99+
* @return a configured extension
100+
*/
102101
public Extension build() {
103-
return HeadingAnchorExtension.create(this);
102+
return new HeadingAnchorExtension(this);
104103
}
105104
}
106105
}

commonmark-ext-heading-anchor/src/test/java/org/commonmark/ext/heading/anchor/HeadingAnchorConfigurationTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package org.commonmark.ext.heading.anchor;
22

3-
import java.util.Arrays;
4-
import java.util.Collections;
5-
import java.util.Set;
6-
73
import org.commonmark.Extension;
84
import org.commonmark.parser.Parser;
95
import org.commonmark.renderer.html.HtmlRenderer;
106
import org.junit.Test;
117

8+
import java.util.Arrays;
9+
1210
import static org.hamcrest.CoreMatchers.equalTo;
1311
import static org.hamcrest.MatcherAssert.assertThat;
1412

0 commit comments

Comments
 (0)