2018-09-29 14:15:36 +02:00
|
|
|
const stringPadding = require('string-padding');
|
2018-05-23 13:14:38 +02:00
|
|
|
const urlUtils = require('lib/urlUtils');
|
2018-09-24 21:15:23 +02:00
|
|
|
const MarkdownIt = require('markdown-it');
|
2019-05-10 02:06:06 +02:00
|
|
|
const setupLinkify = require('lib/MdToHtml/setupLinkify');
|
2018-05-23 13:14:38 +02:00
|
|
|
|
2017-08-02 19:47:25 +02:00
|
|
|
const markdownUtils = {
|
|
|
|
|
|
|
|
// Not really escaping because that's not supported by marked.js
|
|
|
|
escapeLinkText(text) {
|
|
|
|
return text.replace(/(\[|\]|\(|\))/g, '_');
|
|
|
|
},
|
|
|
|
|
|
|
|
escapeLinkUrl(url) {
|
|
|
|
url = url.replace(/\(/g, '%28');
|
|
|
|
url = url.replace(/\)/g, '%29');
|
|
|
|
return url;
|
|
|
|
},
|
|
|
|
|
2018-05-23 13:14:38 +02:00
|
|
|
prependBaseUrl(md, baseUrl) {
|
|
|
|
return md.replace(/(\]\()([^\s\)]+)(.*?\))/g, (match, before, url, after) => {
|
|
|
|
return before + urlUtils.prependBaseUrl(url, baseUrl) + after;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-05-23 15:25:59 +02:00
|
|
|
extractImageUrls(md) {
|
2018-09-24 21:15:23 +02:00
|
|
|
const markdownIt = new MarkdownIt();
|
2019-05-10 02:06:06 +02:00
|
|
|
setupLinkify(markdownIt); // Necessary to support file:/// links
|
|
|
|
|
2018-09-24 21:15:23 +02:00
|
|
|
const env = {};
|
|
|
|
const tokens = markdownIt.parse(md, env);
|
2018-05-23 15:25:59 +02:00
|
|
|
const output = [];
|
2018-09-24 21:15:23 +02:00
|
|
|
|
|
|
|
const searchUrls = (tokens) => {
|
|
|
|
for (let i = 0; i < tokens.length; i++) {
|
|
|
|
const token = tokens[i];
|
|
|
|
|
|
|
|
if (token.type === 'image') {
|
|
|
|
for (let j = 0; j < token.attrs.length; j++) {
|
|
|
|
const a = token.attrs[j];
|
|
|
|
if (a[0] === 'src' && a.length >= 2 && a[1]) {
|
|
|
|
output.push(a[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (token.children && token.children.length) {
|
|
|
|
searchUrls(token.children);
|
|
|
|
}
|
|
|
|
}
|
2018-05-23 15:25:59 +02:00
|
|
|
}
|
2018-09-24 21:15:23 +02:00
|
|
|
|
|
|
|
searchUrls(tokens);
|
|
|
|
|
2018-05-23 15:25:59 +02:00
|
|
|
return output;
|
|
|
|
},
|
|
|
|
|
2018-06-14 09:52:12 +02:00
|
|
|
olLineNumber(line) {
|
|
|
|
const match = line.match(/^(\d+)\.(\s.*|)$/);
|
|
|
|
return match ? Number(match[1]) : 0;
|
|
|
|
},
|
|
|
|
|
2018-09-28 22:03:28 +02:00
|
|
|
createMarkdownTable(headers, rows) {
|
|
|
|
let output = [];
|
|
|
|
|
|
|
|
const headersMd = [];
|
|
|
|
const lineMd = [];
|
|
|
|
for (let i = 0; i < headers.length; i++) {
|
|
|
|
const mdRow = [];
|
|
|
|
const h = headers[i];
|
|
|
|
headersMd.push(stringPadding(h.label, 3, ' ', stringPadding.RIGHT));
|
|
|
|
lineMd.push('---');
|
|
|
|
}
|
|
|
|
|
|
|
|
output.push(headersMd.join(' | '));
|
|
|
|
output.push(lineMd.join(' | '));
|
|
|
|
|
|
|
|
for (let i = 0; i < rows.length; i++) {
|
|
|
|
const row = rows[i];
|
|
|
|
const rowMd = [];
|
|
|
|
for (let j = 0; j < headers.length; j++) {
|
|
|
|
const h = headers[j];
|
|
|
|
const value = h.filter ? h.filter(row[h.name]) : row[h.name];
|
|
|
|
rowMd.push(stringPadding(value, 3, ' ', stringPadding.RIGHT));
|
|
|
|
}
|
|
|
|
output.push(rowMd.join(' | '));
|
|
|
|
}
|
|
|
|
|
|
|
|
return output.join('\n');
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2017-08-02 19:47:25 +02:00
|
|
|
};
|
|
|
|
|
2018-05-23 13:14:38 +02:00
|
|
|
module.exports = markdownUtils;
|