I’m starting with javascript now.
I’m looking for a tip or guidance as I can’t seem to get any further.
Do I need to save the result of line 70 of console.log(html) in a variable (const, let or var)?
Any tips on doing this? I tried arguments and return in the runRtfjs function, but it didn’t work.
I need to save it in a variable to later display it in HTML.
const jsdom = require("jsdom");
const {JSDOM} = jsdom;
const filertf =
`{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
{\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\sa200\sl276\slmult1\lang9\f0\fs22 This \fs44 is \fs22 a \b simple \ul one \i paragraph \ulnone\b0 document\i0 .\par
}`;
function stringToArrayBuffer(string) {
const buffer = new ArrayBuffer(string.length);
const bufferView = new Uint8Array(buffer);
for (let i = 0; i < string.length; i++) {
bufferView[i] = string.charCodeAt(i);
}
return buffer;
}
function runRtfjs(rtf, callback, errorCallback) {
const virtualConsole = new jsdom.VirtualConsole();
virtualConsole.sendTo(console);
new JSDOM(`
<script src="./node_modules/rtf.js/dist/WMFJS.bundle.js"></script>
<script src="./node_modules/rtf.js/dist/EMFJS.bundle.js"></script>
<script src="./node_modules/rtf.js/dist/RTFJS.bundle.js"></script>
<script>
RTFJS.loggingEnabled(false);
WMFJS.loggingEnabled(false);
EMFJS.loggingEnabled(false);
try {
const doc = new RTFJS.Document(rtfFile);
const meta = doc.metadata();
doc.render().then(function(htmlElements) {
const div = document.createElement("div");
div.append(...htmlElements);
window.done(meta, div.innerHTML);
}).catch(error => window.onerror(error))
} catch (error){
window.onerror(error)
}
</script>
`, {
resources: "usable",
runScripts: "dangerously",
url: "file://" + __dirname + "/",
virtualConsole,
beforeParse(window) {
window.rtfFile = stringToArrayBuffer(rtf);
window.done = function (meta, html) {
callback(meta, html);
};
window.onerror = function (error) {
errorCallback(error);
};
}
});
};
runRtfjs(
filertf,
(meta, html) => {
//console.log("Meta:");
//console.log(meta);
//console.log("Html:");
console.log(html);
},
error => console.error(error)
);
I tried arguments and return in the runRtfjs function, but it didn’t work.
2
Answers
Declare a variable outside the function to hold the result.
Modify the runRtfjs function to set the variable with the HTML content.
Use the variable after the function has executed.
Declear a variable outside the scope of the function and assign the value to it.
If it’s to be re-used in the same scope you can use
let
instead ofvar