diff --git a/app/com/gitpitch/models/MarkdownModel.java b/app/com/gitpitch/models/MarkdownModel.java index e50f706..68376c7 100644 --- a/app/com/gitpitch/models/MarkdownModel.java +++ b/app/com/gitpitch/models/MarkdownModel.java @@ -109,8 +109,6 @@ 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); @@ -473,70 +471,6 @@ 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 { - - if(linkAbsolute(md)) { - return buildAnchor(md.trim(), md.trim()); - } else { - - 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 = buildAnchor(title, link); - - 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 buildAnchor(String title, String link) { - return new StringBuffer(HTML_ANCHOR_OPEN) - .append(link) - .append(HTML_ANCHOR_MID) - .append(title) - .append(HTML_ANCHOR_CLOSE) - .toString(); - } - private String postProcess(String md, PitchParams pp, YAMLOptions yOpts, diff --git a/app/com/gitpitch/services/CodeService.java b/app/com/gitpitch/services/CodeService.java index 9769355..66726e3 100644 --- a/app/com/gitpitch/services/CodeService.java +++ b/app/com/gitpitch/services/CodeService.java @@ -34,6 +34,8 @@ import com.gitpitch.utils.DelimParams; import com.gitpitch.utils.YAMLOptions; import java.util.*; import java.nio.file.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; import javax.inject.*; import play.Logger; import play.Logger.ALogger; @@ -84,6 +86,9 @@ public class CodeService { if(downStatus == 0) { String code = diskService.asText(pp, SOURCE_CODE); + if(isMarkdown(codePath)) { + code = prepMarkdown(code); + } theContent = buildCodeBlock(mdm.extractDelim(md), code, langHint, slideTitle); } else { @@ -140,9 +145,42 @@ public class CodeService { .toString(); } + private boolean isMarkdown(String codePath) { + return (codePath != null) && + codePath.endsWith(MARKDOWN_EXTENSION); + } + + /* + * To prevent MD code being treated as presentation content + * do the following on code-delimiter injection: + * + * 1. Auto-indent code. + * 2. Replace inline code block tripple-ticks with double-ticks + * to prevent nested code-block parser and rendering errors. + */ + private String prepMarkdown(String md) { + try { + md = Stream.of(lineByLine(md)).map(mdline -> { + return mdline.startsWith(MARKDOWN_CODE_TICKS) ? + mdline.substring(1) : mdline; + }).map(mdline -> { + return MARKDOWN_INDENT + mdline; + }).collect(Collectors.joining(MarkdownModel.MD_SPACER)); + } catch(Exception ex) {} + return md; + } + + private String[] lineByLine(String md) { + return md.split("\\r?\\n"); + } + private static final String SOURCE_CODE = "PITCHME.code"; private static final String SOURCE_CODE_DELIMITER = "### Code Block Delimiter"; private static final String SOURCE_CODE_NOT_FOUND = "### Source File Not Found"; + private static final String MARKDOWN_EXTENSION = ".md"; + private static final String MARKDOWN_INDENT = " "; + private static final String MARKDOWN_NEWLINE = "\n"; + private static final String MARKDOWN_CODE_TICKS = "```"; } diff --git a/app/com/gitpitch/services/ShortcutsService.java b/app/com/gitpitch/services/ShortcutsService.java index 16cb424..fe0e021 100644 --- a/app/com/gitpitch/services/ShortcutsService.java +++ b/app/com/gitpitch/services/ShortcutsService.java @@ -55,8 +55,7 @@ public class ShortcutsService { public boolean codeFragmentFound(String md) { boolean found = false; if(md != null) { - String trimmed = md.trim(); - if(trimmed.startsWith(MarkdownModel.MD_CODE_FRAG_OPEN)) { + if(md.startsWith(MarkdownModel.MD_CODE_FRAG_OPEN)) { found = true; } } @@ -66,8 +65,7 @@ public class ShortcutsService { public boolean titleHintFound(String md) { boolean found = false; if(md != null) { - String trimmed = md.trim(); - if(trimmed.startsWith(MarkdownModel.MD_TITLE_HINT_OPEN)) { + if(md.startsWith(MarkdownModel.MD_TITLE_HINT_OPEN)) { found = true; } } @@ -139,11 +137,9 @@ public class ShortcutsService { if(hintEnd > hintStart) { String hint = md.substring(hintStart, hintEnd); - md = new StringBuffer(MarkdownModel.MD_SPACER) - .append(TITLE_HINT_SPAN_OPEN) + md = new StringBuffer(TITLE_HINT_SPAN_OPEN) .append(hint) .append(TITLE_HINT_SPAN_CLOSE) - .append(MarkdownModel.MD_SPACER) .toString(); }