Эх сурвалжийг харах

fix(simpleLineBreaks): fix simpleLineBreak option breaking lists html

When option was enabled, `<br />` tags where being added wrongfully
between `<li>` tags, which resulted in malformed html. This commit
prevents this behavior.

Closes #316
Estevao Soares dos Santos 8 жил өмнө
parent
commit
ed4c33fe4e

+ 10 - 6
dist/showdown.js

@@ -1732,7 +1732,7 @@ showdown.subParser('hashHTMLSpans', function (text, config, globals) {
   var matches = showdown.helper.matchRecursiveRegExp(text, '<code\\b[^>]*>', '</code>', 'gi');
 
   for (var i = 0; i < matches.length; ++i) {
-    text = text.replace(matches[i][0], '~L' + (globals.gHtmlSpans.push(matches[i][0]) - 1) + 'L');
+    text = text.replace(matches[i][0], '~C' + (globals.gHtmlSpans.push(matches[i][0]) - 1) + 'C');
   }
   return text;
 });
@@ -1744,7 +1744,7 @@ showdown.subParser('unhashHTMLSpans', function (text, config, globals) {
   'use strict';
 
   for (var i = 0; i < globals.gHtmlSpans.length; ++i) {
-    text = text.replace('~L' + i + 'L', globals.gHtmlSpans[i]);
+    text = text.replace('~C' + i + 'C', globals.gHtmlSpans[i]);
   }
 
   return text;
@@ -1952,6 +1952,7 @@ showdown.subParser('italicsAndBold', function (text, options, globals) {
 showdown.subParser('lists', function (text, options, globals) {
   'use strict';
   text = globals.converter._dispatch('lists.before', text, options, globals);
+
   /**
    * Process the contents of a single ordered or unordered list, splitting it
    * into individual list items.
@@ -2039,6 +2040,8 @@ showdown.subParser('lists', function (text, options, globals) {
         // Recursion for sub-lists:
         item = showdown.subParser('lists')(item, options, globals);
         item = item.replace(/\n$/, ''); // chomp(item)
+        item = showdown.subParser('hashHTMLBlocks')(item, options, globals);
+        item = item.replace(/\n\n+/g, '\n\n');
         if (isParagraphed) {
           item = showdown.subParser('paragraphs')(item, options, globals);
         } else {
@@ -2050,6 +2053,7 @@ showdown.subParser('lists', function (text, options, globals) {
       item = item.replace('~A', '');
       // we can finally wrap the line in list item tags
       item =  '<li' + bulletStyle + '>' + item + '</li>\n';
+
       return item;
     });
 
@@ -2260,12 +2264,12 @@ showdown.subParser('spanGamut', function (text, options, globals) {
   text = showdown.subParser('strikethrough')(text, options, globals);
 
   // Do hard breaks
-
-  // GFM style hard breaks
   if (options.simpleLineBreaks) {
-    text = text.replace(/\n/g, '<br />\n');
+    // GFM style hard breaks
+    text = text.replace(/\b\n\b/g, '<br />\n');
   } else {
-    text = text.replace(/  +\n/g, '<br />\n');
+    // Vanilla hard breaks
+    text = text.replace(/\b  +\n\b/g, '<br />\n');
   }
 
   text = globals.converter._dispatch('spanGamut.after', text, options, globals);

Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 0 - 0
dist/showdown.js.map


Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 0 - 0
dist/showdown.min.js


Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 0 - 0
dist/showdown.min.js.map


+ 2 - 2
src/subParsers/hashHTMLSpans.js

@@ -7,7 +7,7 @@ showdown.subParser('hashHTMLSpans', function (text, config, globals) {
   var matches = showdown.helper.matchRecursiveRegExp(text, '<code\\b[^>]*>', '</code>', 'gi');
 
   for (var i = 0; i < matches.length; ++i) {
-    text = text.replace(matches[i][0], '~L' + (globals.gHtmlSpans.push(matches[i][0]) - 1) + 'L');
+    text = text.replace(matches[i][0], '~C' + (globals.gHtmlSpans.push(matches[i][0]) - 1) + 'C');
   }
   return text;
 });
@@ -19,7 +19,7 @@ showdown.subParser('unhashHTMLSpans', function (text, config, globals) {
   'use strict';
 
   for (var i = 0; i < globals.gHtmlSpans.length; ++i) {
-    text = text.replace('~L' + i + 'L', globals.gHtmlSpans[i]);
+    text = text.replace('~C' + i + 'C', globals.gHtmlSpans[i]);
   }
 
   return text;

+ 4 - 0
src/subParsers/lists.js

@@ -4,6 +4,7 @@
 showdown.subParser('lists', function (text, options, globals) {
   'use strict';
   text = globals.converter._dispatch('lists.before', text, options, globals);
+
   /**
    * Process the contents of a single ordered or unordered list, splitting it
    * into individual list items.
@@ -91,6 +92,8 @@ showdown.subParser('lists', function (text, options, globals) {
         // Recursion for sub-lists:
         item = showdown.subParser('lists')(item, options, globals);
         item = item.replace(/\n$/, ''); // chomp(item)
+        item = showdown.subParser('hashHTMLBlocks')(item, options, globals);
+        item = item.replace(/\n\n+/g, '\n\n');
         if (isParagraphed) {
           item = showdown.subParser('paragraphs')(item, options, globals);
         } else {
@@ -102,6 +105,7 @@ showdown.subParser('lists', function (text, options, globals) {
       item = item.replace('~A', '');
       // we can finally wrap the line in list item tags
       item =  '<li' + bulletStyle + '>' + item + '</li>\n';
+
       return item;
     });
 

+ 4 - 4
src/subParsers/spanGamut.js

@@ -24,12 +24,12 @@ showdown.subParser('spanGamut', function (text, options, globals) {
   text = showdown.subParser('strikethrough')(text, options, globals);
 
   // Do hard breaks
-
-  // GFM style hard breaks
   if (options.simpleLineBreaks) {
-    text = text.replace(/\n/g, '<br />\n');
+    // GFM style hard breaks
+    text = text.replace(/\b\n\b/g, '<br />\n');
   } else {
-    text = text.replace(/  +\n/g, '<br />\n');
+    // Vanilla hard breaks
+    text = text.replace(/\b  +\n\b/g, '<br />\n');
   }
 
   text = globals.converter._dispatch('spanGamut.after', text, options, globals);

+ 48 - 0
test/features/#316.new-simpleLineBreaks-option-breaks-lists.html

@@ -0,0 +1,48 @@
+<ol>
+  <li>One</li>
+  <li>Two<ul>
+      <li>A</li>
+      <li>B</li></ul></li>
+  <li>Three</li>
+</ol>
+<blockquote>
+  <p>this has<br />
+    simple linebreaks</p>
+</blockquote>
+<pre><code>testing
+some
+code
+</code></pre>
+<ol>
+  <li><p>paragraphed list</p>
+    <p>this belongs<br />
+      to the first list item</p></li>
+  <li><p>This text<br />
+    also</p></li>
+</ol>
+<p>simple<br />
+  text</p>
+<ul>
+  <li>a list<br />
+    item</li>
+  <li>another<br />
+    list item</li>
+</ul>
+<p>simple<br />
+  text</p>
+<ul>
+  <li><p>some item</p>
+    <p>another<br />
+      paragraph</p>
+    <ul>
+      <li><p>And<br />
+          now</p>
+        <p>paragraph<br />
+          sublist</p>
+        <ul>
+          <li><p>and<br />
+              even</p>
+              <p>another<br />
+                one</p></li></ul></li></ul></li>
+  <li><p>foo</p></li>
+</ul>

+ 51 - 0
test/features/#316.new-simpleLineBreaks-option-breaks-lists.md

@@ -0,0 +1,51 @@
+1. One
+2. Two
+    - A
+    - B
+3. Three
+
+> this has
+> simple linebreaks
+
+    testing
+    some
+    code
+
+ 1. paragraphed list
+
+    this belongs
+    to the first list item
+    
+ 2. This text
+    also
+
+simple
+text
+
+ - a list
+   item
+ - another
+   list item
+
+simple
+text
+
+  - some item
+ 
+    another
+    paragraph
+   
+      - And
+        now
+     
+        paragraph
+        sublist
+     
+          - and
+            even
+       
+            another
+            one
+
+ - foo
+

+ 4 - 8
test/features/disableForced4SpacesIndentedSublists.html

@@ -1,13 +1,9 @@
 <ul>
-  <li>foo
-
-    <ul>
-      <li>bar</li></ul></li>
+  <li>foo<ul>
+    <li>bar</li></ul></li>
 </ul>
 <p>...</p>
 <ul>
-  <li>baz
-
-    <ol>
-      <li>bazinga</li></ol></li>
+  <li>baz<ol>
+    <li>bazinga</li></ol></li>
 </ul>

+ 6 - 12
test/issues/#196.entity-in-code-block-in-nested-list.html

@@ -1,17 +1,11 @@
 <p>Test pre in a list</p>
 <ul>
     <li>&amp; &lt;</li>
-    <li><code>&amp; &lt;</code>
-
-        <ul>
+    <li><code>&amp; &lt;</code><ul>
+        <li>&amp; &lt;</li>
+        <li><code>&amp; &lt;</code><ul>
             <li>&amp; &lt;</li>
-            <li><code>&amp; &lt;</code>
-
-                <ul>
-                    <li>&amp; &lt;</li>
-                    <li><code>&amp; &lt;</code>
-
-                        <ul>
-                            <li>&amp; &lt;</li>
-                            <li><code>&amp; &lt;</code></li></ul></li></ul></li></ul></li>
+            <li><code>&amp; &lt;</code><ul>
+                <li>&amp; &lt;</li>
+                <li><code>&amp; &lt;</code></li></ul></li></ul></li></ul></li>
 </ul>

+ 1 - 1
test/issues/#196.entity-in-code-block-in-nested-list.md

@@ -1,7 +1,7 @@
 Test pre in a list
 
 - & <
-- `& <` 
+- `& <`
     - & <
     - `& <`
         - & <

+ 4 - 8
test/issues/#299.nested-ordered-unordered-list-inconsistent-behavior-2.html

@@ -20,10 +20,8 @@
 </ol>
 <p>foo</p>
 <ul>
-    <li>one
-
-        <ol>
-            <li>two</li></ol></li>
+    <li>one<ol>
+        <li>two</li></ol></li>
 </ul>
 <p>foo</p>
 <ul>
@@ -47,8 +45,6 @@
 </ul>
 <p>foo</p>
 <ul>
-    <li>one
-
-        <ul>
-            <li>two</li></ul></li>
+    <li>one<ul>
+        <li>two</li></ul></li>
 </ul>

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

@@ -37,6 +37,8 @@ describe('makeHtml() features testsuite', function () {
       converter = new showdown.Converter({disableForced4SpacesIndentedSublists: true});
     } else if (testsuite[i].name === '#206.treat-single-line-breaks-as-br') {
       converter = new showdown.Converter({simpleLineBreaks: true});
+    } else if (testsuite[i].name === '#316.new-simpleLineBreaks-option-breaks-lists') {
+      converter = new showdown.Converter({simpleLineBreaks: true});
     } else if (testsuite[i].name === 'excludeTrailingPunctuationFromURLs-option') {
       converter = new showdown.Converter({simplifiedAutoLink: true, excludeTrailingPunctuationFromURLs: true});
     } else if (testsuite[i].name === 'requireSpaceBeforeHeadingText') {

Энэ ялгаанд хэт олон файл өөрчлөгдсөн тул зарим файлыг харуулаагүй болно