mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
Turndown: support parsing table without head
This commit is contained in:
parent
d66fd7c1c5
commit
a7bdcf4d54
@ -1505,7 +1505,8 @@ var htmlToText = function(identifier, id, timeStamp, html) {
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
var gfm = turndownPluginGfm.gfm
|
turndownPluginGfm.options.autoHead = true;
|
||||||
|
|
||||||
var ts = new TurndownService({ headingStyle: 'atx',
|
var ts = new TurndownService({ headingStyle: 'atx',
|
||||||
bulletListMarker: '-',
|
bulletListMarker: '-',
|
||||||
emDelimiter: '*',
|
emDelimiter: '*',
|
||||||
@ -1519,7 +1520,8 @@ var htmlToText = function(identifier, id, timeStamp, html) {
|
|||||||
return node.isBlock ? '\n\n' : ''
|
return node.isBlock ? '\n\n' : ''
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
ts.use(gfm);
|
ts.use(turndownPluginGfm.gfm);
|
||||||
|
|
||||||
ts.addRule('emspan', {
|
ts.addRule('emspan', {
|
||||||
filter: 'span',
|
filter: 'span',
|
||||||
replacement: function(content, node, options) {
|
replacement: function(content, node, options) {
|
||||||
|
@ -39,6 +39,7 @@ function strikethrough (turndownService) {
|
|||||||
var indexOf = Array.prototype.indexOf;
|
var indexOf = Array.prototype.indexOf;
|
||||||
var every = Array.prototype.every;
|
var every = Array.prototype.every;
|
||||||
var rules = {};
|
var rules = {};
|
||||||
|
var configs = { autoHead: false };
|
||||||
|
|
||||||
rules.tableCell = {
|
rules.tableCell = {
|
||||||
filter: ['th', 'td'],
|
filter: ['th', 'td'],
|
||||||
@ -64,7 +65,26 @@ rules.tableRow = {
|
|||||||
|
|
||||||
borderCells += cell(border, node.childNodes[i]);
|
borderCells += cell(border, node.childNodes[i]);
|
||||||
}
|
}
|
||||||
|
} else if (configs.autoHead && isFirstRow(node)) {
|
||||||
|
var fakeHead = '';
|
||||||
|
|
||||||
|
for (var i = 0; i < node.childNodes.length; i++) {
|
||||||
|
// Add a fake head.
|
||||||
|
fakeHead += cell('<br>', node.childNodes[i]);
|
||||||
|
|
||||||
|
var border = '---';
|
||||||
|
var align = (
|
||||||
|
node.childNodes[i].getAttribute('align') || ''
|
||||||
|
).toLowerCase();
|
||||||
|
|
||||||
|
if (align) border = alignMap[align] || border;
|
||||||
|
|
||||||
|
borderCells += cell(border, node.childNodes[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return '\n' + fakeHead + '\n' + borderCells + '\n' + content;
|
||||||
}
|
}
|
||||||
|
|
||||||
return '\n' + content + (borderCells ? '\n' + borderCells : '')
|
return '\n' + content + (borderCells ? '\n' + borderCells : '')
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -73,7 +93,8 @@ rules.table = {
|
|||||||
// Only convert tables with a heading row.
|
// Only convert tables with a heading row.
|
||||||
// Tables with no heading row are kept using `keep` (see below).
|
// Tables with no heading row are kept using `keep` (see below).
|
||||||
filter: function (node) {
|
filter: function (node) {
|
||||||
return node.nodeName === 'TABLE' && isHeadingRow(node.rows[0])
|
return node.nodeName === 'TABLE'
|
||||||
|
&& (configs.autoHead || isHeadingRow(node.rows[0]))
|
||||||
},
|
},
|
||||||
|
|
||||||
replacement: function (content) {
|
replacement: function (content) {
|
||||||
@ -90,6 +111,12 @@ rules.tableSection = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function isFirstRow (tr) {
|
||||||
|
var parentNode = tr.parentNode;
|
||||||
|
return parentNode.firstChild === tr
|
||||||
|
&& (parentNode.nodeName === 'TABLE' || isFirstTbody(parentNode));
|
||||||
|
}
|
||||||
|
|
||||||
// A tr is a heading row if:
|
// A tr is a heading row if:
|
||||||
// - the parent is a THEAD
|
// - the parent is a THEAD
|
||||||
// - or if its the first child of the TABLE or the first TBODY (possibly
|
// - or if its the first child of the TABLE or the first TBODY (possibly
|
||||||
@ -128,9 +155,12 @@ function cell (content, node) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function tables (turndownService) {
|
function tables (turndownService) {
|
||||||
turndownService.keep(function (node) {
|
if (!configs.autoHead) {
|
||||||
return node.nodeName === 'TABLE' && !isHeadingRow(node.rows[0])
|
turndownService.keep(function (node) {
|
||||||
});
|
return node.nodeName === 'TABLE' && !isHeadingRow(node.rows[0])
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
for (var key in rules) turndownService.addRule(key, rules[key]);
|
for (var key in rules) turndownService.addRule(key, rules[key]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,6 +189,7 @@ exports.highlightedCodeBlock = highlightedCodeBlock;
|
|||||||
exports.strikethrough = strikethrough;
|
exports.strikethrough = strikethrough;
|
||||||
exports.tables = tables;
|
exports.tables = tables;
|
||||||
exports.taskListItems = taskListItems;
|
exports.taskListItems = taskListItems;
|
||||||
|
exports.options = configs;
|
||||||
|
|
||||||
return exports;
|
return exports;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user