Ghost/ghost/admin/app/utils/ed-image-manager.js
Kevin Ansfield 02b57750cf Fix editor image perf and upload bug
no issue
- ~10x speedup in processing time taken on each keypress when there are many images/image upload components in the editor
  - edit DOM in memory before changing it in the page to avoid double-render
  - keep upload components around and re-assign them on re-render, adding or removing an image will still re-generate everything
- adds a throttle to the preview rendering so that renders don't get queued up
- fixes occasional bug where uploading an image didn't update the markdown correctly due to a timing issue
2016-05-08 12:55:56 +02:00

41 lines
1.3 KiB
JavaScript

const imageMarkdownRegex = /^!(?:\[([^\n\]]*)\])(?:\(([^\n\]]*)\))?$/gim;
// Process the markdown content and find all of the locations where there is an image markdown block
function parse(stringToParse) {
let images = [];
let m;
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
function getSrcRange(content, index) {
let images = parse(content);
let replacement = {};
if (index > -1) {
// [1] matches the alt text, and 2 matches the url between the ()
// 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 {
getSrcRange
};