mirror of
https://github.com/showdownjs/showdown.git
synced 2024-03-22 13:30:55 +08:00
feat(literalMidWordUnderscores): add support for GFM literal midword underscores
Github Flavored Markdown does not parse underscores in the middle of a word as emphasis/bold. This commit adds this feature to showdown through an option called "literalMidWordUnderscores". Related to #164
This commit is contained in:
parent
cff0237299
commit
0c0cd7db99
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.
|
@ -22,11 +22,7 @@ showdown.Converter = function (converterOptions) {
|
||||||
* @private
|
* @private
|
||||||
* @type {{}}
|
* @type {{}}
|
||||||
*/
|
*/
|
||||||
options = {
|
options = {},
|
||||||
omitExtraWLInCodeBlocks: false,
|
|
||||||
prefixHeaderId: false,
|
|
||||||
noHeaderId: false
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Language extensions used by this converter
|
* Language extensions used by this converter
|
||||||
|
|
|
@ -12,7 +12,8 @@ var showdown = {},
|
||||||
noHeaderId: false,
|
noHeaderId: false,
|
||||||
headerLevelStart: 1,
|
headerLevelStart: 1,
|
||||||
parseImgDimensions: false,
|
parseImgDimensions: false,
|
||||||
simplifiedAutoLink: false
|
simplifiedAutoLink: false,
|
||||||
|
literalMidWordUnderscores: false
|
||||||
},
|
},
|
||||||
globalOptions = JSON.parse(JSON.stringify(defaultOptions)); //clone default options out of laziness =P
|
globalOptions = JSON.parse(JSON.stringify(defaultOptions)); //clone default options out of laziness =P
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,19 @@
|
||||||
showdown.subParser('italicsAndBold', function (text) {
|
showdown.subParser('italicsAndBold', function (text, options) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
if (options.literalMidWordUnderscores) {
|
||||||
|
//underscores
|
||||||
|
// Since we are consuming a \s character, we need to add it
|
||||||
|
text = text.replace(/(\s)__(?=\S)([^]+?)__(?=\s)/g, '$1<strong>$2</strong>');
|
||||||
|
text = text.replace(/(\s)_(?=\S)([^]+?)_(?=\s)/g, '$1<em>$2</em>');
|
||||||
|
//asterisks
|
||||||
|
text = text.replace(/\*\*(?=\S)([^]+?)\*\*/g, '<strong>$1</strong>');
|
||||||
|
text = text.replace(/\*(?=\S)([^]+?)\*/g, '<em>$1</em>');
|
||||||
|
|
||||||
|
} else {
|
||||||
// <strong> must go first:
|
// <strong> must go first:
|
||||||
text = text.replace(/(\*\*|__)(?=\S)([^\r]*?\S[*_]*)\1/g, '<strong>$2</strong>');
|
text = text.replace(/(\*\*|__)(?=\S)([^\r]*?\S[*_]*)\1/g, '<strong>$2</strong>');
|
||||||
|
|
||||||
text = text.replace(/(\*|_)(?=\S)([^\r]*?\S)\1/g, '<em>$2</em>');
|
text = text.replace(/(\*|_)(?=\S)([^\r]*?\S)\1/g, '<em>$2</em>');
|
||||||
|
}
|
||||||
return text;
|
return text;
|
||||||
});
|
});
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
<p>this is a sentence_with_mid underscores</p>
|
||||||
|
|
||||||
|
<p>this is a sentence with just_one underscore</p>
|
||||||
|
|
||||||
|
<p>this <em>should be parsed</em> as emphasis</p>
|
||||||
|
|
||||||
|
<p>this is double__underscore__mid word</p>
|
||||||
|
|
||||||
|
<p>this has just__one double underscore</p>
|
||||||
|
|
||||||
|
<p>this <strong>should be parsed</strong> as bold</p>
|
|
@ -0,0 +1,11 @@
|
||||||
|
this is a sentence_with_mid underscores
|
||||||
|
|
||||||
|
this is a sentence with just_one underscore
|
||||||
|
|
||||||
|
this _should be parsed_ as emphasis
|
||||||
|
|
||||||
|
this is double__underscore__mid word
|
||||||
|
|
||||||
|
this has just__one double underscore
|
||||||
|
|
||||||
|
this __should be parsed__ as bold
|
|
@ -23,7 +23,8 @@ describe('showdown.options', function () {
|
||||||
noHeaderId: false,
|
noHeaderId: false,
|
||||||
headerLevelStart: 1,
|
headerLevelStart: 1,
|
||||||
parseImgDimensions: false,
|
parseImgDimensions: false,
|
||||||
simplifiedAutoLink: false
|
simplifiedAutoLink: false,
|
||||||
|
literalMidWordUnderscores: false
|
||||||
};
|
};
|
||||||
expect(showdown.getDefaultOptions()).to.be.eql(opts);
|
expect(showdown.getDefaultOptions()).to.be.eql(opts);
|
||||||
});
|
});
|
||||||
|
|
|
@ -16,6 +16,8 @@ describe('makeHtml() features testsuite', function () {
|
||||||
converter = new showdown.Converter({headerLevelStart: 3});
|
converter = new showdown.Converter({headerLevelStart: 3});
|
||||||
} else if (testsuite[i].name === '#164.1.simple_autolink') {
|
} else if (testsuite[i].name === '#164.1.simple_autolink') {
|
||||||
converter = new showdown.Converter({simplifiedAutoLink: true});
|
converter = new showdown.Converter({simplifiedAutoLink: true});
|
||||||
|
} else if (testsuite[i].name === '#164.2.disallow_underscore_emphasis_mid_word') {
|
||||||
|
converter = new showdown.Converter({literalMidWordUnderscores: true});
|
||||||
} else {
|
} else {
|
||||||
converter = new showdown.Converter();
|
converter = new showdown.Converter();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user