mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-24 10:27:10 +02:00
71efff6827
* Update eslint config * Applied linter to lib * Applied eslint config to CliClient/app * Removed prettier due to https://github.com/prettier/prettier/pull/4765 * First pass on test units * Applied linter config to test units * Applied eslint config to clipper * Applied to plugin dir * Applied to root of ElectronClient * Applied on RN root * Applied on CLI root * Applied on Clipper root * Applied config to tools * test hook * test hook * test hook * Added pre-commit hook * Applied rule no-trailing-spaces * Make sure root packages are installed when installing sub-dir * Added doc
92 lines
2.2 KiB
JavaScript
92 lines
2.2 KiB
JavaScript
const stringPadding = require('string-padding');
|
|
const urlUtils = require('lib/urlUtils');
|
|
const MarkdownIt = require('markdown-it');
|
|
const setupLinkify = require('lib/renderers/MdToHtml/setupLinkify');
|
|
|
|
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 = [];
|
|
|
|
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);
|
|
}
|
|
}
|
|
};
|
|
|
|
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');
|
|
},
|
|
};
|
|
|
|
module.exports = markdownUtils;
|