1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-06-15 23:00:36 +02:00

Clipper: Fixed importing certain images with sources that contain brackets

This commit is contained in:
Laurent Cozic
2018-09-24 20:15:23 +01:00
parent 312c7f2d27
commit f82dfde6f4
6 changed files with 96 additions and 11 deletions

View File

@ -1,4 +1,5 @@
const urlUtils = require('lib/urlUtils');
const MarkdownIt = require('markdown-it');
const markdownUtils = {
@ -20,15 +21,32 @@ const markdownUtils = {
},
extractImageUrls(md) {
// ![some text](http://path/to/image)
const regex = new RegExp(/!\[.*?\]\(([^\s\)]+).*?\)/, 'g')
let match = regex.exec(md);
const markdownIt = new MarkdownIt();
const env = {};
const tokens = markdownIt.parse(md, env);
const output = [];
while (match) {
const url = match[1];
if (output.indexOf(url) < 0) output.push(url);
match = regex.exec(md);
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;
},