Ver Fonte

feat(strikethrough): add support for GFM strikethrough

Github Flavored Markdown supports strikethrough (`<del>`) syntax using double tilde `~~` delimiters.
This commit adds this feature to showdown through an option called "strikethrough".

Related to #164
Estevão Soares dos Santos há 10 anos atrás
pai
commit
43e9448d6e

+ 13 - 1
dist/showdown.js

@@ -15,7 +15,8 @@ var showdown = {},
       headerLevelStart:          1,
       parseImgDimensions:        false,
       simplifiedAutoLink:        false,
-      literalMidWordUnderscores: false
+      literalMidWordUnderscores: false,
+      strikethrough:             false
     },
     globalOptions = JSON.parse(JSON.stringify(defaultOptions)); //clone default options out of laziness =P
 
@@ -1910,6 +1911,7 @@ showdown.subParser('spanGamut', function (text, options, globals) {
   text = showdown.subParser('autoLinks')(text, options, globals);
   text = showdown.subParser('encodeAmpsAndAngles')(text, options, globals);
   text = showdown.subParser('italicsAndBold')(text, options, globals);
+  text = showdown.subParser('strikethrough')(text, options, globals);
 
   // Do hard breaks:
   text = text.replace(/  +\n/g, ' <br />\n');
@@ -1918,6 +1920,16 @@ showdown.subParser('spanGamut', function (text, options, globals) {
 
 });
 
+showdown.subParser('strikethrough', function (text, options) {
+  'use strict';
+
+  if (options.strikethrough) {
+    text = text.replace(/(?:~T){2}([^~]+)(?:~T){2}/g, '<del>$1</del>');
+  }
+
+  return text;
+});
+
 /**
  * Strip any lines consisting only of spaces and tabs.
  * This makes subsequent regexs easier to write, because we can

Diff do ficheiro suprimidas por serem muito extensas
+ 0 - 0
dist/showdown.js.map


Diff do ficheiro suprimidas por serem muito extensas
+ 0 - 0
dist/showdown.min.js


Diff do ficheiro suprimidas por serem muito extensas
+ 0 - 0
dist/showdown.min.js.map


+ 2 - 1
src/showdown.js

@@ -13,7 +13,8 @@ var showdown = {},
       headerLevelStart:          1,
       parseImgDimensions:        false,
       simplifiedAutoLink:        false,
-      literalMidWordUnderscores: false
+      literalMidWordUnderscores: false,
+      strikethrough:             false
     },
     globalOptions = JSON.parse(JSON.stringify(defaultOptions)); //clone default options out of laziness =P
 

+ 1 - 0
src/subParsers/spanGamut.js

@@ -20,6 +20,7 @@ showdown.subParser('spanGamut', function (text, options, globals) {
   text = showdown.subParser('autoLinks')(text, options, globals);
   text = showdown.subParser('encodeAmpsAndAngles')(text, options, globals);
   text = showdown.subParser('italicsAndBold')(text, options, globals);
+  text = showdown.subParser('strikethrough')(text, options, globals);
 
   // Do hard breaks:
   text = text.replace(/  +\n/g, ' <br />\n');

+ 9 - 0
src/subParsers/strikethrough.js

@@ -0,0 +1,9 @@
+showdown.subParser('strikethrough', function (text, options) {
+  'use strict';
+
+  if (options.strikethrough) {
+    text = text.replace(/(?:~T){2}([^~]+)(?:~T){2}/g, '<del>$1</del>');
+  }
+
+  return text;
+});

+ 5 - 0
test/features/#164.3.strikethrough.html

@@ -0,0 +1,5 @@
+<p>a <del>strikethrough</del> word</p>
+
+<p>this should~~not be parsed</p>
+
+<p><del>strike-through text</del></p>

+ 5 - 0
test/features/#164.3.strikethrough.md

@@ -0,0 +1,5 @@
+a ~~strikethrough~~ word
+
+this should~~not be parsed
+
+~~strike-through text~~

+ 2 - 1
test/node/showdown.js

@@ -24,7 +24,8 @@ describe('showdown.options', function () {
         headerLevelStart:          1,
         parseImgDimensions:        false,
         simplifiedAutoLink:        false,
-        literalMidWordUnderscores: false
+        literalMidWordUnderscores: false,
+        strikethrough:             false
       };
       expect(showdown.getDefaultOptions()).to.be.eql(opts);
     });

+ 2 - 0
test/node/testsuite.features.js

@@ -18,6 +18,8 @@ describe('makeHtml() features testsuite', function () {
       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 if (testsuite[i].name === '#164.3.strikethrough') {
+      converter = new showdown.Converter({strikethrough: true});
     } else {
       converter = new showdown.Converter();
     }

Alguns ficheiros não foram mostrados porque muitos ficheiros mudaram neste diff