mirror of
https://github.com/showdownjs/showdown.git
synced 2024-03-22 13:30:55 +08:00
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:
parent
2746949d7d
commit
c97f1dc6b1
BIN
dist/showdown.js
vendored
BIN
dist/showdown.js
vendored
Binary file not shown.
BIN
dist/showdown.js.map
vendored
BIN
dist/showdown.js.map
vendored
Binary file not shown.
BIN
dist/showdown.min.js
vendored
BIN
dist/showdown.min.js
vendored
Binary file not shown.
BIN
dist/showdown.min.js.map
vendored
BIN
dist/showdown.min.js.map
vendored
Binary file not shown.
|
@ -233,6 +233,7 @@ showdown.Converter = function (converterOptions) {
|
|||
|
||||
var globals = {
|
||||
gHtmlBlocks: [],
|
||||
gHtmlMdBlocks: [],
|
||||
gHtmlSpans: [],
|
||||
gUrls: {},
|
||||
gTitles: {},
|
||||
|
|
|
@ -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('');
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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>
|
||||
|
|
@ -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>
|
Loading…
Reference in New Issue
Block a user