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-07-16 20:05:47 +02:00
|
|
|
const setupLinkify = require('lib/renderers/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) {
|
2019-07-30 09:35:42 +02:00
|
|
|
// eslint-disable-next-line no-useless-escape
|
2018-05-23 13:14:38 +02:00
|
|
|
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
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
const searchUrls = tokens => {
|
2018-09-24 21:15:23 +02:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-29 15:43:53 +02:00
|
|
|
|
2018-09-24 21:15:23 +02:00
|
|
|
if (token.children && token.children.length) {
|
|
|
|
searchUrls(token.children);
|
|
|
|
}
|
|
|
|
}
|
2019-07-29 15:43:53 +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 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
|
|
|
};
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
module.exports = markdownUtils;
|