Skip to content

Commit aac622c

Browse files
authored
Merge pull request #398 from SolidOS/fix/issue#395
add promise
2 parents ea66187 + d106c9f commit aac622c

1 file changed

Lines changed: 29 additions & 36 deletions

File tree

src/humanReadablePane.js

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const humanReadablePane = {
4545

4646
// This data could come from a fetch OR from ldp container
4747
const hasContentTypeIn2 = function (kb, x, displayables) {
48-
const t = kb.findTypeURIs(x)
48+
const t = kb.findTypeURIs(subject)
4949
for (let k = 0; k < displayables.length; k++) {
5050
if (Util.mediaTypeClass(displayables[k]).uri in t) {
5151
return true
@@ -91,7 +91,7 @@ const humanReadablePane = {
9191
frame.setAttribute('src', URL.createObjectURL(blob));
9292
frame.setAttribute('type', blob.type);
9393
frame.setAttribute('class', 'doc');
94-
frame.setAttribute('style', `border: 1px solid; padding: 1em; height: ${lines}em; width: 100%; max-width: 800px; resize: both; overflow: auto;`);
94+
frame.setAttribute('style', `border: 1px solid; padding: 1em; height: ${lines}em; width: 800px; resize: both; overflow: auto;`);
9595

9696
// Apply sandbox attribute only for HTML files
9797
// @@ Note below - if we set ANY sandbox, then Chrome and Safari won't display it if it is PDF.
@@ -103,47 +103,40 @@ const humanReadablePane = {
103103
}
104104
};
105105

106-
const processMarkdown = (frame, markdownText) => {
107-
const lines = Math.min(30, markdownText.split(/\n/).length + 5);
108-
const res = marked.parse(markdownText);
109-
const clean = DOMPurify.sanitize(res);
110-
frame.innerHTML = clean;
111-
frame.setAttribute('class', 'doc');
112-
frame.setAttribute('style', `border: 1px solid; padding: 1em; height: ${lines}em; width: 100%; max-width: 800px; resize: both; overflow: auto;`);
113-
};
114-
115-
const fetchAndProcessBlob = (kb, subject, frame) => {
116-
kb.fetcher._fetch(subject.uri)
117-
.then(response => response.blob())
118-
.then(blob => {
119-
const lines = blob.type.startsWith('text') ? Math.min(30, blob.text().split(/\n/).length + 5) : 5;
120-
setIframeAttributes(frame, blob, lines);
121-
return blob.type.startsWith('text') ? blob.text() : '';
122-
})
123-
.then(blobText => {
124-
if (blobText) {
125-
const newLines = blobText.includes('<script src="https://dokie.li/scripts/dokieli.js">') ? -10 : 5;
126-
const lines = Math.min(30, blobText.split(/\n/).length + newLines);
127-
frame.setAttribute('style', `border: 1px solid; padding: 1em; height: ${lines}em; width: 100%; max-width: 800px; resize: both; overflow: auto;`);
128-
}
129-
})
130-
.catch(error => {
131-
console.error('Error processing blob:', error);
132-
frame.setAttribute('style', 'border: 1px solid; padding: 1em; height: 5em; width: 100%; max-width: 800px; resize: both; overflow: auto;');
133-
frame.textContent = 'Error loading content';
134-
});
135-
};
136-
137-
if (ct === 'text/markdown') {
106+
// render markdown to html
107+
const markdownHtml = function () {
138108
kb.fetcher.webOperation('GET', subject.uri).then(response => {
139109
const markdownText = response.responseText
140-
processMarkdown(frame, markdownText);
110+
const lines = Math.min(30, markdownText.split(/\n/).length + 5)
111+
const res = marked.parse(markdownText)
112+
const clean = DOMPurify.sanitize(res)
113+
frame.innerHTML = clean
114+
frame.setAttribute('class', 'doc')
115+
frame.setAttribute('style', `border: 1px solid; padding: 1em; height: ${lines}em; width: 800px; resize: both; overflow: auto;`)
141116
}).catch(error => {
142117
console.error('Error fetching markdown content:', error)
143118
frame.innerHTML = '<p>Error loading content</p>'
144119
})
120+
}
121+
122+
if (ct === 'text/markdown') {
123+
markdownHtml()
145124
} else {
146-
fetchAndProcessBlob(kb, subject, frame);
125+
// Fetch and process the blob
126+
kb.fetcher._fetch(subject.uri)
127+
.then(response => response.blob())
128+
.then(blob => {
129+
const blobTextPromise = blob.type.startsWith('text') ? blob.text() : Promise.resolve('')
130+
return blobTextPromise.then(blobText => ({ blob, blobText }))
131+
})
132+
.then(({ blob, blobText }) => {
133+
const newLines = blobText.includes('<script src="https://dokie.li/scripts/dokieli.js">') ? -10 : 5
134+
const lines = Math.min(30, blobText.split(/\n/).length + newLines)
135+
setIframeAttributes(frame, blob, lines)
136+
})
137+
.catch(err => {
138+
console.log('Error fetching or processing blob:', err)
139+
})
147140
}
148141

149142
const tr = myDocument.createElement('TR')

0 commit comments

Comments
 (0)