From 5bec8f9e9ac2ed320a8aac1e8b5fe7e4c456956a Mon Sep 17 00:00:00 2001 From: Miguel Laginha Date: Wed, 5 Apr 2017 16:25:20 +0100 Subject: [PATCH] feat(literalMidWordAsterisks): add option for mid word asterisks Implements feature similar to ignoring midword underscores but with asterisks. The main use case is ignoring cursing. --- README.md | 12 +++++ src/options.js | 5 ++ src/subParsers/italicsAndBold.js | 33 +++++++++---- .../enable-literalMidWordAsterisks.html | 24 ++++++++++ .../enable-literalMidWordAsterisks.md | 46 +++++++++++++++++++ test/node/testsuite.features.js | 4 ++ 6 files changed, 114 insertions(+), 10 deletions(-) create mode 100644 test/features/enable-literalMidWordAsterisks.html create mode 100644 test/features/enable-literalMidWordAsterisks.md diff --git a/README.md b/README.md index 8125943c..b8d69504 100644 --- a/README.md +++ b/README.md @@ -253,6 +253,18 @@ var defaultOptions = showdown.getDefaultOptions(); ```html

some text with__underscores__in middle

``` + + * **literalMidWordAsterisks**: (boolean) [default false] Turning this on will stop showdown from interpreting asterisks in the middle of words as `` and `` and instead treat them as literal asterisks. + + Example: + + ```md + some text with**underscores**in middle + ``` + will be parsed as + ```html +

some text with**underscores**in middle

+ ``` * **strikethrough**: (boolean) [default false] Enable support for strikethrough syntax. `~~strikethrough~~` as `strikethrough` diff --git a/src/options.js b/src/options.js index abe254af..98aef491 100644 --- a/src/options.js +++ b/src/options.js @@ -51,6 +51,11 @@ function getDefaultOpts (simple) { describe: 'Parse midword underscores as literal underscores', type: 'boolean' }, + literalMidWordAsterisks: { + defaultValue: false, + describe: 'Parse midword asterisks as literal asterisks', + type: 'boolean' + }, strikethrough: { defaultValue: false, describe: 'Turn on/off strikethrough support', diff --git a/src/subParsers/italicsAndBold.js b/src/subParsers/italicsAndBold.js index 9fd80c1d..e22ca399 100644 --- a/src/subParsers/italicsAndBold.js +++ b/src/subParsers/italicsAndBold.js @@ -39,16 +39,29 @@ showdown.subParser('italicsAndBold', function (text, options, globals) { } // Now parse asterisks - text = text.replace(/\*\*\*(\S[\s\S]*?)\*\*\*/g, function (wm, m) { - return (/\S$/.test(m)) ? parseInside (m, '', '') : wm; - }); - text = text.replace(/\*\*(\S[\s\S]*?)\*\*/g, function (wm, m) { - return (/\S$/.test(m)) ? parseInside (m, '', '') : wm; - }); - text = text.replace(/\*([^\s*][\s\S]*?)\*/g, function (wm, m) { - // !/^\*[^*]/.test(m) - test if it doesn't start with ** (since it seems redundant, we removed it) - return (/\S$/.test(m)) ? parseInside (m, '', '') : wm; - }); + if (options.literalMidWordAsterisks) { + text = text.trim().replace(/(?:^| +)\*{3}(\S[\s\S]*?)\*{3}(?: +|$)/g, function (wm, txt) { + return parseInside (txt, ' ', ' '); + }); + text = text.trim().replace(/(?:^| +)\*{2}(\S[\s\S]*?)\*{2}(?: +|$)/g, function (wm, txt) { + return parseInside (txt, ' ', ' '); + }); + text = text.trim().replace(/(?:^| +)\*{1}(\S[\s\S]*?)\*{1}(?: +|$)/g, function (wm, txt) { + return parseInside (txt, ' ', '' + (wm.slice(-1) === ' ' ? ' ' : '')); + }); + } else { + text = text.replace(/\*\*\*(\S[\s\S]*?)\*\*\*/g, function (wm, m) { + return (/\S$/.test(m)) ? parseInside (m, '', '') : wm; + }); + text = text.replace(/\*\*(\S[\s\S]*?)\*\*/g, function (wm, m) { + return (/\S$/.test(m)) ? parseInside (m, '', '') : wm; + }); + text = text.replace(/\*([^\s*][\s\S]*?)\*/g, function (wm, m) { + // !/^\*[^*]/.test(m) - test if it doesn't start with ** (since it seems redundant, we removed it) + return (/\S$/.test(m)) ? parseInside (m, '', '') : wm; + }); + } + text = globals.converter._dispatch('italicsAndBold.after', text, options, globals); return text; diff --git a/test/features/enable-literalMidWordAsterisks.html b/test/features/enable-literalMidWordAsterisks.html new file mode 100644 index 00000000..0bc4ef92 --- /dev/null +++ b/test/features/enable-literalMidWordAsterisks.html @@ -0,0 +1,24 @@ +

this is a sentence*with*mid asterisks

+

this is a sentence**with**two mid asterisks

+

this is a sentence***with***three mid asterisks

+

this is a sentence with just*one asterisk

+

this is a sentence with just**one asterisk

+

this is a sentence with just***one asterisk

+

this is double**asterisk**mid word

+

this has just**one double asterisk

+

this has just***one triple asterisk

+

this should be parsed as emphasis

+

this should be parsed as bold

+

this should be parsed as bold and emphasis

+

emphasis at end of sentence

+

bold at end of sentence

+

bold and emphasis at end of sentence

+

emphasis at line start

+

bold at line start

+

bold and emphasis at line start

+

multi line emphasis +yeah it is yeah

+

multi line emphasis +yeah it is yeah

+

multi line emphasis +yeah it is yeah

diff --git a/test/features/enable-literalMidWordAsterisks.md b/test/features/enable-literalMidWordAsterisks.md new file mode 100644 index 00000000..b4d132de --- /dev/null +++ b/test/features/enable-literalMidWordAsterisks.md @@ -0,0 +1,46 @@ +this is a sentence*with*mid asterisks + +this is a sentence**with**two mid asterisks + +this is a sentence***with***three mid asterisks + +this is a sentence with just*one asterisk + +this is a sentence with just**one asterisk + +this is a sentence with just***one asterisk + +this is double**asterisk**mid word + +this has just**one double asterisk + +this has just***one triple asterisk + +this *should be parsed* as emphasis + +this **should be parsed** as bold + +this ***should be parsed*** as bold and emphasis + +emphasis at *end of sentence* + +bold at **end of sentence** + +bold and emphasis at ***end of sentence*** + +*emphasis at* line start + +**bold at** line start + +***bold and emphasis at*** line start + +multi *line emphasis +yeah it is* yeah + + +multi **line emphasis +yeah it is** yeah + + +multi ***line emphasis +yeah it is*** yeah diff --git a/test/node/testsuite.features.js b/test/node/testsuite.features.js index 09824474..4090f7e3 100644 --- a/test/node/testsuite.features.js +++ b/test/node/testsuite.features.js @@ -62,6 +62,8 @@ describe('makeHtml() features testsuite', function () { converter = new showdown.Converter({encodeEmails: false, simplifiedAutoLink: true}); } else if (testsuite[i].name === '#331.allow-escaping-of-tilde') { converter = new showdown.Converter({strikethrough: true}); + } else if (testsuite[i].name === 'enable-literalMidWordAsterisks') { + converter = new showdown.Converter({literalMidWordAsterisks: true}); } else if (testsuite[i].name === 'prefixHeaderId-simple') { converter = new showdown.Converter({prefixHeaderId: true}); } else if (testsuite[i].name === 'prefixHeaderId-string') { @@ -104,6 +106,8 @@ describe('makeHtml() features testsuite', function () { converter = new showdown.Converter({simplifiedAutoLink: true, strikethrough: true}); } else if (suite[i].name === 'disallow-underscores') { converter = new showdown.Converter({literalMidWordUnderscores: true, simplifiedAutoLink: true}); + } else if (suite[i].name === 'disallow-asterisks') { + converter = new showdown.Converter({literalMidWordAsterisks: true, simplifiedAutoLink: true}); } else { converter = new showdown.Converter({simplifiedAutoLink: true}); }