Skip to content

Commit

Permalink
feat(markdown="1"): enable parsing markdown inside HTML blocks
Browse files Browse the repository at this point in the history
Enable parsing markdown inside HTML blocks if those blocks have an attribute called markdown="1".
This feature is EXPERIMENTAL! As such, the behavior might change on future releases.

Closes #178
  • Loading branch information
tivie committed Jan 2, 2016
1 parent 2746949 commit c97f1dc
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 27 deletions.
45 changes: 33 additions & 12 deletions dist/showdown.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/showdown.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/showdown.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ showdown.Converter = function (converterOptions) {

var globals = {
gHtmlBlocks: [],
gHtmlMdBlocks: [],
gHtmlSpans: [],
gUrls: {},
gTitles: {},
Expand Down
29 changes: 21 additions & 8 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,13 @@ var rgxFindMatchPos = function (str, left, right, flags) {
} else if (t) {
if (!--t) {
end = m.index + m[0].length;
pos.push({start: start, end: end});
var obj = {
left: {start: start, end: s},
match: {start: s, end: m.index},
right: {start: m.index, end: end},
wholeMatch: {start: start, end: end}
};
pos.push(obj);
if (!g) {
return pos;
}
Expand Down Expand Up @@ -200,7 +206,7 @@ showdown.helper.matchRecursiveRegExp = function (str, left, right, flags) {
if (!--t) {
end = m[0];
var match = str.slice(s, m.index);
a.push([start + match + end, match]);
a.push([start + match + end, match, start, end]);
if (!g) {
return a;
}
Expand Down Expand Up @@ -237,17 +243,24 @@ showdown.helper.replaceRecursiveRegExp = function (str, replacement, left, right

if (lng > 0) {
var bits = [];
if (matchPos[0].start !== 0) {
bits.push(str.slice(0, matchPos[0].start));
if (matchPos[0].wholeMatch.start !== 0) {
bits.push(str.slice(0, matchPos[0].wholeMatch.start));
}
for (var i = 0; i < lng; ++i) {
bits.push(replacement(str.slice(matchPos[i].start, matchPos[i].end)));
bits.push(
replacement(
str.slice(matchPos[i].wholeMatch.start, matchPos[i].wholeMatch.end),
str.slice(matchPos[i].match.start, matchPos[i].match.end),
str.slice(matchPos[i].left.start, matchPos[i].left.end),
str.slice(matchPos[i].right.start, matchPos[i].right.end)
)
);
if (i < lng - 1) {
bits.push(str.slice(matchPos[i].end, matchPos[i + 1].start));
bits.push(str.slice(matchPos[i].wholeMatch.end, matchPos[i + 1].wholeMatch.start));
}
}
if (matchPos[lng - 1].end < str.length) {
bits.push(str.slice(matchPos[lng - 1].end));
if (matchPos[lng - 1].wholeMatch.end < str.length) {
bits.push(str.slice(matchPos[lng - 1].wholeMatch.end));
}
finalStr = bits.join('');
}
Expand Down
10 changes: 8 additions & 2 deletions src/subParsers/hashHTMLBlocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ showdown.subParser('hashHTMLBlocks', function (text, options, globals) {
'video',
'p'
],
repFunc = function (match) {
return '\n\n~K' + (globals.gHtmlBlocks.push(match) - 1) + 'K\n\n';
repFunc = function (wholeMatch, match, left, right) {
var txt = wholeMatch;
// check if this html element is marked as markdown
// if so, it's contents should be parsed as markdown
if (left.search(/\bmarkdown\b/) !== -1) {
txt = left + globals.converter.makeHtml(match) + right;
}
return '\n\n~K' + (globals.gHtmlBlocks.push(txt) - 1) + 'K\n\n';
};

for (var i = 0; i < blockTags.length; ++i) {
Expand Down
3 changes: 2 additions & 1 deletion src/subParsers/paragraphs.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ showdown.subParser('paragraphs', function (text, options, globals) {
/** Unhashify HTML blocks */
end = grafsOut.length;
for (i = 0; i < end; i++) {
var blockText = '';
// if this is a marker for an html block...
while (grafsOut[i].search(/~K(\d+)K/) >= 0) {
var blockText = globals.gHtmlBlocks[RegExp.$1];
blockText = globals.gHtmlBlocks[RegExp.$1];
blockText = blockText.replace(/\$/g, '$$$$'); // Escape any dollar signs
grafsOut[i] = grafsOut[i].replace(/~K\d+K/, blockText);
}
Expand Down
9 changes: 9 additions & 0 deletions test/features/#178.markdown-inside-html-does-not-parse.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1 id="somemarkdown">some markdown</h1>

<p>blabla</p>
<div>This is **not parsed**</div>
<div markdown="1">
<p>This is <strong>parsed</strong></p>
</div>
<div>This is **not parsed**</div>

6 changes: 6 additions & 0 deletions test/features/#178.markdown-inside-html-does-not-parse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# some markdown

blabla
<div>This is **not parsed**</div>
<div markdown="1">This is **parsed**</div>
<div>This is **not parsed**</div>

0 comments on commit c97f1dc

Please sign in to comment.