Skip to content

Commit c97f1dc

Browse files
committed
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
1 parent 2746949 commit c97f1dc

10 files changed

Lines changed: 84 additions & 27 deletions

dist/showdown.js

Lines changed: 33 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/converter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ showdown.Converter = function (converterOptions) {
233233

234234
var globals = {
235235
gHtmlBlocks: [],
236+
gHtmlMdBlocks: [],
236237
gHtmlSpans: [],
237238
gUrls: {},
238239
gTitles: {},

src/helpers.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,13 @@ var rgxFindMatchPos = function (str, left, right, flags) {
138138
} else if (t) {
139139
if (!--t) {
140140
end = m.index + m[0].length;
141-
pos.push({start: start, end: end});
141+
var obj = {
142+
left: {start: start, end: s},
143+
match: {start: s, end: m.index},
144+
right: {start: m.index, end: end},
145+
wholeMatch: {start: start, end: end}
146+
};
147+
pos.push(obj);
142148
if (!g) {
143149
return pos;
144150
}
@@ -200,7 +206,7 @@ showdown.helper.matchRecursiveRegExp = function (str, left, right, flags) {
200206
if (!--t) {
201207
end = m[0];
202208
var match = str.slice(s, m.index);
203-
a.push([start + match + end, match]);
209+
a.push([start + match + end, match, start, end]);
204210
if (!g) {
205211
return a;
206212
}
@@ -237,17 +243,24 @@ showdown.helper.replaceRecursiveRegExp = function (str, replacement, left, right
237243

238244
if (lng > 0) {
239245
var bits = [];
240-
if (matchPos[0].start !== 0) {
241-
bits.push(str.slice(0, matchPos[0].start));
246+
if (matchPos[0].wholeMatch.start !== 0) {
247+
bits.push(str.slice(0, matchPos[0].wholeMatch.start));
242248
}
243249
for (var i = 0; i < lng; ++i) {
244-
bits.push(replacement(str.slice(matchPos[i].start, matchPos[i].end)));
250+
bits.push(
251+
replacement(
252+
str.slice(matchPos[i].wholeMatch.start, matchPos[i].wholeMatch.end),
253+
str.slice(matchPos[i].match.start, matchPos[i].match.end),
254+
str.slice(matchPos[i].left.start, matchPos[i].left.end),
255+
str.slice(matchPos[i].right.start, matchPos[i].right.end)
256+
)
257+
);
245258
if (i < lng - 1) {
246-
bits.push(str.slice(matchPos[i].end, matchPos[i + 1].start));
259+
bits.push(str.slice(matchPos[i].wholeMatch.end, matchPos[i + 1].wholeMatch.start));
247260
}
248261
}
249-
if (matchPos[lng - 1].end < str.length) {
250-
bits.push(str.slice(matchPos[lng - 1].end));
262+
if (matchPos[lng - 1].wholeMatch.end < str.length) {
263+
bits.push(str.slice(matchPos[lng - 1].wholeMatch.end));
251264
}
252265
finalStr = bits.join('');
253266
}

src/subParsers/hashHTMLBlocks.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,14 @@ showdown.subParser('hashHTMLBlocks', function (text, options, globals) {
3737
'video',
3838
'p'
3939
],
40-
repFunc = function (match) {
41-
return '\n\n~K' + (globals.gHtmlBlocks.push(match) - 1) + 'K\n\n';
40+
repFunc = function (wholeMatch, match, left, right) {
41+
var txt = wholeMatch;
42+
// check if this html element is marked as markdown
43+
// if so, it's contents should be parsed as markdown
44+
if (left.search(/\bmarkdown\b/) !== -1) {
45+
txt = left + globals.converter.makeHtml(match) + right;
46+
}
47+
return '\n\n~K' + (globals.gHtmlBlocks.push(txt) - 1) + 'K\n\n';
4248
};
4349

4450
for (var i = 0; i < blockTags.length; ++i) {

src/subParsers/paragraphs.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ showdown.subParser('paragraphs', function (text, options, globals) {
3030
/** Unhashify HTML blocks */
3131
end = grafsOut.length;
3232
for (i = 0; i < end; i++) {
33+
var blockText = '';
3334
// if this is a marker for an html block...
3435
while (grafsOut[i].search(/~K(\d+)K/) >= 0) {
35-
var blockText = globals.gHtmlBlocks[RegExp.$1];
36+
blockText = globals.gHtmlBlocks[RegExp.$1];
3637
blockText = blockText.replace(/\$/g, '$$$$'); // Escape any dollar signs
3738
grafsOut[i] = grafsOut[i].replace(/~K\d+K/, blockText);
3839
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<h1 id="somemarkdown">some markdown</h1>
2+
3+
<p>blabla</p>
4+
<div>This is **not parsed**</div>
5+
<div markdown="1">
6+
<p>This is <strong>parsed</strong></p>
7+
</div>
8+
<div>This is **not parsed**</div>
9+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# some markdown
2+
3+
blabla
4+
<div>This is **not parsed**</div>
5+
<div markdown="1">This is **parsed**</div>
6+
<div>This is **not parsed**</div>

0 commit comments

Comments
 (0)