Skip to content

Commit

Permalink
feat(literalMidWordUnderscores): add support for GFM literal midword …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
tivie committed Jul 11, 2015
1 parent cff0237 commit 0c0cd7d
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 39 deletions.
37 changes: 22 additions & 15 deletions dist/showdown.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/showdown.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js.map

Large diffs are not rendered by default.

6 changes: 1 addition & 5 deletions src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ showdown.Converter = function (converterOptions) {
* @private
* @type {{}}
*/
options = {
omitExtraWLInCodeBlocks: false,
prefixHeaderId: false,
noHeaderId: false
},
options = {},

/**
* Language extensions used by this converter
Expand Down
13 changes: 7 additions & 6 deletions src/showdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ var showdown = {},
parsers = {},
extensions = {},
defaultOptions = {
omitExtraWLInCodeBlocks: false,
prefixHeaderId: false,
noHeaderId: false,
headerLevelStart: 1,
parseImgDimensions: false,
simplifiedAutoLink: false
omitExtraWLInCodeBlocks: false,
prefixHeaderId: false,
noHeaderId: false,
headerLevelStart: 1,
parseImgDimensions: false,
simplifiedAutoLink: false,
literalMidWordUnderscores: false
},
globalOptions = JSON.parse(JSON.stringify(defaultOptions)); //clone default options out of laziness =P

Expand Down
18 changes: 14 additions & 4 deletions src/subParsers/italicsAndBold.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
showdown.subParser('italicsAndBold', function (text) {
showdown.subParser('italicsAndBold', function (text, options) {
'use strict';
// <strong> must go first:
text = text.replace(/(\*\*|__)(?=\S)([^\r]*?\S[*_]*)\1/g, '<strong>$2</strong>');

text = text.replace(/(\*|_)(?=\S)([^\r]*?\S)\1/g, '<em>$2</em>');
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:
text = text.replace(/(\*\*|__)(?=\S)([^\r]*?\S[*_]*)\1/g, '<strong>$2</strong>');
text = text.replace(/(\*|_)(?=\S)([^\r]*?\S)\1/g, '<em>$2</em>');
}
return text;
});
11 changes: 11 additions & 0 deletions test/features/#164.2.disallow_underscore_emphasis_mid_word.html
Original file line number Diff line number Diff line change
@@ -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>
11 changes: 11 additions & 0 deletions test/features/#164.2.disallow_underscore_emphasis_mid_word.md
Original file line number Diff line number Diff line change
@@ -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
13 changes: 7 additions & 6 deletions test/node/showdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ describe('showdown.options', function () {
describe('getDefaultOptions()', function () {
it('should get default options', function () {
var opts = {
omitExtraWLInCodeBlocks: false,
prefixHeaderId: false,
noHeaderId: false,
headerLevelStart: 1,
parseImgDimensions: false,
simplifiedAutoLink: false
omitExtraWLInCodeBlocks: false,
prefixHeaderId: false,
noHeaderId: false,
headerLevelStart: 1,
parseImgDimensions: false,
simplifiedAutoLink: false,
literalMidWordUnderscores: false
};
expect(showdown.getDefaultOptions()).to.be.eql(opts);
});
Expand Down
2 changes: 2 additions & 0 deletions test/node/testsuite.features.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ describe('makeHtml() features testsuite', function () {
converter = new showdown.Converter({headerLevelStart: 3});
} else if (testsuite[i].name === '#164.1.simple_autolink') {
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 {
converter = new showdown.Converter();
}
Expand Down

0 comments on commit 0c0cd7d

Please sign in to comment.