From e8f3b0c8d000f48981b50a2ef95b32748bd17017 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Sat, 2 May 2020 15:03:18 -0400 Subject: [PATCH] Fix Tables with uneven columns --- shared/naturalcrit/markdown.js | 135 +++++++++++++++++++++++++-------- 1 file changed, 103 insertions(+), 32 deletions(-) diff --git a/shared/naturalcrit/markdown.js b/shared/naturalcrit/markdown.js index 266230d5b..e6e6be292 100644 --- a/shared/naturalcrit/markdown.js +++ b/shared/naturalcrit/markdown.js @@ -1,40 +1,112 @@ const _ = require('lodash'); const Markdown = require('marked'); -const renderer = new Markdown.Renderer(); -const lexer = new Markdown.Lexer(); -//Processes the markdown within an HTML block if it's just a class-wrapper -renderer.html = function (html) { - if(_.startsWith(_.trim(html), '')+1); - let closeTag = ''; - html = html.substring(html.indexOf('>')+1); - if(_.endsWith(_.trim(html), '')){ - closeTag = ''; - html = html.substring(0,html.lastIndexOf(' { + let escaped = false, + curr = offset; + while (--curr >= 0 && str[curr] === '\\') escaped = !escaped; + if (escaped) { + // odd number of slashes means | is escaped + // so we leave it alone + return '|'; + } else { + // add space before unescaped | + return ' |'; + } + }), + cells = row.split(/ \|/); + let i = 0; + + if (cells.length > count) { + cells.splice(count); + } else { + while (cells.length < count) cells.push(''); + } + + for (; i < cells.length; i++) { + // leading or trailing whitespace is ignored per the gfm spec + cells[i] = cells[i].trim().replace(/\\\|/g, '|'); + } + return cells; +} + +const renderer = { + // Adjust the way html is handled + html(html) { + // Processes the markdown within an HTML block if it's just a class-wrapper + if(_.startsWith(_.trim(html), '')+1); + let closeTag = ''; + html = html.substring(html.indexOf('>')+1); + if(_.endsWith(_.trim(html), '')){ + closeTag = ''; + html = html.substring(0,html.lastIndexOf('')+1); - if(!_.endsWith(_.trim(html), '>')){ // If there is no closing tag, parse markdown directly after - let remainder = html.substring(html.indexOf('>')+1); - return `${openTag} ${Markdown(remainder)}`; + // Allow raw HTML tags to end without a blank line if markdown is right after + if(html.includes('\n')){ + let openTag = html.substring(0, html.indexOf('>')+1); + if(!_.endsWith(_.trim(html), '>')){ // If there is no closing tag, parse markdown directly after + let remainder = html.substring(html.indexOf('>')+1); + return `${openTag} ${Markdown(remainder)}`; + } } + + // Above may work better if we just force + /*if(_.startsWith(_.trim(html), '{ return content .replace(/