mirror of
https://github.com/showdownjs/showdown.git
synced 2024-03-22 13:30:55 +08:00
feat(allowBlockIndents): Indented inline block elements can be parsed as markdown
When the text is pulled from indented HTML elements, ex: ``` <body> <div> ## Content to be converted </div> </body> ``` it no longer becomes wrapped in code/pre tags. A new option is also available (allowBlockIndents) that when set to false, reverts to the previous behavior
This commit is contained in:
parent
f1211ac916
commit
f6326b84e4
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.
|
@ -6,7 +6,8 @@
|
|||
var showdown = {},
|
||||
parsers = {},
|
||||
globalOptions = {
|
||||
omitExtraWLInCodeBlocks: false
|
||||
omitExtraWLInCodeBlocks: false,
|
||||
allowBlockIndents: true
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
|
62
src/subParsers/allowBlockIndents.js
Normal file
62
src/subParsers/allowBlockIndents.js
Normal file
|
@ -0,0 +1,62 @@
|
|||
/**
|
||||
* Credits to Christopher (https://github.com/cwalker107)
|
||||
*/
|
||||
|
||||
/**
|
||||
* If text is being pulled from indented HTML elements, i.e.
|
||||
* <body>
|
||||
* <div>
|
||||
* ## Content to be converted
|
||||
* </div>
|
||||
* </body>
|
||||
*/
|
||||
showdown.subParser('allowBlockIndents', function (text, config, globals) {
|
||||
'use strict';
|
||||
|
||||
if (!config.allowBlockIndents) {
|
||||
return text;
|
||||
}
|
||||
|
||||
//Split the given array by it's new line characters
|
||||
var textSplitArr = text.split('\n');
|
||||
//We'll use this later to determine if there are leading whitespace characters
|
||||
var leadingWhiteChars = 0;
|
||||
var i;
|
||||
|
||||
for(i=0; i<=textSplitArr.length;i++) {
|
||||
if(textSplitArr[i] !== undefined) {
|
||||
|
||||
// Trim all trailing whitespaces from each line
|
||||
textSplitArr[i].replace(/[\s]*$/,'');
|
||||
|
||||
// roots out empty array values
|
||||
if(textSplitArr[i].length > 0) {
|
||||
|
||||
// Defines this single line's leading whitespace
|
||||
var lineLeadingWhiteChars = (textSplitArr[i].match(/^(\s)*/))[0].length;
|
||||
|
||||
// Determine how much the text is indented
|
||||
// by. This fixes nesting issues and also
|
||||
// doesn't break MarkDown syntax if code is on
|
||||
// the first lines
|
||||
if(leadingWhiteChars === 0 || (lineLeadingWhiteChars < leadingWhiteChars)) {
|
||||
if(textSplitArr[i].match(/[^\s]$/) !== null) {
|
||||
leadingWhiteChars = lineLeadingWhiteChars;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Only a regex that will replace how much it is indented by
|
||||
var reg = '^\\s{'+leadingWhiteChars+'}';
|
||||
for(i=0; i<=textSplitArr.length;i++) {
|
||||
if(textSplitArr[i] !== undefined) {
|
||||
// Replace leading indents
|
||||
textSplitArr[i] = textSplitArr[i].replace(new RegExp(reg),'');
|
||||
}
|
||||
}
|
||||
text = textSplitArr.join('\n\n'); //Join it all back together
|
||||
|
||||
return text;
|
||||
});
|
Loading…
Reference in New Issue
Block a user