Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
switchMap((action: Action) => {
try {
Parser.parse(action.payload, ['asciimath']);
} catch (err) {
alert(`Error reading file`);
console.error(err);
throw err;
}
return fromPromise(new Promise(resolve => {
const notepad: INotepad = Parser.notepad;
// Sort out assets
Parser.parseAssets(action.payload, async (assets: IAssets) => {
const notepadAssets = new Set((notepad.notepadAssets || []));
for (let i = 0; i < assets.assets.length; i++) {
if (!notepadAssets.has(assets.assets[i].uuid)) notepadAssets.add(assets.assets[i].uuid);
await ASSET_STORAGE.setItem(assets.assets[i].uuid, assets.assets[i].data);
}
export async function toSyncedNotepad(notepad) {
// Setup access to our binary assets
const ASSET_STORAGE = localforage.createInstance({
name: 'MicroPad',
storeName: 'assets'
});
await ASSET_STORAGE.ready();
const assetHashes = {};
notepad = await Translators.Json.toNotepadFromNotepad(notepad);
const npAssets = Array.from(getUsedAssets(notepad.flatten()));
// Get assets from storage as base64
const base64Assets = await Promise.all((await Promise.all(npAssets.map(uuid => ASSET_STORAGE.getItem(uuid))))
.map((blob) => {
try {
return getAsBase64(blob);
} catch (e) {
return null;
}
}));
// Build up the asset list
base64Assets
.filter(base64 => !!base64 && base64.length > 0)
.map(base64 => md5(base64))
export const uploadNotepad = (username: string, token: string, syncId: string, notepad: ISyncedNotepad, passkey?: string): Observable =>
from(!!notepad.crypto && !!passkey ? encrypt(notepad, passkey) : of(notepad)).pipe(
concatMap(np =>
call<{ assetsToUpload: AssetList }>('upload', syncId, {
notepadV2: JSON.stringify(np, (k, v) => (k === 'parent') ? undefined : v) // Remove parent links here, unneeded content
.replace(
/[\u007f-\uffff]/g,
char => '\\u' + ('0000' + char.charCodeAt(0).toString(16)).slice(-4)
), // Fix unicode encoding
username,
token
})
),
map(res => res.assetsToUpload)
);
return fromPromise(new Promise(resolve => {
const notepad: INotepad = Parser.notepad;
// Sort out assets
Parser.parseAssets(action.payload, async (assets: IAssets) => {
const notepadAssets = new Set((notepad.notepadAssets || []));
for (let i = 0; i < assets.assets.length; i++) {
if (!notepadAssets.has(assets.assets[i].uuid)) notepadAssets.add(assets.assets[i].uuid);
await ASSET_STORAGE.setItem(assets.assets[i].uuid, assets.assets[i].data);
}
notepad.notepadAssets = Array.from(notepadAssets);
resolve(notepad);
});
}));
}),
return fromPromise(new Promise(resolve => {
const notepad: INotepad = Parser.notepad;
// Sort out assets
Parser.parseAssets(action.payload, async (assets: IAssets) => {
const notepadAssets = new Set((notepad.notepadAssets || []));
for (let i = 0; i < assets.assets.length; i++) {
if (!notepadAssets.has(assets.assets[i].uuid)) notepadAssets.add(assets.assets[i].uuid);
await ASSET_STORAGE.setItem(assets.assets[i].uuid, assets.assets[i].data);
}
notepad.notepadAssets = Array.from(notepadAssets);
resolve(notepad);
});
}));
}),
await NOTEPAD_STORAGE.iterate((json: string) => {
let shell: NotepadShell;
try {
shell = JSON.parse(json);
} catch (ignored) {
return;
}
notepads.push(
Translators.Json.toFlatNotepadFromNotepad(shell, passkeys[shell.title])
.catch(e =>
console.warn(`Couldn't parse notepad: ${e}`)
)
);
return;
});
readFileInputEventAsText(event).then(json => {
insertElement({
type: 'markdown',
args: {} as ElementArgs,
content: Translators.Json.toMarkdownFromJupyter(json)
});
}).catch(err => {
console.error(err);
map((json: string) => {
try {
const res = JSON.parse(json);
const notepad: INotepad = Parser.restoreNotepad(res);
notepad.notepadAssets = res.notepadAssets;
return actions.parseNpx.done({
params: '',
result: notepad
});
} catch (err) {
alert(`Error restoring notepad`);
console.error(err);
return actions.parseNpx.failed({
params: '',
error: err
});
}
})
);
private getHtml: () => Promise = async () => {
const whatsNewNote = (await Translators.Xml.toNotepadFromNpx(helpNpx)).sections[0].notes[2];
extension('colour', {
type: 'listener',
listeners: {
'images.after': (event, text: string) =>
text.replace(/c\[([^\]]+)]\(([^)]+)\)/gi, (match, content, colour) =>
`<span style="color: ${colour}">${content}</span>`
)
}
} as any);
const html = new Converter({
parseImgDimensions: true,
simplifiedAutoLink: true,
strikethrough: true,
tables: true,
render() {
Parser.parse(helpNpx, ['asciimath']);
const help: INotepad = Parser.notepad;
const mdGuide = help.sections[1].notes[0];
extension('mock', () => {
let matches: string[] = [];
return [
{
type: 'lang',
regex: /(===([^]+?)===|''([^]+?)''|;;([^]+?);;)/gi,
replace: function(s: string, match: string) {
matches.push('<Maths won\'t display in this view. See the help notepad.><br>' + match);
let n = matches.length - 1;
return '%ph' + n + 'ph%';
}
},
{
type: 'output',