1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-11 18:24:43 +02:00

Desktop, Cli: Add table captions when importing ENEX files

This commit is contained in:
Laurent Cozic 2020-12-25 17:44:51 +00:00
parent 9e2f60523f
commit b8f14d50f5
3 changed files with 69 additions and 2 deletions

View File

@ -0,0 +1,11 @@
<table>
<caption><en-media alt="Video recordings download options" width="20" height="20" style="border:0px;margin:0px;padding:0px;-webkit-text-size-adjust:none;vertical-align:text-bottom;max-width:100%;height:auto;margin-right:5px;" hash="50b609a6abfeddd7005382bdcc6120f4" type="image/png"></en-media>MP4</caption>
<tr>
<th>Video Segments</th>
<th>Download</th>
</tr>
<tr>
<td>one</td>
<td>two</td>
</tr>
</table>

View File

@ -0,0 +1,5 @@
| Video Segments | Download |
| --- | --- |
| one | two |
MP4

View File

@ -422,6 +422,27 @@ function displaySaxWarning(context, message) {
console.warn(line.join(': '));
}
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
function removeSectionParent(section) {
if (typeof section === 'string') return section;
section = { ...section };
delete section.parent;
section.lines = section.lines.slice();
for (let i = 0; i < section.lines.length; i++) {
section.lines[i] = removeSectionParent(section.lines[i]);
}
return section;
}
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
function printSection(section) {
console.info(JSON.stringify(removeSectionParent(section), null, 4));
}
function enexXmlToMdArray(stream, resources) {
const remainingResources = resources.slice();
@ -570,6 +591,20 @@ function enexXmlToMdArray(stream, resources) {
parent: section,
};
section.lines.push(newSection);
section = newSection;
} else if (n == 'caption') {
if (section.type != 'table') {
displaySaxWarning(this, 'Found a <caption> tag outside of a <table>');
// return;
}
const newSection = {
type: 'caption',
lines: [],
parent: section,
};
section.lines.push(newSection);
section = newSection;
} else if (isInvisibleBlock(nodeAttributes)) {
@ -780,7 +815,7 @@ function enexXmlToMdArray(stream, resources) {
section.lines.push(BLOCK_CLOSE);
} else if (n == 'td' || n == 'th') {
if (section && section.parent) section = section.parent;
} else if (n == 'tr') {
} else if (n == 'tr' || n == 'caption') {
if (section && section.parent) section = section.parent;
} else if (n == 'table') {
if (section && section.parent) section = section.parent;
@ -978,6 +1013,10 @@ function tableHasSubTables(table) {
for (let tdIndex = 0; tdIndex < tr.lines.length; tdIndex++) {
const td = tr.lines[tdIndex];
// We are inside a CAPTION, not a TD
if (typeof td === 'string') continue;
for (let i = 0; i < td.lines.length; i++) {
if (typeof td.lines[i] === 'object') return true;
}
@ -1003,8 +1042,15 @@ function drawTable(table) {
let lines = [];
lines.push(BLOCK_OPEN);
let headerDone = false;
let caption = null;
for (let trIndex = 0; trIndex < table.lines.length; trIndex++) {
const tr = table.lines[trIndex];
if (tr.type === 'caption') {
caption = tr;
continue;
}
const isHeader = tr.isHeader;
const line = [];
const headerLine = [];
@ -1027,7 +1073,7 @@ function drawTable(table) {
// In here, recursively render the tables
for (let i = 0; i < td.lines.length; i++) {
const c = td.lines[i];
if (typeof c === 'object' && ['table', 'td', 'tr', 'th'].indexOf(c.type) >= 0) {
if (typeof c === 'object' && ['table', 'td', 'tr', 'th', 'caption'].indexOf(c.type) >= 0) {
// This is a table
renderCurrentCells();
currentCells = currentCells.concat(drawTable(c));
@ -1095,6 +1141,11 @@ function drawTable(table) {
lines.push(BLOCK_CLOSE);
if (caption) {
const captionLines = renderLines(caption.lines);
lines = lines.concat(captionLines);
}
return flatRender ? lines : lines.join(`<<<<:D>>>>${NEWLINE}<<<<:D>>>>`).split('<<<<:D>>>>');
}