|
1 | 1 | package org.commonmark.ext.autolink; |
2 | 2 |
|
| 3 | +import java.util.EnumSet; |
| 4 | +import java.util.Set; |
| 5 | + |
3 | 6 | import org.commonmark.Extension; |
4 | 7 | import org.commonmark.ext.autolink.internal.AutolinkPostProcessor; |
5 | 8 | import org.commonmark.parser.Parser; |
|
18 | 21 | */ |
19 | 22 | public class AutolinkExtension implements Parser.ParserExtension { |
20 | 23 |
|
21 | | - private AutolinkExtension() { |
| 24 | + private final Set<AutolinkType> linkTypes; |
| 25 | + |
| 26 | + private AutolinkExtension(Builder builder) { |
| 27 | + this.linkTypes = builder.linkTypes; |
22 | 28 | } |
23 | 29 |
|
| 30 | + /** |
| 31 | + * @return the extension with default options |
| 32 | + */ |
24 | 33 | public static Extension create() { |
25 | | - return new AutolinkExtension(); |
| 34 | + return builder().build(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @return a builder to configure the behavior of the extension. |
| 39 | + */ |
| 40 | + public static Builder builder() { |
| 41 | + return new Builder(); |
26 | 42 | } |
27 | 43 |
|
28 | 44 | @Override |
29 | 45 | public void extend(Parser.Builder parserBuilder) { |
30 | | - parserBuilder.postProcessor(new AutolinkPostProcessor()); |
| 46 | + parserBuilder.postProcessor(new AutolinkPostProcessor(linkTypes)); |
31 | 47 | } |
32 | 48 |
|
| 49 | + public static class Builder { |
| 50 | + |
| 51 | + private Set<AutolinkType> linkTypes = EnumSet.allOf(AutolinkType.class); |
| 52 | + |
| 53 | + /** |
| 54 | + * @param linkTypes the link types that should be converted. By default, |
| 55 | + * all {@link AutolinkType}s are converted. |
| 56 | + * @return {@code this} |
| 57 | + */ |
| 58 | + public Builder linkTypes(AutolinkType... linkTypes) { |
| 59 | + if (linkTypes == null) { |
| 60 | + throw new NullPointerException("linkTypes must not be null"); |
| 61 | + } |
| 62 | + |
| 63 | + return this.linkTypes(Set.of(linkTypes)); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @param linkTypes the link types that should be converted. By default, |
| 68 | + * all {@link AutolinkType}s are converted. |
| 69 | + * @return {@code this} |
| 70 | + */ |
| 71 | + public Builder linkTypes(Set<AutolinkType> linkTypes) { |
| 72 | + if (linkTypes == null) { |
| 73 | + throw new NullPointerException("linkTypes must not be null"); |
| 74 | + } |
| 75 | + |
| 76 | + if (linkTypes.isEmpty()) { |
| 77 | + throw new IllegalArgumentException("linkTypes must not be empty"); |
| 78 | + } |
| 79 | + |
| 80 | + this.linkTypes = EnumSet.copyOf(linkTypes); |
| 81 | + return this; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @return a configured extension |
| 86 | + */ |
| 87 | + public Extension build() { |
| 88 | + return new AutolinkExtension(this); |
| 89 | + } |
| 90 | + } |
33 | 91 | } |
0 commit comments