Ver Fonte

feat(smoothLivePreview): fix weird effects due to parsing incomplete input

Estevão Soares dos Santos há 10 anos atrás
pai
commit
62ba3733cd
6 ficheiros alterados com 22 adições e 30 exclusões
  1. 11 15
      dist/showdown.js
  2. 0 0
      dist/showdown.js.map
  3. 0 0
      dist/showdown.min.js
  4. 0 0
      dist/showdown.min.js.map
  5. 5 0
      src/options.js
  6. 6 15
      src/subParsers/headers.js

+ 11 - 15
dist/showdown.js

@@ -67,6 +67,11 @@ function getDefaultOpts(simple) {
       default: false,
       describe: 'Turn on/off GFM tasklist support',
       type: 'boolean'
+    },
+    smoothLivePreview: {
+      default: false,
+      describe: 'Prevents weird effects in live previews due to incomplete input',
+      type: 'boolean'
     }
   };
   if (simple === false) {
@@ -1572,7 +1577,7 @@ showdown.subParser('headers', function (text, options, globals) {
   'use strict';
 
   var prefixHeader = options.prefixHeaderId,
-      headerLevelStart = (isNaN(parseInt(options.headerLevelStart))) ? 1 : parseInt(options.headerLevelStart);
+      headerLevelStart = (isNaN(parseInt(options.headerLevelStart))) ? 1 : parseInt(options.headerLevelStart),
 
   // Set text-style headers:
   //	Header 1
@@ -1581,7 +1586,10 @@ showdown.subParser('headers', function (text, options, globals) {
   //	Header 2
   //	--------
   //
-  text = text.replace(/^(.+)[ \t]*\n=+[ \t]*\n+/gm, function (wholeMatch, m1) {
+      setextRegexH1 = (options.smoothLivePreview) ? /^(.+)[ \t]*\n={2,}[ \t]*\n+/gm : /^(.+)[ \t]*\n=+[ \t]*\n+/gm,
+      setextRegexH2 = (options.smoothLivePreview) ? /^(.+)[ \t]*\n-{2,}[ \t]*\n+/gm : /^(.+)[ \t]*\n-+[ \t]*\n+/gm;
+
+  text = text.replace(setextRegexH1, function (wholeMatch, m1) {
 
     var spanGamut = showdown.subParser('spanGamut')(m1, options, globals),
         hID = (options.noHeaderId) ? '' : ' id="' + headerId(m1) + '"',
@@ -1590,7 +1598,7 @@ showdown.subParser('headers', function (text, options, globals) {
     return showdown.subParser('hashBlock')(hashBlock, options, globals);
   });
 
-  text = text.replace(/^(.+)[ \t]*\n-+[ \t]*\n+/gm, function (matchFound, m1) {
+  text = text.replace(setextRegexH2, function (matchFound, m1) {
     var spanGamut = showdown.subParser('spanGamut')(m1, options, globals),
         hID = (options.noHeaderId) ? '' : ' id="' + headerId(m1) + '"',
         hLevel = headerLevelStart + 1,
@@ -1605,18 +1613,6 @@ showdown.subParser('headers', function (text, options, globals) {
   //  ...
   //  ###### Header 6
   //
-
-  /*
-   text = text.replace(/
-   ^(\#{1,6})				// $1 = string of #'s
-   [ \t]*
-   (.+?)					// $2 = Header text
-   [ \t]*
-   \#*						// optional closing #'s (not counted)
-   \n+
-   /gm, function() {...});
-   */
-
   text = text.replace(/^(#{1,6})[ \t]*(.+?)[ \t]*#*\n+/gm, function (wholeMatch, m1, m2) {
     var span = showdown.subParser('spanGamut')(m2, options, globals),
         hID = (options.noHeaderId) ? '' : ' id="' + headerId(m2) + '"',

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


+ 5 - 0
src/options.js

@@ -65,6 +65,11 @@ function getDefaultOpts(simple) {
       default: false,
       describe: 'Turn on/off GFM tasklist support',
       type: 'boolean'
+    },
+    smoothLivePreview: {
+      default: false,
+      describe: 'Prevents weird effects in live previews due to incomplete input',
+      type: 'boolean'
     }
   };
   if (simple === false) {

+ 6 - 15
src/subParsers/headers.js

@@ -2,7 +2,7 @@ showdown.subParser('headers', function (text, options, globals) {
   'use strict';
 
   var prefixHeader = options.prefixHeaderId,
-      headerLevelStart = (isNaN(parseInt(options.headerLevelStart))) ? 1 : parseInt(options.headerLevelStart);
+      headerLevelStart = (isNaN(parseInt(options.headerLevelStart))) ? 1 : parseInt(options.headerLevelStart),
 
   // Set text-style headers:
   //	Header 1
@@ -11,7 +11,10 @@ showdown.subParser('headers', function (text, options, globals) {
   //	Header 2
   //	--------
   //
-  text = text.replace(/^(.+)[ \t]*\n=+[ \t]*\n+/gm, function (wholeMatch, m1) {
+      setextRegexH1 = (options.smoothLivePreview) ? /^(.+)[ \t]*\n={2,}[ \t]*\n+/gm : /^(.+)[ \t]*\n=+[ \t]*\n+/gm,
+      setextRegexH2 = (options.smoothLivePreview) ? /^(.+)[ \t]*\n-{2,}[ \t]*\n+/gm : /^(.+)[ \t]*\n-+[ \t]*\n+/gm;
+
+  text = text.replace(setextRegexH1, function (wholeMatch, m1) {
 
     var spanGamut = showdown.subParser('spanGamut')(m1, options, globals),
         hID = (options.noHeaderId) ? '' : ' id="' + headerId(m1) + '"',
@@ -20,7 +23,7 @@ showdown.subParser('headers', function (text, options, globals) {
     return showdown.subParser('hashBlock')(hashBlock, options, globals);
   });
 
-  text = text.replace(/^(.+)[ \t]*\n-+[ \t]*\n+/gm, function (matchFound, m1) {
+  text = text.replace(setextRegexH2, function (matchFound, m1) {
     var spanGamut = showdown.subParser('spanGamut')(m1, options, globals),
         hID = (options.noHeaderId) ? '' : ' id="' + headerId(m1) + '"',
         hLevel = headerLevelStart + 1,
@@ -35,18 +38,6 @@ showdown.subParser('headers', function (text, options, globals) {
   //  ...
   //  ###### Header 6
   //
-
-  /*
-   text = text.replace(/
-   ^(\#{1,6})				// $1 = string of #'s
-   [ \t]*
-   (.+?)					// $2 = Header text
-   [ \t]*
-   \#*						// optional closing #'s (not counted)
-   \n+
-   /gm, function() {...});
-   */
-
   text = text.replace(/^(#{1,6})[ \t]*(.+?)[ \t]*#*\n+/gm, function (wholeMatch, m1, m2) {
     var span = showdown.subParser('spanGamut')(m2, options, globals),
         hID = (options.noHeaderId) ? '' : ' id="' + headerId(m2) + '"',

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