Repository: zestedesavoir/zmarkdown (js)

Issue:
rebber-plugins: grid-tables stringifier replace \n by \par even in \inlineCode
# markdown

```
+------+----------------------+
| cell | `code                |
|      | with line break`     |
+------+----------------------+
```

# generated latex

```tex
\begin{longtabu}{|p{\dimexpr(\linewidth) / 2}|p{\dimexpr(\linewidth) / 2}|} \hline
\rowfont[c]{\bfseries}
cell & \CodeInline{code \par with line break} \\ \hline
\end{longtabu}
```

# expected latex

```tex
\begin{longtabu}{|p{\dimexpr(\linewidth) / 2}|p{\dimexpr(\linewidth) / 2}|} \hline
\rowfont[c]{\bfseries}
cell & \CodeInline{code with line break} \\ \hline
\end{longtabu}
```


Pull request description:
rebber-plugins: fix #352 (\par in codes)
#352 

# Resolution

- remove the `replace(/\n/g, ' \\par ')` 
- add the fixture given on the ticket to generate a proper snapshot
- run the jest command and observe old snapshots fails
- develop the fix while previous step is true
- lerna run build --scope rebber-plugins
- run zmarkdown tests

# Note

On my local version I've got issues with zmarkdown tests because of iframes.


Patch:
diff --git a/packages/rebber-plugins/dist/type/gridTable.js b/packages/rebber-plugins/dist/type/gridTable.js
index e981969a..90770a77 100644
--- a/packages/rebber-plugins/dist/type/gridTable.js
+++ b/packages/rebber-plugins/dist/type/gridTable.js
@@ -14,6 +14,10 @@ var tableCell = require('rebber/dist/types/tableCell');
 var tableRow = require('rebber/dist/types/tableRow');
 
 var table = require('rebber/dist/types/table');
+
+var text = require('rebber/dist/types/text');
+
+var paragraph = require('rebber/dist/types/paragraph');
 /* Expose. */
 
 
