1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-27 10:32:58 +02:00
joplin/ReactNativeClient/lib/markdownUtils.js

92 lines
2.2 KiB
JavaScript
Raw Normal View History

2018-09-29 14:15:36 +02:00
const stringPadding = require('string-padding');
const urlUtils = require('lib/urlUtils');
const MarkdownIt = require('markdown-it');
const setupLinkify = require('lib/renderers/MdToHtml/setupLinkify');
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;
},
prependBaseUrl(md, baseUrl) {
// eslint-disable-next-line no-useless-escape
return md.replace(/(\]\()([^\s\)]+)(.*?\))/g, (match, before, url, after) => {
return before + urlUtils.prependBaseUrl(url, baseUrl) + after;
});
},
extractImageUrls(md) {
const markdownIt = new MarkdownIt();
setupLinkify(markdownIt); // Necessary to support file:/// links
const env = {};
const tokens = markdownIt.parse(md, env);
const output = [];
2019-07-29 15:43:53 +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]);
}
}
}
2019-07-29 15:43:53 +02:00
if (token.children && token.children.length) {
searchUrls(token.children);
}
}
2019-07-29 15:43:53 +02:00
};
searchUrls(tokens);
return output;
},
olLineNumber(line) {
const match = line.match(/^(\d+)\.(\s.*|)$/);
return match ? Number(match[1]) : 0;
},
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;