Pārlūkot izejas kodu

fix(tables): tables are properly rendered when followed by a single linebreak and a list

Closes #443
Estevao Soares dos Santos 7 gadi atpakaļ
vecāks
revīzija
d88b095f05

+ 16 - 3
dist/showdown.js

@@ -1,4 +1,4 @@
-;/*! showdown v 1.7.5 - 02-10-2017 */
+;/*! showdown v 1.7.5 - 06-10-2017 */
 (function(){
 /**
  * Created by Tivie on 13-07-2015.
@@ -2843,9 +2843,9 @@ showdown.subParser('tables', function (text, options, globals) {
     return text;
   }
 
-  var tableRgx       = /^ {0,3}\|?.+\|.+\n {0,3}\|?[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*:?[ \t]*(?:[-=]){2,}[\s\S]+?(?:\n\n|¨0)/gm,
+  var tableRgx       = /^ {0,3}\|?.+\|.+\n {0,3}\|?[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*:?[ \t]*(?:[-=]){2,}[\s\S]+?(?:\n\n|<ol|<ul|¨0)/gm,
     //singeColTblRgx = /^ {0,3}\|.+\|\n {0,3}\|[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*\n(?: {0,3}\|.+\|\n)+(?:\n\n|¨0)/gm;
-      singeColTblRgx = /^ {0,3}\|.+\|\n {0,3}\|[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|\n( {0,3}\|.+\|\n)*(?:\n|¨0)/gm;
+      singeColTblRgx = /^ {0,3}\|.+\|\n {0,3}\|[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|\n( {0,3}\|.+\|\n)*(?:\n|<ol|<ul|¨0)/gm;
 
   function parseStyles (sLine) {
     if (/^:[ \t]*--*$/.test(sLine)) {
@@ -2961,11 +2961,24 @@ showdown.subParser('tables', function (text, options, globals) {
     return buildTable(headers, cells);
   }
 
+  function hackFixTableFollowedByList (rawTable) {
+    var lastChars = rawTable.slice(-3);
+    if (lastChars === '<ol' || lastChars === '<ul') {
+      rawTable = rawTable.slice(0, -3) + '\n\n' + rawTable.slice(-3);
+    }
+    return rawTable;
+  }
+
   text = globals.converter._dispatch('tables.before', text, options, globals);
 
   // find escaped pipe characters
   text = text.replace(/\\(\|)/g, showdown.helper.escapeCharactersCallback);
 
+  // hackfix issue #443. Due to lists only having a linebreak before them, we need to manually insert a linebreak to prevent
+  // tables not being parsed when followed by a list
+  text = text.replace(tableRgx, hackFixTableFollowedByList);
+  text = text.replace(singeColTblRgx, hackFixTableFollowedByList);
+
   // parse multi column tables
   text = text.replace(tableRgx, parseTable);
 

Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 0 - 0
dist/showdown.js.map


Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 0 - 1
dist/showdown.min.js


Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 0 - 0
dist/showdown.min.js.map


+ 15 - 2
src/subParsers/tables.js

@@ -5,9 +5,9 @@ showdown.subParser('tables', function (text, options, globals) {
     return text;
   }
 
-  var tableRgx       = /^ {0,3}\|?.+\|.+\n {0,3}\|?[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*:?[ \t]*(?:[-=]){2,}[\s\S]+?(?:\n\n|¨0)/gm,
+  var tableRgx       = /^ {0,3}\|?.+\|.+\n {0,3}\|?[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*:?[ \t]*(?:[-=]){2,}[\s\S]+?(?:\n\n|<ol|<ul|¨0)/gm,
     //singeColTblRgx = /^ {0,3}\|.+\|\n {0,3}\|[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|[ \t]*\n(?: {0,3}\|.+\|\n)+(?:\n\n|¨0)/gm;
-      singeColTblRgx = /^ {0,3}\|.+\|\n {0,3}\|[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|\n( {0,3}\|.+\|\n)*(?:\n|¨0)/gm;
+      singeColTblRgx = /^ {0,3}\|.+\|\n {0,3}\|[ \t]*:?[ \t]*(?:[-=]){2,}[ \t]*:?[ \t]*\|\n( {0,3}\|.+\|\n)*(?:\n|<ol|<ul|¨0)/gm;
 
   function parseStyles (sLine) {
     if (/^:[ \t]*--*$/.test(sLine)) {
@@ -123,11 +123,24 @@ showdown.subParser('tables', function (text, options, globals) {
     return buildTable(headers, cells);
   }
 
+  function hackFixTableFollowedByList (rawTable) {
+    var lastChars = rawTable.slice(-3);
+    if (lastChars === '<ol' || lastChars === '<ul') {
+      rawTable = rawTable.slice(0, -3) + '\n\n' + rawTable.slice(-3);
+    }
+    return rawTable;
+  }
+
   text = globals.converter._dispatch('tables.before', text, options, globals);
 
   // find escaped pipe characters
   text = text.replace(/\\(\|)/g, showdown.helper.escapeCharactersCallback);
 
+  // hackfix issue #443. Due to lists only having a linebreak before them, we need to manually insert a linebreak to prevent
+  // tables not being parsed when followed by a list
+  text = text.replace(tableRgx, hackFixTableFollowedByList);
+  text = text.replace(singeColTblRgx, hackFixTableFollowedByList);
+
   // parse multi column tables
   text = text.replace(tableRgx, parseTable);
 

+ 21 - 0
test/features/tables/#443.2.table-followed-by-list-does-not-parse-correctly.html

@@ -0,0 +1,21 @@
+<table>
+    <thead>
+    <tr>
+        <th>Tables</th>
+    </tr>
+    </thead>
+    <tbody>
+    <tr>
+        <td><strong>col 3 is</strong></td>
+    </tr>
+    <tr>
+        <td>col 2 is</td>
+    </tr>
+    <tr>
+        <td>zebra stripes</td>
+    </tr>
+    </tbody>
+</table>
+<ol>
+    <li>test</li>
+</ol>

+ 7 - 0
test/features/tables/#443.2.table-followed-by-list-does-not-parse-correctly.md

@@ -0,0 +1,7 @@
+| Tables        |
+| ------------- |
+| **col 3 is**  |
+| col 2 is      |
+| zebra stripes |
+
+1. test

+ 29 - 0
test/features/tables/#443.table-followed-by-list-does-not-parse-correctly.html

@@ -0,0 +1,29 @@
+<table>
+<thead>
+<tr>
+    <th>Tables</th>
+    <th style="text-align:center;">Are</th>
+    <th style="text-align:right;">Cool</th>
+</tr>
+</thead>
+<tbody>
+<tr>
+    <td><strong>col 3 is</strong></td>
+    <td style="text-align:center;">right-aligned</td>
+    <td style="text-align:right;">$1600</td>
+</tr>
+<tr>
+    <td>col 2 is</td>
+    <td style="text-align:center;"><em>centered</em></td>
+    <td style="text-align:right;">$12</td>
+</tr>
+<tr>
+    <td>zebra stripes</td>
+    <td style="text-align:center;">are neat</td>
+    <td style="text-align:right;">$1</td>
+</tr>
+</tbody>
+</table>
+<ol>
+    <li>test</li>
+</ol>

+ 7 - 0
test/features/tables/#443.table-followed-by-list-does-not-parse-correctly.md

@@ -0,0 +1,7 @@
+| Tables        | Are           | Cool  |
+| ------------- |:-------------:| -----:|
+| **col 3 is**  | right-aligned | $1600 |
+| col 2 is      | *centered*    |   $12 |
+| zebra stripes | are neat      |    $1 |
+
+1. test

Daži faili netika attēloti, jo izmaiņu fails ir pārāk liels