-
Notifications
You must be signed in to change notification settings - Fork 267
/
YouTubeLinkNodePostProcessor.java
59 lines (50 loc) · 2.22 KB
/
YouTubeLinkNodePostProcessor.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
package com.vladsch.flexmark.ext.youtube.embedded.internal;
import com.vladsch.flexmark.ast.Link;
import com.vladsch.flexmark.ast.Text;
import com.vladsch.flexmark.ext.youtube.embedded.YouTubeLink;
import com.vladsch.flexmark.parser.block.NodePostProcessor;
import com.vladsch.flexmark.parser.block.NodePostProcessorFactory;
import com.vladsch.flexmark.util.ast.Document;
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.ast.NodeTracker;
import com.vladsch.flexmark.util.data.DataHolder;
import com.vladsch.flexmark.util.misc.CharPredicate;
import com.vladsch.flexmark.util.sequence.BasedSequence;
import org.jetbrains.annotations.NotNull;
public class YouTubeLinkNodePostProcessor extends NodePostProcessor {
public YouTubeLinkNodePostProcessor(DataHolder options) {
}
@Override
public void process(@NotNull NodeTracker state, @NotNull Node node) {
if (node instanceof Link) {
Node previous = node.getPrevious();
if (previous instanceof Text) {
BasedSequence chars = previous.getChars();
if (chars.endsWith("@") && chars.isContinuedBy(node.getChars())) {
int prevBackslash = chars.subSequence(0, chars.length() - 1).countTrailing(CharPredicate.BACKSLASH);
if ((prevBackslash & 1) == 0) {
// trim previous chars to remove '@'
previous.setChars(chars.subSequence(0, chars.length() - 1));
YouTubeLink youTubeLink = new YouTubeLink((Link) node);
youTubeLink.takeChildren(node);
node.unlink();
previous.insertAfter(youTubeLink);
state.nodeRemoved(node);
state.nodeAddedWithChildren(youTubeLink);
}
}
}
}
}
public static class Factory extends NodePostProcessorFactory {
public Factory(DataHolder options) {
super(false);
addNodes(Link.class);
}
@NotNull
@Override
public NodePostProcessor apply(@NotNull Document document) {
return new YouTubeLinkNodePostProcessor(document);
}
}
}