Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
module.exports.toMobiledoc = (html, options = {}) => {
// 1. sanitize HTML
// @TODO: what sanitisations are needed?
let sanitizedHTML = html;
// 2. Do something vaguely like loadPost
// https://github.com/ErisDS/mobiledoc-kit/blob/master/src/js/editor/editor.js#L193
// 2.a. Parse our HTML and convert to a DOM with same API as browser
let dom = new JSDOM(`${sanitizedHTML}`);
// 2.b. Use Mobiledoc-kit's own DOM Parser to convert the DOM into mobiledoc's internal format
// We use our parser plugins by default, but this is extensible
if (!options.plugins) {
options.plugins = createParserPlugins({
createDocument(html) {
return (new JSDOM(html)).window.document;
}
});
}
let parser = new DOMParser(new Builder(), options);
let post = parser.parse(dom.window.document.body);
// 3. Do something vaguely like serializePost, to render the mobiledoc internal format as mobiledoc
// https://github.com/ErisDS/mobiledoc-kit/blob/master/src/js/editor/editor.js#L567
let mobiledoc = mobiledocRenderer.render(post, '0.3.1');
return mobiledoc;
};
// trigger the willCreateEditor closure action
this.willCreateEditor();
// teardown any old editor that might be around
let editor = this.editor;
if (editor) {
editor.destroy();
}
// create a new editor
let editorOptions = this.editorOptions;
editorOptions.mobiledoc = mobiledoc;
editorOptions.showLinkTooltips = false;
editorOptions.undoDepth = UNDO_DEPTH;
editorOptions.parserPlugins = createParserPlugins();
let componentHooks = {
// triggered when a card section is added to the mobiledoc
[ADD_CARD_HOOK]: ({env, options, payload}, koenigOptions) => {
let cardName = env.name;
let componentName = CARD_COMPONENT_MAP[cardName];
// the payload must be copied to avoid sharing the reference
// `payload.files` is special because it's set by paste/drag-n-drop
// events and can't be copied for security reasons
let {files} = payload;
let payloadCopy = JSON.parse(JSON.stringify(payload || null));
payloadCopy.files = files;
// all of the properties that will be passed through to the
// component cards via the template