Skip to content

Commit be37255

Browse files
authored
Merge pull request #691 from Peergos/fix/md-viewer-display-images
md-viewer lazily load images
2 parents cb5b569 + aef733c commit be37255

File tree

2 files changed

+54
-20
lines changed

2 files changed

+54
-20
lines changed

assets/apps/markup-viewer/init.js

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var mainWindow;
22
var origin;
33
var editorJS = null;
4-
4+
var markdownContent = "";
55
window.MathJax = {
66
tex: {
77
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
@@ -25,7 +25,19 @@ window.MathJax = {
2525
load: ['[tex]/noerrors']
2626
}
2727
};
28-
28+
function updateImage(id, data, retryAttempt) {
29+
let image = document.getElementById(id);
30+
if (image == null) {
31+
if (retryAttempt < 20) {
32+
setTimeout(() => {updateImage(id, data, retryAttempt + 1)}, 50);
33+
} else {
34+
console.log('unable to set data for image with id:' + id);
35+
}
36+
} else {
37+
let blob = new Blob([data]);
38+
image.src = URL.createObjectURL(blob);
39+
}
40+
}
2941
window.addEventListener('message', function (e) {
3042
let parentDomain = window.location.host.substring(window.location.host.indexOf(".")+1)
3143
if (e.origin !== (window.location.protocol + "//" + parentDomain))
@@ -38,9 +50,7 @@ window.addEventListener('message', function (e) {
3850
if (e.data.action == "ping") {
3951
mainWindow.postMessage({action:'pong'}, e.origin);
4052
} else if(e.data.action == "respondToLoadImage"){
41-
let image = document.getElementById(e.data.id);
42-
let blob = new Blob([e.data.data]);
43-
image.src = URL.createObjectURL(blob);
53+
updateImage(e.data.id, e.data.data, 0);
4454
} else if(e.data.action == "respondToNavigateTo"){
4555
let markdownThemeToUse = e.data.theme != null && e.data.theme == 'dark-mode' ? 'dark' : 'light';
4656
if (e.data.extension == 'note') {
@@ -79,8 +89,7 @@ function initialiseMarkdownEditor(theme, subPathInput, text) {
7989
theme: theme,
8090
subPath: subPath
8191
});
82-
let output = viewer.getHTML();
83-
let xss = DOMPurify.sanitize(output);
92+
let xss = rewriteImages(DOMPurify.sanitize(markdownContent));
8493
let element = document.getElementById('sanitized');
8594
let body = document.getElementById('body-element');
8695
let mdElement = document.getElementById('md-element');
@@ -182,22 +191,46 @@ function updateResourcesInDoc() {
182191
}
183192
});
184193
}
185-
let images = document.getElementsByTagName("img");
186-
for(var i=0; i < images.length;i++) {
187-
let image = images[i];
188-
let isDataImage = image.src.startsWith("data:");
189-
if (!isDataImage) {
190-
const requestedResource = new URL(image.src);
191-
if (window.location.host == requestedResource.host) {
192-
image.src = '#';
193-
let generatedId = 'image-' + uuid();
194-
image.id = generatedId;
195-
mainWindow.postMessage({action:'loadImage', src: requestedResource.pathname, id: generatedId}, origin);
194+
195+
}
196+
function setMarkdownContent(content) {
197+
markdownContent = content;
198+
}
199+
function rewriteImages(html) {
200+
var finished = false;
201+
var startIndex = 0;
202+
while(!finished) {
203+
let imgStartIndex = html.indexOf('<img ', startIndex);
204+
if (imgStartIndex == -1) {
205+
finished = true;
206+
} else {
207+
let imgEndIndex = html.indexOf('">', imgStartIndex + 5);
208+
if (imgEndIndex == -1) {
209+
finished = true;
196210
} else {
197-
console.log('invalid link: ' + image.src);
211+
startIndex = imgStartIndex + 5;
212+
let imgTag = html.substring(imgStartIndex, imgEndIndex);
213+
startIndex = imgEndIndex;
214+
let sourceAttrIndex = imgTag.indexOf('src="');
215+
if (sourceAttrIndex > 0) {
216+
let src = imgTag.substring(sourceAttrIndex + 5);
217+
//console.log('image src=' + src);
218+
let generatedId = 'image-' + uuid();
219+
html = html.substring(0, imgStartIndex + sourceAttrIndex) + ' id="' + generatedId + '" src="#' + html.substring(imgEndIndex);
220+
let tmpImg = new Image();
221+
tmpImg.src = src;
222+
tmpImg.loading="lazy";
223+
const requestedResource = new URL(tmpImg.src);
224+
if (window.location.host == requestedResource.host) {
225+
mainWindow.postMessage({action:'loadImage', src: requestedResource.pathname, id: generatedId}, origin);
226+
} else {
227+
console.log('invalid link: ' + tmpImg.src);
228+
}
229+
}
198230
}
199231
}
200232
}
233+
return html;
201234
}
202235
function initialiseEditorJS(theme, jsonData) {
203236
let setToDarkMode = theme != null && theme == 'dark-mode';

assets/apps/markup-viewer/toastui-editor-viewer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11352,6 +11352,7 @@ function sanitizeHTML(html) {
1135211352
}
1135311353
removeUnnecessaryTags(root);
1135411354
leaveOnlyWhitelistAttribute(root);
11355+
setMarkdownContent(html);
1135511356
return finalizeHtml(root, true);
1135611357
}
1135711358
function removeUnnecessaryTags(html) {
@@ -11915,7 +11916,7 @@ var MarkdownPreview = /** @class */ (function () {
1191511916
var contentEl = this.previewContent;
1191611917
var newHtml = this.eventEmitter.emitReduce('beforePreviewRender', this.sanitizer(nodes.map(function (node) { return _this.renderer.render(node); }).join('')));
1191711918
if (!removedNodeRange) {
11918-
contentEl.insertAdjacentHTML('afterbegin', newHtml);
11919+
//disabled so image urls are not resolved contentEl.insertAdjacentHTML('afterbegin', newHtml);
1191911920
}
1192011921
else {
1192111922
var _a = removedNodeRange.id, startNodeId = _a[0], endNodeId = _a[1];

0 commit comments

Comments
 (0)