Просмотр исходного кода

omit LangProcessor/Tsv2Table and add PreProcessor/CsvToTable

Yuki Takei 8 лет назад
Родитель
Сommit
ae8721a028

+ 1 - 0
package.json

@@ -68,6 +68,7 @@
     "crowi-pluginkit": "^1.1.0",
     "csrf": "~3.0.3",
     "css-loader": "^0.28.0",
+    "csv-to-markdown-table": "^0.4.0",
     "date-fns": "^1.29.0",
     "debug": "^3.1.0",
     "diff": "^3.3.0",

+ 2 - 4
resource/js/util/GrowiRenderer.js

@@ -2,9 +2,9 @@ import MarkdownIt from 'markdown-it';
 import * as entities from 'entities';
 
 import Linker        from './PreProcessor/Linker';
+import CsvToTable    from './PreProcessor/CsvToTable';
 import XssFilter     from './PreProcessor/XssFilter';
 
-import Tsv2Table from './LangProcessor/Tsv2Table';
 import Template from './LangProcessor/Template';
 import PlantUML from './LangProcessor/PlantUML';
 
@@ -19,18 +19,16 @@ export default class GrowiRenderer {
 
     this.preProcessors = [
       new Linker(crowi),
+      new CsvToTable(crowi),
       new XssFilter(crowi),
     ];
     this.postProcessors = [
     ];
-
     this.markdownItConfigurers = [
       new EmojiConfigurer(crowi),
       new MathJaxConfigurer(crowi),
     ];
     this.langProcessors = {
-      'tsv': new Tsv2Table(crowi),
-      'tsv-h': new Tsv2Table(crowi, {header: true}),
       'template': new Template(crowi),
       'plantuml': new PlantUML(crowi),
     };

+ 0 - 85
resource/js/util/LangProcessor/Tsv2Table.js

@@ -1,85 +0,0 @@
-import * as entities from 'entities';
-
-export default class Tsv2Table {
-
-  constructor(crowi, option) {
-    if (!option) {
-      option = {};
-    }
-    this.option = option;
-
-    this.option.header = this.option.header || false;
-  }
-  getCols(codeLines) {
-    let max = 0;
-
-    for (let i = 0; i < codeLines ; i++) {
-      if (max < codeLines.length) {
-        max = codeLines.length;
-      }
-    }
-
-    return max;
-  }
-
-  splitColums(line) {
-    // \t is replaced to '    ' by Lexer.lex(), so split by 4 spaces
-    return line.split(/\s{4}/g);
-  }
-
-  getTableHeader(codeLines, option) {
-    let headers = [];
-    let headLine = (codeLines[0] || '');
-
-    //console.log('head', headLine);
-    headers = this.splitColums(headLine).map(col => {
-      return `<th>${entities.encodeHTML(col)}</th>`;
-    });
-
-    if (headers.length < option.cols) {
-      headers.concat(new Array(option.cols - headers.length));
-    }
-
-    return `<tr>
-      ${headers.join('\n')}
-    </tr>`;
-  }
-
-  getTableBody(codeLines, option) {
-    let rows;
-
-    if (this.option.header) {
-      codeLines.shift();
-    }
-
-    rows = codeLines.map(row => {
-      const cols = this.splitColums(row).map(col => {
-        return `<td>${entities.encodeHTML(col)}</td>`;
-      }).join('');
-      return `<tr>${cols}</tr>`;
-    });
-
-    return rows.join('\n');
-  }
-
-  process(code) {
-    let option = {};
-    const codeLines = code.split(/\n|\r/);
-
-    option.cols = this.getCols(codeLines);
-
-    let header = '';
-    if (this.option.header) {
-      header = `<thead>
-        ${this.getTableHeader(codeLines, option)}
-      </thead>`;
-    }
-
-    return `<table>
-      ${header}
-      <tbody>
-        ${this.getTableBody(codeLines, option)}
-      </tbody>
-    </table>`;
-  }
-}

+ 26 - 0
resource/js/util/PreProcessor/CsvToTable.js

@@ -0,0 +1,26 @@
+import csvToMarkdownTable from 'csv-to-markdown-table';
+
+export default class CsvToTable {
+  process(markdown) {
+
+    // see: https://regex101.com/r/WR6IvX/2
+    return markdown.replace(/```(\S+)[\r\n]((.|[\r\n])*?)[\r\n]```/gm, (all, group1, group2) => {
+      switch (group1) {
+        case 'tsv':
+          return csvToMarkdownTable(group2, '\t');
+          break;
+        case 'tsv-h':
+          return csvToMarkdownTable(group2, '\t', true);
+          break;
+        case 'csv':
+          return csvToMarkdownTable(group2, ',');
+          break;
+        case 'csv-h':
+          return csvToMarkdownTable(group2, ',', true);
+          break;
+        default:
+          return all;
+      }
+    });
+  }
+}

+ 4 - 0
yarn.lock

@@ -1753,6 +1753,10 @@ csso@~2.3.1:
     clap "^1.0.9"
     source-map "^0.5.3"
 
+csv-to-markdown-table@^0.4.0:
+  version "0.4.0"
+  resolved "https://registry.yarnpkg.com/csv-to-markdown-table/-/csv-to-markdown-table-0.4.0.tgz#9040fd58a7bb963f515652f31e48ead8ca516fc7"
+
 currently-unhandled@^0.4.1:
   version "0.4.1"
   resolved "https://registry.yarnpkg.com/currently-unhandled/-/currently-unhandled-0.4.1.tgz#988df33feab191ef799a61369dd76c17adf957ea"