forked from vsch/flexmark-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PegdownCustomLinkResolverOptions.java
162 lines (138 loc) · 6.31 KB
/
PegdownCustomLinkResolverOptions.java
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.vladsch.flexmark.java.samples;
import com.vladsch.flexmark.ast.Link;
import com.vladsch.flexmark.ext.wikilink.WikiImage;
import com.vladsch.flexmark.ext.wikilink.WikiLink;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.html.LinkResolver;
import com.vladsch.flexmark.html.LinkResolverFactory;
import com.vladsch.flexmark.html.renderer.*;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.profile.pegdown.Extensions;
import com.vladsch.flexmark.profile.pegdown.PegdownOptionsAdapter;
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.data.DataHolder;
import com.vladsch.flexmark.util.data.MutableDataHolder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashSet;
import java.util.Set;
public class PegdownCustomLinkResolverOptions {
final private static DataHolder OPTIONS = PegdownOptionsAdapter.flexmarkOptions(Extensions.ALL, CustomExtension.create());
static final Parser PARSER = Parser.builder(OPTIONS).build();
static final HtmlRenderer RENDERER = HtmlRenderer.builder(OPTIONS).build();
static class CustomExtension implements HtmlRenderer.HtmlRendererExtension {
@Override
public void rendererOptions(@NotNull MutableDataHolder options) {
}
@Override
public void extend(@NotNull HtmlRenderer.Builder htmlRendererBuilder, @NotNull String rendererType) {
htmlRendererBuilder.linkResolverFactory(new CustomLinkResolver.Factory());
htmlRendererBuilder.nodeRendererFactory(new CustomLinkRenderer.Factory());
}
static CustomExtension create() {
return new CustomExtension();
}
}
static class CustomLinkResolver implements LinkResolver {
public CustomLinkResolver(LinkResolverBasicContext context) {
// can use context for custom settings
// context.getDocument();
// context.getHtmlOptions();
}
@NotNull
@Override
public ResolvedLink resolveLink(@NotNull Node node, @NotNull LinkResolverBasicContext context, @NotNull ResolvedLink link) {
// you can also set/clear/modify attributes through ResolvedLink.getAttributes() and ResolvedLink.getNonNullAttributes()
if (node instanceof WikiImage) {
// resolve wiki image link
String url = link.getUrl() + ".png";
// resolve url, return one of LinkStatus other than LinkStatus.UNKNOWN
return link.withStatus(LinkStatus.VALID)
.withUrl(url);
} else if (node instanceof WikiLink) {
// resolve wiki link
String url = link.getUrl() + ".html";
// resolve url, return one of LinkStatus other than LinkStatus.UNKNOWN
return link.withStatus(LinkStatus.VALID)
.withUrl(url);
}
return link;
}
static class Factory implements LinkResolverFactory {
@Nullable
@Override
public Set<Class<?>> getAfterDependents() {
return null;
}
@Nullable
@Override
public Set<Class<?>> getBeforeDependents() {
return null;
}
@Override
public boolean affectsGlobalScope() {
return false;
}
@NotNull
@Override
public LinkResolver apply(@NotNull LinkResolverBasicContext context) {
return new CustomLinkResolver(context);
}
}
}
static class CustomLinkRenderer implements NodeRenderer {
public static class Factory implements DelegatingNodeRendererFactory {
@NotNull
@Override
public NodeRenderer apply(@NotNull DataHolder options) {
return new CustomLinkRenderer();
}
@Override
public Set<Class<?>> getDelegates() {
///Set<Class<?>>();
// add node renderer factory classes to which this renderer will delegate some of its rendering
// core node renderer is assumed to have all depend it so there is no need to add it
//set.add(WikiLinkNodeRenderer.Factory.class);
//return set;
// return null if renderer does not delegate or delegates only to core node renderer
return null;
}
}
;
@Override
public Set<NodeRenderingHandler<?>> getNodeRenderingHandlers() {
HashSet<NodeRenderingHandler<?>> set = new HashSet<>();
set.add(new NodeRenderingHandler<>(Link.class, (node, context, html) -> {
// test the node to see if it needs overriding
if (node.getText().equals("bar")) {
html.text("(eliminated)");
} else {
// otherwise pass it for default rendering
context.delegateRender();
}
}));
//set.add(new NodeRenderingHandler<WikiLink>(WikiLink.class, new CustomNodeRenderer<WikiLink>() {
// @Override
// public void render(WikiLink node, NodeRendererContext context, HtmlWriter html) {
// // test the node to see if it needs overriding
// Matcher matcher = CONFLUENCE_WIKI_LINK.matcher(node.getChars());
// if (matcher.find()) {
// String link = "...";
// html.raw(link);
// } else {
// // otherwise pass it for default rendering
// context.delegateRender();
// }
// }
//}));
return set;
}
}
// use the PARSER to parse and RENDERER to render with pegdown compatibility
public static void main(String[] args) {
// You can re-use parser and renderer instances
Node document = PARSER.parse("This is *Sparta* [[document]] and this is not a link [bar](/url)");
String html = RENDERER.render(document); // "<p>This is <em>Sparta</em> <a href="document.html">document</a> and this is not a link (eliminated)</p>\n"
System.out.println(html);
}
}