From 580b972d6d8fa4cdcc9ac7a4b03be6cc96573290 Mon Sep 17 00:00:00 2001 From: David Russell Date: Fri, 19 May 2017 17:42:34 +0700 Subject: [PATCH] Updated preprocessing for regular markdown links. --- app/com/gitpitch/models/MarkdownModel.java | 57 ++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/app/com/gitpitch/models/MarkdownModel.java b/app/com/gitpitch/models/MarkdownModel.java index bd0267a..0696abc 100644 --- a/app/com/gitpitch/models/MarkdownModel.java +++ b/app/com/gitpitch/models/MarkdownModel.java @@ -101,6 +101,8 @@ public class MarkdownModel implements Markdown { consumed = stream.map(md -> { return process(md, pp, yOpts, gitRawBase); + }).map(md -> { + return processAnchors(md); }).collect(Collectors.joining("\n")); consumed = postProcess(consumed, pp, yOpts, gitRawBase); @@ -460,6 +462,56 @@ public class MarkdownModel implements Markdown { return null; } + /* + * Ensure all regular Markdown links are upgraded to + * HTML anchors with target=_blank in order to + * open from within embedded GitPitch iFrame. + */ + private String processAnchors(String md) { + try { + boolean moreAnchors = md.contains(MD_ANCHOR_OPEN); + while(moreAnchors) { + + if(md.contains(MD_ANCHOR_OPEN)) { + + int anchorStart = md.indexOf(MD_ANCHOR_OPEN); + int anchorDelim = md.indexOf(MD_LINK_DELIM, anchorStart); + + // Regular Markdown Link not Markdown Image Link. + if(anchorDelim != -1 && + (anchorStart == 0 || !md.substring(anchorStart-1) + .startsWith(MD_LINK_OPEN))) { + + int anchorEnd = md.indexOf(MD_LINK_BRCKT, anchorDelim); + + String title = md.substring(anchorStart+1, anchorDelim); + String link = md.substring(anchorDelim+2, anchorEnd); + + String anchor = + new StringBuffer(HTML_ANCHOR_OPEN) + .append(link) + .append(HTML_ANCHOR_MID) + .append(title) + .append(HTML_ANCHOR_CLOSE) + .toString(); + + String mdLeft = md.substring(0, anchorStart); + String mdRight = md.substring(anchorEnd + 1); + md = mdLeft + anchor + mdRight; + + } else { + moreAnchors = false; + } + } else { + moreAnchors = false; + } + } + } catch(Exception tex) { + return md; + } + return md; + } + private String postProcess(String md, PitchParams pp, YAMLOptions yOpts, @@ -599,6 +651,7 @@ public class MarkdownModel implements Markdown { public static final String VSLIDE_DELIM_BACK_COMPAT = "#VSLIDE"; public static final String MD_LINK_OPEN = "!["; + public static final String MD_ANCHOR_OPEN = "["; public static final String MD_IMAGE_OPEN = "