-
Notifications
You must be signed in to change notification settings - Fork 271
/
HtmlToMarkdownCustomizedSample.java
127 lines (106 loc) · 4.78 KB
/
HtmlToMarkdownCustomizedSample.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
package com.vladsch.flexmark.java.samples;
import com.vladsch.flexmark.html.renderer.ResolvedLink;
import com.vladsch.flexmark.html2md.converter.*;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.data.DataHolder;
import com.vladsch.flexmark.util.data.MutableDataHolder;
import com.vladsch.flexmark.util.data.MutableDataSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public class HtmlToMarkdownCustomizedSample {
static class CustomLinkResolver implements HtmlLinkResolver {
public CustomLinkResolver(HtmlNodeConverterContext context) {
}
@Override
public ResolvedLink resolveLink(Node node, HtmlNodeConverterContext context, ResolvedLink link) {
// convert all links from http:// to https://
if (link.getUrl().startsWith("http:")) {
return link.withUrl("https:" + link.getUrl().substring("http:".length()));
}
return link;
}
static class Factory implements HtmlLinkResolverFactory {
@Nullable
@Override
public Set<Class<?>> getAfterDependents() {
return null;
}
@Nullable
@Override
public Set<Class<?>> getBeforeDependents() {
return null;
}
@Override
public boolean affectsGlobalScope() {
return false;
}
@Override
public HtmlLinkResolver apply(HtmlNodeConverterContext context) {
return new CustomLinkResolver(context);
}
}
}
static class HtmlConverterTextExtension implements FlexmarkHtmlConverter.HtmlConverterExtension {
public static HtmlConverterTextExtension create() {
return new HtmlConverterTextExtension();
}
@Override
public void rendererOptions(@NotNull MutableDataHolder options) {
}
@Override
public void extend(FlexmarkHtmlConverter.@NotNull Builder builder) {
builder.linkResolverFactory(new CustomLinkResolver.Factory());
builder.htmlNodeRendererFactory(new CustomHtmlNodeConverter.Factory());
}
}
static class CustomHtmlNodeConverter implements HtmlNodeRenderer {
public CustomHtmlNodeConverter(DataHolder options) {
}
@Override
public Set<HtmlNodeRendererHandler<?>> getHtmlNodeRendererHandlers() {
return new HashSet<>(Collections.singletonList(
new HtmlNodeRendererHandler<>("kbd", Element.class, this::processKbd)
));
}
private void processKbd(Element node, HtmlNodeConverterContext context, HtmlMarkdownWriter out) {
out.append("<<");
context.renderChildren(node, false, null);
out.append(">>");
}
static class Factory implements HtmlNodeRendererFactory {
@Override
public HtmlNodeRenderer apply(DataHolder options) {
return new CustomHtmlNodeConverter(options);
}
}
}
public static void main(String[] args) {
MutableDataSet options = new MutableDataSet()
.set(Parser.EXTENSIONS, Collections.singletonList(HtmlConverterTextExtension.create()));
String html = "<ul>\n" +
" <li>\n" +
" <p>Add: live templates starting with <code>.</code> <kbd>Kbd</kbd> <a href='http://example.com'>link</a></p>\n" +
" <table>\n" +
" <thead>\n" +
" <tr><th> Element </th><th> Abbreviation </th><th> Expansion </th></tr>\n" +
" </thead>\n" +
" <tbody>\n" +
" <tr><td> Abbreviation </td><td> <code>.abbreviation</code> </td><td> <code>*[]:</code> </td></tr>\n" +
" <tr><td> Code fence </td><td> <code>.codefence</code> </td><td> ``` ... ``` </td></tr>\n" +
" <tr><td> Explicit link </td><td> <code>.link</code> </td><td> <code>[]()</code> </td></tr>\n" +
" </tbody>\n" +
" </table>\n" +
" </li>\n" +
"</ul>";
String markdown = FlexmarkHtmlConverter.builder(options).build().convert(html);
System.out.println("HTML:");
System.out.println(html);
System.out.println("\nMarkdown:");
System.out.println(markdown);
}
}