@@ -75,8 +79,22 @@ function () {
     value: function gridTableCell(ctx, node) {
       var overriddenCtx = clone(ctx);
       this.colIndex++;
-      overriddenCtx.tableCell = undefined;
-      var baseText = tableCell(overriddenCtx, node).trim().replace(/\n/g, ' \\par ');
+      overriddenCtx.tableCell = undefined; // we have to replace \n by \par only in text node, not in other
+      // see #352
+
+      overriddenCtx.overrides.text = function (c, n, index, parent) {
+        return text(c, n, index, parent).replace(/\n/g, ' \\par ');
+      };
+
+      overriddenCtx.overrides.paragraph = function (c, n) {
+        return "".concat(paragraph(c, n).trim(), " \\par  \\par ");
+      };
+
+      var baseText = tableCell(overriddenCtx, node).trim();
+
+      while (baseText.substring(baseText.length - '\\par'.length) === '\\par') {
+        baseText = baseText.substring(0, baseText.length - '\\par'.length).trim();
+      }
 
       if (node.data && node.data.hProperties.rowspan > 1) {
         this.currentSpan = node.data.hProperties.rowspan;
diff --git a/packages/rebber-plugins/src/type/gridTable.js b/packages/rebber-plugins/src/type/gridTable.js
index 5054c9d7..2cb40dee 100644
--- a/packages/rebber-plugins/src/type/gridTable.js
+++ b/packages/rebber-plugins/src/type/gridTable.js
@@ -4,6 +4,8 @@ const clone = require('clone')
 const tableCell = require('rebber/dist/types/tableCell')
 const tableRow = require('rebber/dist/types/tableRow')
 const table = require('rebber/dist/types/table')
+const text = require('rebber/dist/types/text')
+const paragraph = require('rebber/dist/types/paragraph')
 
 /* Expose. */
 module.exports = gridTable
@@ -49,8 +51,14 @@ class GridTableStringifier {
     const overriddenCtx = clone(ctx)
     this.colIndex++
     overriddenCtx.tableCell = undefined
-    let baseText = tableCell(overriddenCtx, node).trim().replace(/\n/g, ' \\par ')
-
+    // we have to replace \n by \par only in text node, not in other
+    // see #352
+    overriddenCtx.overrides.text = (c, n, index, parent) => text(c, n, index, parent).replace(/\n/g, ' \\par ')
+    overriddenCtx.overrides.paragraph = (c, n) => `${paragraph(c, n).trim()} \\par  \\par `
+    let baseText = tableCell(overriddenCtx, node).trim()
+    while (baseText.substring(baseText.length - '\\par'.length) === '\\par') {
+      baseText = baseText.substring(0, baseText.length - '\\par'.length).trim()
+    }
     if (node.data && node.data.hProperties.rowspan > 1) {
       this.currentSpan = node.data.hProperties.rowspan
       this.multiLineCellIndex = this.colIndex


Test patch:
diff --git a/packages/rebber-plugins/__tests__/__snapshots__/rebber.test.js.snap b/packages/rebber-plugins/__tests__/__snapshots__/rebber.test.js.snap
index 0a5264a7..a0909f34 100644
--- a/packages/rebber-plugins/__tests__/__snapshots__/rebber.test.js.snap
+++ b/packages/rebber-plugins/__tests__/__snapshots__/rebber.test.js.snap
@@ -968,6 +968,26 @@ Video: a caption
 no caption"
 `;
 
+exports[`mix-7 1`] = `
+"\\\\begin{longtabu}{|p{\\\\dimexpr(\\\\linewidth) / 2}|p{\\\\dimexpr(\\\\linewidth) / 2}|} \\\\hline
+\\\\rowfont[c]{\\\\bfseries}
+cell & \\\\texttt{code
+with line break} \\\\\\\\ \\\\hline
+\\\\rowfont[l]{}
+\\\\multicolumn{2}{|p{\\\\dimexpr(\\\\linewidth) * 2 / 2}|}{[]} \\\\\\\\ \\\\hline
+\\\\end{longtabu}
+
+
+\\\\part{Annexes}
+
+
+\\\\footnote{\\\\label{appendix-1}}
+\\\\begin{CodeBlock}{text}
+line 1
+line2
+\\\\end{CodeBlock}"
+`;
+
 exports[`paragraph 1`] = `
 "\\\\part{first 1}
 
diff --git a/packages/rebber-plugins/__tests__/fixtures/mix-7.fixture.md b/packages/rebber-plugins/__tests__/fixtures/mix-7.fixture.md
new file mode 100644
index 00000000..df472e26
--- /dev/null
+++ b/packages/rebber-plugins/__tests__/fixtures/mix-7.fixture.md
@@ -0,0 +1,9 @@
++------+----------------------+
+| cell | `code                |
+|      | with line break`     |
++------+----------------------+
+|  ```text                    |
+| line 1                      |
+| line2                       |
+|```                          |
++------+----------------------+
diff --git a/packages/zmarkdown/__tests__/__snapshots__/old-suite-latex.test.js.snap b/packages/zmarkdown/__tests__/__snapshots__/old-suite-latex.test.js.snap
index 8ca44329..ba0bd32e 100644
--- a/packages/zmarkdown/__tests__/__snapshots__/old-suite-latex.test.js.snap
+++ b/packages/zmarkdown/__tests__/__snapshots__/old-suite-latex.test.js.snap
@@ -4230,7 +4230,12 @@ D & E & F & G \\\\\\\\ \\\\hline
 \\\\rowfont[c]{\\\\bfseries}
 A & \\\\multicolumn{6}{|p{\\\\dimexpr(\\\\linewidth) * 6 / 7}|}{B} \\\\\\\\ \\\\hline
 \\\\rowfont[c]{\\\\bfseries}
-\\\\multirow{4}{*}{C} & \\\\multirow{4}{*}{\\\\begin{longtabu}{|p{\\\\dimexpr(\\\\linewidth) / 4}|p{\\\\dimexpr(\\\\linewidth) / 4}|p{\\\\dimexpr(\\\\linewidth) / 4}|p{\\\\dimexpr(\\\\linewidth) / 4}|} \\\\cline{1-1} \\\\par \\\\rowfont[c]{\\\\bfseries} \\\\par D & E & F & G \\\\\\\\ \\\\hline \\\\par \\\\rowfont[l]{} \\\\par \\\\multicolumn{4}{|p{\\\\dimexpr(\\\\linewidth) * 4 / 4}|}{H} \\\\\\\\ \\\\hline \\\\par \\\\end{longtabu}} \\\\\\\\ \\\\hline
+\\\\multirow{4}{*}{C} & \\\\multirow{4}{*}{\\\\begin{longtabu}{|p{\\\\dimexpr(\\\\linewidth) / 4}|p{\\\\dimexpr(\\\\linewidth) / 4}|p{\\\\dimexpr(\\\\linewidth) / 4}|p{\\\\dimexpr(\\\\linewidth) / 4}|} \\\\cline{1-1}
+\\\\rowfont[c]{\\\\bfseries}
+D & E & F & G \\\\\\\\ \\\\hline
+\\\\rowfont[l]{}
+\\\\multicolumn{4}{|p{\\\\dimexpr(\\\\linewidth) * 4 / 4}|}{H} \\\\\\\\ \\\\hline
+\\\\end{longtabu}} \\\\\\\\ \\\\hline
 \\\\rowfont[l]{}
  &  &  &  &  &  \\\\\\\\ \\\\cline{1-1}
  &  &  &  &  &  \\\\\\\\ \\\\cline{1-1}