0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-24 18:32:41 +00:00

Fix multiline IMG tags breaking

This commit is contained in:
Trevor Buckner
2020-05-08 13:15:35 -04:00
parent 62f549f038
commit a0e92b54d0
2 changed files with 63 additions and 57 deletions

View File

@@ -9,6 +9,7 @@ module.exports = {
},
env : {
browser : true,
node: true
},
plugins : ['react'],
rules : {
@@ -66,7 +67,7 @@ module.exports = {
multiLine : { beforeColon: true, afterColon: true, align: 'colon' },
singleLine : { beforeColon: false, afterColon: true }
}],
'linebreak-style' : ['warn', 'unix'],
'linebreak-style' : 'off',
'no-trailing-spaces' : 'warn',
'no-whitespace-before-property' : 'warn',
'object-curly-spacing' : ['warn', 'always'],
@@ -74,4 +75,4 @@ module.exports = {
'space-in-parens' : ['warn', 'never'],
'template-curly-spacing' : ['warn', 'never'],
}
};
};

View File

@@ -2,74 +2,79 @@ const _ = require('lodash');
const Markdown = require('marked');
// Copied directly from Marked helpers.js source with permission
function splitCells(tableRow, count) {
// ensure that every cell-delimiting pipe has a space
// before it to distinguish it from an escaped pipe
const row = tableRow.replace(/\|/g, (match, offset, str) => {
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;
const splitCells = function(tableRow, count) {
// ensure that every cell-delimiting pipe has a space
// before it to distinguish it from an escaped pipe
const row = tableRow.replace(/\|/g, (match, offset, str)=>{
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('');
}
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;
}
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) {
html = _.trim(html)
html = _.trim(html);
// Process the markdown within Divs
if(_.startsWith(html, '<div') && html.includes('>')) {
let openTag = html.substring(0, html.indexOf('>')+1);
html = html.substring(html.indexOf('>')+1);
return `${openTag} ${Markdown(html)}`;
}
// Process the markdown within Divs
if(_.startsWith(html, '<div') && html.includes('>')) {
const openTag = html.substring(0, html.indexOf('>')+1);
html = html.substring(html.indexOf('>')+1);
return `${openTag} ${Markdown(html)}`;
}
// Don't require a blank line after HTML to parse Markdown
if(html.includes('\n')) {
if(_.startsWith(html, '<style') || _.startsWith(html, '<pre') || _.startsWith(html, '<img')) {
if(html.includes('>')) {
const openTag = html.substring(0, html.lastIndexOf('>')+1);
html = html.substring(html.lastIndexOf('>')+1);
return `${openTag} ${Markdown(html)}`;
}
return html; // Style, Pre, and Img tags should not parse Markdown
}
const openTag = html.substring(0, html.indexOf('\n')+1);
html = html.substring(html.indexOf('\n')+1);
return `${openTag} ${Markdown(html)}`;
}
// Don't require a blank line after HTML to parse Markdown
if(html.includes('\n')) {
if(_.startsWith(html, '<style') || _.startsWith(html, '<pre')) {
return html; // Style and Pre tags should not parse Markdown
}
let openTag = html.substring(0, html.indexOf('\n')+1);
html = html.substring(html.indexOf('\n')+1);
return `${openTag} ${Markdown(html)}`;
}
return html;
}
};
const tokenizer = {
//Adjust tables to work even if columns are uneven
//Adjust tables to work even if columns are uneven
table(src) {
const cap = this.rules.block.table.exec(src);
if (cap) {
if(cap) {
const item = {
type: 'table',
header: splitCells(cap[1].replace(/^ *| *\| *$/g, '')),
align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
cells: cap[3] ? cap[3].replace(/\n$/, '').split('\n') : []
type : 'table',
header : splitCells(cap[1].replace(/^ *| *\| *$/g, '')),
align : cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
cells : cap[3] ? cap[3].replace(/\n$/, '').split('\n') : []
};
item.raw = cap[0];
@@ -77,11 +82,11 @@ const tokenizer = {
let l = item.align.length;
let i;
for (i = 0; i < l; i++) {
if (/^ *-+: *$/.test(item.align[i])) {
if(/^ *-+: *$/.test(item.align[i])) {
item.align[i] = 'right';
} else if (/^ *:-+: *$/.test(item.align[i])) {
} else if(/^ *:-+: *$/.test(item.align[i])) {
item.align[i] = 'center';
} else if (/^ *:-+ *$/.test(item.align[i])) {
} else if(/^ *:-+ *$/.test(item.align[i])) {
item.align[i] = 'left';
} else {
item.align[i] = null;