2015-10-28 14:36:45 +03:00
|
|
|
const imageMarkdownRegex = /^!(?:\[([^\n\]]*)\])(?:\(([^\n\]]*)\))?$/gim;
|
2015-03-13 12:39:28 +03:00
|
|
|
|
|
|
|
// Process the markdown content and find all of the locations where there is an image markdown block
|
|
|
|
function parse(stringToParse) {
|
2015-10-28 14:36:45 +03:00
|
|
|
let images = [];
|
|
|
|
let m;
|
|
|
|
|
2015-03-13 12:39:28 +03:00
|
|
|
while ((m = imageMarkdownRegex.exec(stringToParse)) !== null) {
|
|
|
|
images.push(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
return images;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out the start and end of the selection range for the src in the markdown, so we know what to replace
|
2016-02-26 16:25:47 +03:00
|
|
|
function getSrcRange(content, index) {
|
2015-10-28 14:36:45 +03:00
|
|
|
let images = parse(content);
|
|
|
|
let replacement = {};
|
2015-03-13 12:39:28 +03:00
|
|
|
|
|
|
|
if (index > -1) {
|
2016-05-08 12:20:26 +03:00
|
|
|
// [1] matches the alt text, and 2 matches the url between the ()
|
2015-03-13 12:39:28 +03:00
|
|
|
// if the () are missing entirely, which is valid, [2] will be undefined and we'll need to treat this case
|
|
|
|
// a little differently
|
|
|
|
if (images[index][2] === undefined) {
|
|
|
|
replacement.needsParens = true;
|
|
|
|
replacement.start = content.indexOf(']', images[index].index) + 1;
|
|
|
|
replacement.end = replacement.start;
|
|
|
|
} else {
|
|
|
|
replacement.start = content.indexOf('(', images[index].index) + 1;
|
|
|
|
replacement.end = replacement.start + images[index][2].length;
|
|
|
|
}
|
|
|
|
return replacement;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
2015-10-28 14:36:45 +03:00
|
|
|
getSrcRange
|
2015-03-13 12:39:28 +03:00
|
|
|
};
|