Code delimiter extended for markdown file injection.

Added support for cleanly handling Markdown file inject on code delimiters. This is a bit meta, injecting md-inside-md to be rendered as raw md. Special handling also added for rendering code-blocks within injected markdown file content.

Also, simplified @title(your-slide-title-goes-here) processing to fix rendering errors. Plus removed obsolete HTML anchor post-processing to fix rendering errors.
This commit is contained in:
David Russell 2017-11-09 12:53:07 +07:00
parent 13dad55e16
commit c8f3a3f785
3 changed files with 41 additions and 73 deletions

View File

@ -109,8 +109,6 @@ public class MarkdownModel implements Markdown {
consumed = stream.map(md -> { consumed = stream.map(md -> {
return process(md, pp, yOpts, gitRawBase); return process(md, pp, yOpts, gitRawBase);
}).map(md -> {
return processAnchors(md);
}).collect(Collectors.joining("\n")); }).collect(Collectors.joining("\n"));
consumed = postProcess(consumed, pp, yOpts, gitRawBase); consumed = postProcess(consumed, pp, yOpts, gitRawBase);
@ -473,70 +471,6 @@ public class MarkdownModel implements Markdown {
return null; 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, private String postProcess(String md,
PitchParams pp, PitchParams pp,
YAMLOptions yOpts, YAMLOptions yOpts,

View File

@ -34,6 +34,8 @@ import com.gitpitch.utils.DelimParams;
import com.gitpitch.utils.YAMLOptions; import com.gitpitch.utils.YAMLOptions;
import java.util.*; import java.util.*;
import java.nio.file.*; import java.nio.file.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.inject.*; import javax.inject.*;
import play.Logger; import play.Logger;
import play.Logger.ALogger; import play.Logger.ALogger;
@ -84,6 +86,9 @@ public class CodeService {
if(downStatus == 0) { if(downStatus == 0) {
String code = diskService.asText(pp, SOURCE_CODE); String code = diskService.asText(pp, SOURCE_CODE);
if(isMarkdown(codePath)) {
code = prepMarkdown(code);
}
theContent = buildCodeBlock(mdm.extractDelim(md), theContent = buildCodeBlock(mdm.extractDelim(md),
code, langHint, slideTitle); code, langHint, slideTitle);
} else { } else {
@ -140,9 +145,42 @@ public class CodeService {
.toString(); .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 = "PITCHME.code";
private static final String SOURCE_CODE_DELIMITER = private static final String SOURCE_CODE_DELIMITER =
"### Code Block Delimiter"; "### Code Block Delimiter";
private static final String SOURCE_CODE_NOT_FOUND = private static final String SOURCE_CODE_NOT_FOUND =
"### Source File 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 = "```";
} }

View File

@ -55,8 +55,7 @@ public class ShortcutsService {
public boolean codeFragmentFound(String md) { public boolean codeFragmentFound(String md) {
boolean found = false; boolean found = false;
if(md != null) { if(md != null) {
String trimmed = md.trim(); if(md.startsWith(MarkdownModel.MD_CODE_FRAG_OPEN)) {
if(trimmed.startsWith(MarkdownModel.MD_CODE_FRAG_OPEN)) {
found = true; found = true;
} }
} }
@ -66,8 +65,7 @@ public class ShortcutsService {
public boolean titleHintFound(String md) { public boolean titleHintFound(String md) {
boolean found = false; boolean found = false;
if(md != null) { if(md != null) {
String trimmed = md.trim(); if(md.startsWith(MarkdownModel.MD_TITLE_HINT_OPEN)) {
if(trimmed.startsWith(MarkdownModel.MD_TITLE_HINT_OPEN)) {
found = true; found = true;
} }
} }
@ -139,11 +137,9 @@ public class ShortcutsService {
if(hintEnd > hintStart) { if(hintEnd > hintStart) {
String hint = md.substring(hintStart, hintEnd); String hint = md.substring(hintStart, hintEnd);
md = new StringBuffer(MarkdownModel.MD_SPACER) md = new StringBuffer(TITLE_HINT_SPAN_OPEN)
.append(TITLE_HINT_SPAN_OPEN)
.append(hint) .append(hint)
.append(TITLE_HINT_SPAN_CLOSE) .append(TITLE_HINT_SPAN_CLOSE)
.append(MarkdownModel.MD_SPACER)
.toString(); .toString();
} }