You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-08-13 22:12:50 +02:00
Convert enex tables to md tables
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
const BLOCK_OPEN = "<div>";
|
import stringPadding from 'string-padding';
|
||||||
const BLOCK_CLOSE = "</div>";
|
|
||||||
const NEWLINE = "<br/>";
|
const BLOCK_OPEN = "[[BLOCK_OPEN]]";
|
||||||
const NEWLINE_MERGED = "<merged/>";
|
const BLOCK_CLOSE = "[[BLOCK_CLOSE]]";
|
||||||
const SPACE = "<space/>";
|
const NEWLINE = "[[NEWLINE]]";
|
||||||
|
const NEWLINE_MERGED = "[[MERGED]]";
|
||||||
|
const SPACE = "[[SPACE]]";
|
||||||
|
|
||||||
function processMdArrayNewLines(md) {
|
function processMdArrayNewLines(md) {
|
||||||
while (md.length && md[0] == BLOCK_OPEN) {
|
while (md.length && md[0] == BLOCK_OPEN) {
|
||||||
@@ -248,34 +250,6 @@ function enexXmlToMdArray(stream, resources) {
|
|||||||
section.lines = collapseWhiteSpaceAndAppend(section.lines, state, text);
|
section.lines = collapseWhiteSpaceAndAppend(section.lines, state, text);
|
||||||
})
|
})
|
||||||
|
|
||||||
// Section: {
|
|
||||||
// type: "block/table/tr/td",
|
|
||||||
// lines: []
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
// {
|
|
||||||
// type: 'text',
|
|
||||||
// lines: [
|
|
||||||
// 'this is a line',
|
|
||||||
// '<br>',
|
|
||||||
// {
|
|
||||||
// type: 'table',
|
|
||||||
// trs: [
|
|
||||||
// {
|
|
||||||
// tds: [
|
|
||||||
// {
|
|
||||||
// lines: [],
|
|
||||||
// }
|
|
||||||
// ],
|
|
||||||
// }
|
|
||||||
// ],
|
|
||||||
// }
|
|
||||||
// ]
|
|
||||||
// }
|
|
||||||
|
|
||||||
//output.push(section);
|
|
||||||
|
|
||||||
saxStream.on('opentag', function(node) {
|
saxStream.on('opentag', function(node) {
|
||||||
let n = node.name.toLowerCase();
|
let n = node.name.toLowerCase();
|
||||||
if (n == 'en-note') {
|
if (n == 'en-note') {
|
||||||
@@ -299,6 +273,7 @@ function enexXmlToMdArray(stream, resources) {
|
|||||||
type: 'tr',
|
type: 'tr',
|
||||||
lines: [],
|
lines: [],
|
||||||
parent: section,
|
parent: section,
|
||||||
|
isHeader: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
section.lines.push(newSection);
|
section.lines.push(newSection);
|
||||||
@@ -306,6 +281,8 @@ function enexXmlToMdArray(stream, resources) {
|
|||||||
} else if (n == 'td' || n == 'th') {
|
} else if (n == 'td' || n == 'th') {
|
||||||
if (section.type != 'tr') throw new Error('Found a <td> tag outside of a <tr>');
|
if (section.type != 'tr') throw new Error('Found a <td> tag outside of a <tr>');
|
||||||
|
|
||||||
|
if (n == 'th') section.isHeader = true;
|
||||||
|
|
||||||
let newSection = {
|
let newSection = {
|
||||||
type: 'td',
|
type: 'td',
|
||||||
lines: [],
|
lines: [],
|
||||||
@@ -516,69 +493,138 @@ function enexXmlToMdArray(stream, resources) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function processMdArrayTables(mdArray) {
|
function setTableCellContent(table) {
|
||||||
|
if (!table.type == 'table') throw new Error('Only for tables');
|
||||||
|
|
||||||
|
for (let trIndex = 0; trIndex < table.lines.length; trIndex++) {
|
||||||
|
const tr = table.lines[trIndex];
|
||||||
|
for (let tdIndex = 0; tdIndex < tr.lines.length; tdIndex++) {
|
||||||
|
const td = tr.lines[tdIndex];
|
||||||
|
td.content = processMdArrayNewLines(td.lines);
|
||||||
|
td.content = td.content.replace(/\n\n\n\n\n/g, ' ');
|
||||||
|
td.content = td.content.replace(/\n\n\n\n/g, ' ');
|
||||||
|
td.content = td.content.replace(/\n\n\n/g, ' ');
|
||||||
|
td.content = td.content.replace(/\n\n/g, ' ');
|
||||||
|
td.content = td.content.replace(/\n/g, ' ');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
|
||||||
|
function cellWidth(cellText) {
|
||||||
|
const lines = cellText.split("\n");
|
||||||
|
let maxWidth = 0;
|
||||||
|
for (let i = 0; i < lines.length; i++) {
|
||||||
|
const line = lines[i];
|
||||||
|
if (line.length > maxWidth) maxWidth = line.length;
|
||||||
|
}
|
||||||
|
return maxWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
function colWidths(table) {
|
||||||
let output = [];
|
let output = [];
|
||||||
|
for (let trIndex = 0; trIndex < table.lines.length; trIndex++) {
|
||||||
for (let i = 0; i < mdArray.length; i++) {
|
const tr = table.lines[trIndex];
|
||||||
let item = mdArray[i];
|
for (let tdIndex = 0; tdIndex < tr.lines.length; tdIndex++) {
|
||||||
|
const td = tr.lines[tdIndex];
|
||||||
if (typeof item == 'string') {
|
const w = cellWidth(td.content);
|
||||||
output.push(item);
|
if (output.length <= tdIndex) output.push(0);
|
||||||
} else if (item.type == 'table') {
|
if (w > output[tdIndex]) output[tdIndex] = w;
|
||||||
output.push('[[TABLE]]');
|
|
||||||
output = output.concat(await processMdArrayTables(item.lines));
|
|
||||||
} else if (item.type == 'tr') {
|
|
||||||
output.push('[[TR]]');
|
|
||||||
output = output.concat(await processMdArrayTables(item.lines));
|
|
||||||
} else if (item.type == 'td') {
|
|
||||||
output.push('[[TD]]');
|
|
||||||
output = output.concat(await processMdArrayTables(item.lines));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
function addTableDimensions(mdArray) {
|
// function wrapLine(line, maxWidth) {
|
||||||
let currentTable = null;
|
// if (line.length <= maxWidth) return line;
|
||||||
|
|
||||||
for (let i = 0; i < mdArray.length; i++) {
|
// let output = [];
|
||||||
let item = mdArray[i];
|
// while (line.length) {
|
||||||
|
// const l = line.substr(0, maxWidth);
|
||||||
|
// line = line.substr(maxWidth);
|
||||||
|
// output.push(l);
|
||||||
|
// }
|
||||||
|
|
||||||
if (typeof item == 'string') {
|
// return output.join("\n");
|
||||||
|
// }
|
||||||
|
|
||||||
} else if (item.type == 'table') {
|
// function wrapCellLines(cellText, maxWidth) {
|
||||||
let colWidths = [];
|
// const lines = cellText.split("\n");
|
||||||
for (let trIndex = 0; trIndex < item.lines.length; trIndex++) {
|
// let output = [];
|
||||||
let tdLines = item.lines[trIndex].lines;
|
// for (let i = 0; i < lines.length; i++) {
|
||||||
for (let tdIndex = 0; tdIndex < tdLines.length; tdIndex++) {
|
// let line = wrapLine(lines[i], maxWidth);
|
||||||
let tdItem = tdLines[tdIndex];
|
// output.push(line);
|
||||||
let tdWidth = 0;
|
// }
|
||||||
for (let j = 0; j < tdItem.lines.length; j++) {
|
// return output.join("\n");
|
||||||
let s = tdItem.lines[j];
|
// }
|
||||||
if (s.length > tdWidth) tdWidth = s.length;
|
|
||||||
|
function drawTable(table, colWidths) {
|
||||||
|
// | First Header | Second Header |
|
||||||
|
// | ------------- | ------------- |
|
||||||
|
// | Content Cell | Content Cell |
|
||||||
|
// | Content Cell | Content Cell |
|
||||||
|
|
||||||
|
let lines = [];
|
||||||
|
let headerDone = false;
|
||||||
|
for (let trIndex = 0; trIndex < table.lines.length; trIndex++) {
|
||||||
|
const tr = table.lines[trIndex];
|
||||||
|
const isHeader = tr.isHeader;
|
||||||
|
let line = [];
|
||||||
|
let headerLine = [];
|
||||||
|
let emptyHeader = null;
|
||||||
|
for (let tdIndex = 0; tdIndex < colWidths.length; tdIndex++) {
|
||||||
|
const width = colWidths[tdIndex];
|
||||||
|
const cell = tr.lines[tdIndex].content;
|
||||||
|
line.push(stringPadding(cell, width, ' ', stringPadding.RIGHT));
|
||||||
|
|
||||||
|
if (!headerDone) {
|
||||||
|
if (!isHeader) {
|
||||||
|
if (!emptyHeader) emptyHeader = [];
|
||||||
|
let h = stringPadding(' ', width, ' ', stringPadding.RIGHT);
|
||||||
|
if (!width) h = '';
|
||||||
|
emptyHeader.push(h);
|
||||||
}
|
}
|
||||||
if (tdWidth > colWidths[tdIndex] || typeof colWidths[tdIndex] === 'undefined') colWidths[tdIndex] = tdWidth;
|
headerLine.push('-'.repeat(width));
|
||||||
}
|
|
||||||
}
|
|
||||||
item.colWidths = colWidths;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return mdArray;
|
if (emptyHeader) {
|
||||||
|
lines.push('| ' + emptyHeader.join(' | ') + ' |');
|
||||||
|
lines.push('| ' + headerLine.join(' | ') + ' |');
|
||||||
|
headerDone = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
lines.push('| ' + line.join(' | ') + ' |');
|
||||||
|
|
||||||
|
if (!headerDone) {
|
||||||
|
lines.push('| ' + headerLine.join(' | ') + ' |');
|
||||||
|
headerDone = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return lines.join('<<<<:D>>>>' + NEWLINE + '<<<<:D>>>>').split('<<<<:D>>>>');
|
||||||
}
|
}
|
||||||
|
|
||||||
async function enexXmlToMd(stream, resources) {
|
async function enexXmlToMd(stream, resources) {
|
||||||
let result = await enexXmlToMdArray(stream, resources);
|
let result = await enexXmlToMdArray(stream, resources);
|
||||||
|
|
||||||
// let bla = addTableDimensions(result.content.lines);
|
let mdLines = [];
|
||||||
// const util = require('util')
|
|
||||||
// console.log(util.inspect(bla, false, null));
|
|
||||||
// return '';
|
|
||||||
|
|
||||||
let mdLines = result.content.lines; //await processMdArrayTables(result.content.lines);
|
for (let i = 0; i < result.content.lines.length; i++) {
|
||||||
|
let line = result.content.lines[i];
|
||||||
|
if (typeof line === 'object') { // A table
|
||||||
|
let table = setTableCellContent(line);
|
||||||
|
const cw = colWidths(table);
|
||||||
|
const tableLines = drawTable(table, cw);
|
||||||
|
mdLines.push(BLOCK_OPEN);
|
||||||
|
mdLines = mdLines.concat(tableLines);
|
||||||
|
mdLines.push(BLOCK_CLOSE);
|
||||||
|
} else { // an actual line
|
||||||
|
mdLines.push(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//let mdLines = result.lines;
|
|
||||||
let firstAttachment = true;
|
let firstAttachment = true;
|
||||||
for (let i = 0; i < result.resources.length; i++) {
|
for (let i = 0; i < result.resources.length; i++) {
|
||||||
let r = result.resources[i];
|
let r = result.resources[i];
|
||||||
@@ -588,8 +634,6 @@ async function enexXmlToMd(stream, resources) {
|
|||||||
firstAttachment = false;
|
firstAttachment = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//console.info(mdLines);
|
|
||||||
|
|
||||||
return processMdArrayNewLines(mdLines);
|
return processMdArrayNewLines(mdLines);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user