Skip to content

Commit e14130b

Browse files
committed
fix the table link
1 parent 2827d3c commit e14130b

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

libraries/markdownj/src/main/java/com/todou/markdownj/MarkdownProcessor.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,15 +420,14 @@ public String genericCodeBlock(String text) {
420420
}
421421

422422
private TextEditor doTableBlocks(TextEditor markup) {
423-
doAnchors(markup);
424423
Pattern p = Pattern.compile(
425424
"(\\|(?:[^\\n]*\\|)+\\n" +
426425
"\\|(?:[ ]*-+[ ]*\\|)+\\n" +
427426
"(?:\\|(?:[^\\n]*\\|)+[^\n]*\\n)+)", Pattern.DOTALL);
428427
return markup.replaceAll(p, new Replacement() {
429428
@Override
430429
public String replacement(Matcher m) {
431-
String tableMd = m.group(1);
430+
String tableMd = getAnchorsString(m.group(1));
432431
String[] lines = tableMd.split("\\n");
433432
StringBuilder sb = new StringBuilder();
434433
sb.append("<table class=\"table\"");
@@ -787,6 +786,47 @@ private void doLocalImage(TextEditor text) {
787786
text.update(sb.toString());
788787
}
789788

789+
private String getAnchorsString(String s) {
790+
Pattern internalLink = Pattern.compile("(" +
791+
"\\[(.*?)\\]" + // Link text = $2
792+
"[ ]?(?:\\n[ ]*)?" +
793+
"\\[(.*?)\\]" + // ID = $3
794+
")");
795+
796+
return replaceAll(s, internalLink, new Replacement() {
797+
@Override
798+
public String replacement(Matcher m) {
799+
String replacementText;
800+
String wholeMatch = m.group(1);
801+
String linkText = m.group(2);
802+
String id = m.group(3).toLowerCase();
803+
if ("".equals(id)) { // for shortcut links like [this][]
804+
id = linkText.toLowerCase();
805+
}
806+
807+
LinkDefinition defn = linkDefinitions.get(id);
808+
if (defn != null) {
809+
String url = defn.getUrl();
810+
// protect emphasis (* and _) within urls
811+
url = url.replaceAll("\\*", CHAR_PROTECTOR.encode("*"));
812+
url = url.replaceAll("_", CHAR_PROTECTOR.encode("_"));
813+
String title = defn.getTitle();
814+
String titleTag = "";
815+
if (title != null && !title.equals("")) {
816+
// protect emphasis (* and _) within urls
817+
title = title.replaceAll("\\*", CHAR_PROTECTOR.encode("*"));
818+
title = title.replaceAll("_", CHAR_PROTECTOR.encode("_"));
819+
titleTag = " title=\"" + title + "\"";
820+
}
821+
replacementText = "<a href=\"" + url + "\"" + titleTag + ">" + linkText + "</a>";
822+
} else {
823+
replacementText = wholeMatch;
824+
}
825+
return replacementText;
826+
}
827+
});
828+
}
829+
790830
private TextEditor doAnchors(TextEditor markup) {
791831
// Internal references: [link text] [id]
792832
Pattern internalLink = Pattern.compile("(" +

0 commit comments

Comments
 (0)