import markdownUtils from './markdownUtils'; import { ResourceEntity } from './services/database/types'; import { htmlentities } from '@joplin/utils/html'; const stringPadding = require('string-padding'); const stringToStream = require('string-to-stream'); const resourceUtils = require('./resourceUtils.js'); const cssParser = require('@adobe/css-tools'); const BLOCK_OPEN = '[[BLOCK_OPEN]]'; const BLOCK_CLOSE = '[[BLOCK_CLOSE]]'; const NEWLINE = '[[NEWLINE]]'; const NEWLINE_MERGED = '[[MERGED]]'; const SPACE = '[[SPACE]]'; enum SectionType { Text = 'text', Tr = 'tr', Td = 'td', Table = 'table', Caption = 'caption', Hidden = 'hidden', Code = 'code', } interface Section { type: SectionType; parent: Section; lines: any[]; isHeader?: boolean; } interface ParserStateTag { name: string; visible: boolean; isCodeBlock: boolean; isHighlight: boolean; } enum ListTag { Ul = 'ul', Ol = 'ol', CheckboxList = 'checkboxList', TaskList = 'taskList', } interface ParserStateList { tag: ListTag; counter: number; startedText: boolean; } interface ParserState { inCode: boolean[]; inPre: boolean; inQuote: boolean; lists: ParserStateList[]; anchorAttributes: any[]; spanAttributes: string[]; tags: ParserStateTag[]; currentCode?: string; } interface ExtractedTask { title: string; completed: boolean; groupId: string; } interface EnexXmlToMdArrayResult { content: Section; resources: ResourceEntity[]; } function processMdArrayNewLines(md: string[]): string { while (md.length && md[0] === BLOCK_OPEN) { md.shift(); } while (md.length && md[md.length - 1] === BLOCK_CLOSE) { md.pop(); } let temp = []; let last = ''; for (let i = 0; i < md.length; i++) { const v = md[i]; if (isNewLineBlock(last) && isNewLineBlock(v) && last === v) { // Skip it } else { temp.push(v); } last = v; } md = temp; temp = []; last = ''; for (let i = 0; i < md.length; i++) { const v = md[i]; if (last === BLOCK_CLOSE && v === BLOCK_OPEN) { temp.pop(); temp.push(NEWLINE_MERGED); } else { temp.push(v); } last = v; } md = temp; temp = []; last = ''; for (let i = 0; i < md.length; i++) { const v = md[i]; if (last === NEWLINE && (v === NEWLINE_MERGED || v === BLOCK_CLOSE)) { // Skip it } else { temp.push(v); } last = v; } md = temp; // NEW!!! temp = []; last = ''; for (let i = 0; i < md.length; i++) { const v = md[i]; if (last === NEWLINE && (v === NEWLINE_MERGED || v === BLOCK_OPEN)) { // Skip it } else { temp.push(v); } last = v; } md = temp; if (md.length > 2) { if (md[md.length - 2] === NEWLINE_MERGED && md[md.length - 1] === NEWLINE) { md.pop(); } } let output = ''; let previous = ''; let start = true; for (let i = 0; i < md.length; i++) { const v = md[i]; let add = ''; if (v === BLOCK_CLOSE || v === BLOCK_OPEN || v === NEWLINE || v === NEWLINE_MERGED) { add = '\n'; } else if (v === SPACE) { if (previous === SPACE || previous === '\n' || start) { continue; // skip } else { add = ' '; } } else { add = v; } start = false; output += add; previous = add; } if (!output.trim().length) return ''; // To simplify the result, we only allow up to one empty line between blocks of text const mergeMultipleNewLines = function(lines: string[]) { const output = []; let newlineCount = 0; for (let i = 0; i < lines.length; i++) { const line = lines[i]; if (!line.trim()) { newlineCount++; } else { newlineCount = 0; } if (newlineCount >= 2) continue; output.push(line); } return output; }; let lines = output.replace(/\\r/g, '').split('\n'); lines = formatMdLayout(lines); lines = mergeMultipleNewLines(lines); return lines.join('\n'); } // While the processMdArrayNewLines() function adds newlines in a way that's technically correct, the resulting Markdown can look messy. // This is because while a "block" element should be surrounded by newlines, in practice, some should be surrounded by TWO new lines, while // others by only ONE. // // For instance, this: // //
Some long paragraph
And another one
And the last paragraph
// // should result in this: // // Some long paragraph // // And another one // // And the last paragraph // // So in one case, one newline between tags, and in another two newlines. In HTML this would be done via CSS, but in Markdown we need // to add new lines. It's also important to get these newlines right because two blocks of text next to each others might be renderered // differently than if there's a newlines between them. So the function below parses the almost final MD and add new lines depending // on various rules. const isHeading = function(line: string) { return !!line.match(/^#+\s/); }; const isListItem = function(line: string) { return line && line.trim().indexOf('- ') === 0; }; const isCodeLine = function(line: string) { return line && line.indexOf('\t') === 0; }; const isTableLine = function(line: string) { return line.indexOf('| ') === 0; }; const isPlainParagraph = function(line: string) { // Note: if a line is no longer than 80 characters, we don't consider it's a paragraph, which // means no newlines will be added before or after. This is to handle text that has been // written with "hard" new lines. if (!line || line.length < 80) return false; if (isListItem(line)) return false; if (isHeading(line)) return false; if (isCodeLine(line)) return false; if (isTableLine(line)) return false; return true; }; function formatMdLayout(lines: string[]) { let previous = ''; const newLines = []; for (let i = 0; i < lines.length; i++) { const line = lines[i]; // Add a new line at the end of a list of items if (isListItem(previous) && line && !isListItem(line)) { newLines.push(''); // Add a new line at the beginning of a list of items } else if (isListItem(line) && previous && !isListItem(previous)) { newLines.push(''); // Add a new line before a heading } else if (isHeading(line) && previous) { newLines.push(''); // Add a new line after a heading } else if (isHeading(previous) && line) { newLines.push(''); } else if (isCodeLine(line) && !isCodeLine(previous)) { newLines.push(''); } else if (!isCodeLine(line) && isCodeLine(previous)) { newLines.push(''); } else if (isTableLine(line) && !isTableLine(previous)) { newLines.push(''); } else if (!isTableLine(line) && isTableLine(previous)) { newLines.push(''); // Add a new line at beginning of paragraph } else if (isPlainParagraph(line) && previous) { newLines.push(''); // Add a new line at end of paragraph } else if (isPlainParagraph(previous) && line) { newLines.push(''); } newLines.push(line); previous = newLines[newLines.length - 1]; } return newLines; } function isWhiteSpace(c: string): boolean { return c === '\n' || c === '\r' || c === '\v' || c === '\f' || c === '\t' || c === ' '; } // Like QString::simplified(), except that it preserves non-breaking spaces (which // Evernote uses for indentation, etc.) function simplifyString(s: string): string { let output = ''; let previousWhite = false; for (let i = 0; i < s.length; i++) { const c = s[i]; const isWhite = isWhiteSpace(c); if (previousWhite && isWhite) { // skip } else { output += c; } previousWhite = isWhite; } while (output.length && isWhiteSpace(output[0])) output = output.substr(1); while (output.length && isWhiteSpace(output[output.length - 1])) output = output.substr(0, output.length - 1); return output; } function collapseWhiteSpaceAndAppend(lines: string[], state: any, text: string) { if (state.inCode.length) { lines.push(text); } else { // Remove all \n and \r from the left and right of the text while (text.length && (text[0] === '\n' || text[0] === '\r')) text = text.substr(1); while (text.length && (text[text.length - 1] === '\n' || text[text.length - 1] === '\r')) text = text.substr(0, text.length - 1); // Collapse all white spaces to just one. If there are spaces to the left and right of the string // also collapse them to just one space. const spaceLeft = text.length && text[0] === ' '; const spaceRight = text.length && text[text.length - 1] === ' '; text = simplifyString(text); if (!spaceLeft && !spaceRight && text === '') return lines; if (state.inQuote) { // Add a ">" at the beginning of the block then at the beginning of each lines. So it turns this: // "my quote\nsecond line" into this => "> my quote\n> second line" lines.push('> '); if (lines.indexOf('\r') >= 0) { text = text.replace(/\n\r/g, '\n\r> '); } else { text = text.replace(/\n/g, '\n> '); } } if (spaceLeft) lines.push(SPACE); lines.push(text); if (spaceRight) lines.push(SPACE); } return lines; } function tagAttributeToMdText(attr: string): string { // HTML attributes may contain newlines so remove them. // https://github.com/laurent22/joplin/issues/1583 if (!attr) return ''; attr = attr.replace(/[\n\r]+/g, ' '); attr = attr.replace(/\]/g, '\\]'); return attr; } interface AddResourceOptions { alt?: string; width?: number; height?: number; } const addResourceTag = (lines: string[], src: string, mime: string, options: AddResourceOptions): string[] => { const alt = options.alt ? tagAttributeToMdText(options.alt) : ''; if (resourceUtils.isImageMimeType(mime)) { if (!!options.width || !!options.height) { const attrs: Record
// elements by ) and only the inner ones, those that don't contain any other tables, are rendered as actual tables. This is generally
// the required behaviour since the outer tables are usually for layout and the inner ones are the content.
function drawTable(table: Section) {
// | First Header | Second Header |
// | ------------- | ------------- |
// | Content Cell | Content Cell |
// | Content Cell | Content Cell |
// There must be at least 3 dashes separating each header cell.
// https://gist.github.com/IanWang/28965e13cdafdef4e11dc91f578d160d#tables
const flatRender = tableHasSubTables(table); // Render the table has regular text
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;
}
if (typeof tr === 'string') {
// A |