feat(markdown="1"): enable parsing markdown inside HTML blocks

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
This commit is contained in:
Estevão Soares dos Santos 2016-01-02 01:08:17 +00:00
parent 2746949d7d
commit c97f1dc6b1
10 changed files with 47 additions and 11 deletions

BIN
dist/showdown.js vendored

Binary file not shown.

BIN
dist/showdown.js.map vendored

Binary file not shown.

BIN
dist/showdown.min.js vendored

Binary file not shown.

Binary file not shown.

View File

@ -233,6 +233,7 @@ showdown.Converter = function (converterOptions) {
var globals = {
gHtmlBlocks: [],
gHtmlMdBlocks: [],
gHtmlSpans: [],
gUrls: {},
gTitles: {},

View File

@ -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;
}
@ -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;
}
@ -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('');
}

View File

@ -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) {

View File

@ -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);
}

View File

@ -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>

View File

@ -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>