-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTranslators.ts
412 lines (356 loc) · 12.3 KB
/
Translators.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import { Asset, FlatNotepad, Note, Notepad, Section } from './index';
import { format, parse, parseISO } from 'date-fns';
import { OptionsV2, parseString } from 'xml2js';
import { NoteElement, Source } from './Note';
import { gfm } from 'turndown-plugin-gfm';
import TurndownService from 'turndown';
import { NotepadShell } from './interfaces';
import { decrypt } from './crypto';
import {
ENEX_FORMAT,
IMPORTED_EVERNOTE_NOTEBOOK_TIME,
IMPORTED_MARKDOWN_NOTE_TIME,
LAST_MODIFIED_FORMAT
} from './date-formats';
export namespace Translators {
export namespace Json {
/**
* @param {string | object} json A {@link Notepad} object in JSON format or as a plain object
* @param {string | undefined} passkey The passkey to decrypt the notepad if it's encrypted.
* @returns {Notepad}
*/
export async function toNotepadFromNotepad(json: string | object, passkey?: string): Promise<Notepad> {
const jsonObj: NotepadShell = (typeof json === 'string') ? JSON.parse(json) : json;
let notepad = new Notepad(jsonObj.title, {
lastModified: parse(jsonObj.lastModified, LAST_MODIFIED_FORMAT, new Date()),
notepadAssets: jsonObj.notepadAssets || [],
crypto: jsonObj.crypto
});
if (typeof jsonObj.sections === 'string') {
if (!passkey) throw new Error('This notepad is encrypted. A passkey is needed to unlock it.');
return await decrypt(jsonObj, passkey);
}
// Restore sections
jsonObj.sections.forEach(section => notepad = notepad.addSection(restoreSection(section)));
return notepad;
function restoreSection(section: Section): Section {
let restored = new Section(section.title).clone({ internalRef: section.internalRef });
section.sections.forEach(s => restored = restored.addSection(restoreSection(s)));
section.notes.forEach(n => restored = restored.addNote(restoreNote(n)));
return restored;
}
function restoreNote(note: Note) {
return new Note(note.title, note.time, note.elements, note.bibliography, note.internalRef);
}
}
/**
* @param {string | object} json A {@link Notepad} object in JSON format or as a plain object
* @param {string | undefined} passkey The passkey to decrypt the notepad if it's encrypted.
* @returns {FlatNotepad}
*/
export async function toFlatNotepadFromNotepad(json: string | object, passkey?: string): Promise<FlatNotepad> {
return (await toNotepadFromNotepad(json, passkey)).flatten();
}
/**
* @param {string} json A Jupyter notebook (.ipynb)
* @returns {string} A Markdown translation of that notebook
*/
export function toMarkdownFromJupyter(json: string): string {
const np = JSON.parse(json);
let mdString = '';
np.cells.forEach(cell => {
if (cell.cell_type === 'markdown') cell.source.forEach(line => mdString += line+'\n');
if (cell.cell_type === 'code') {
mdString += '\n```\n';
cell.source.forEach(line => mdString += line+'\n');
cell.outputs.forEach(output => {
if (!output.text) return;
mdString += '\n--------------------\n';
mdString += 'Output:\n';
output.text.forEach(t => mdString += t);
mdString += '\n--------------------\n';
});
mdString += '```\n';
}
});
return mdString;
}
}
export namespace Xml {
/**
* @param {string} xml The NPX file's contents as a string
* @returns {Promise<Notepad>}
*/
export async function toNotepadFromNpx(xml: string): Promise<Notepad> {
const res = await parseXml(xml);
const lastModifiedStr: string = res.notepad.$.lastModified;
let notepad = new Notepad(res.notepad.$.title, { lastModified: lastModifiedStr ? parse(lastModifiedStr, LAST_MODIFIED_FORMAT, new Date()) : new Date() });
// Parse sections/notes
if (res.notepad.section) {
res.notepad.section.forEach(s => notepad = notepad.addSection(parseSection(s)));
}
// Parse assets
if (res.notepad.assets) {
const assets = await Promise.all<Asset | null>(((res.notepad.assets[0] || {}).asset || []).map(async item => {
try {
return new Asset(await dataURItoBlob(item._), item.$.uuid);
} catch (e) {
console.warn(`Can't parse the asset ${item.$.uuid}\n${e}`);
return null;
}
}));
assets
.filter(asset => asset !== null)
.forEach(asset => notepad = notepad.addAsset(asset!));
}
// Convert inline assets to full-assets
const flatNotepad = notepad.flatten();
const convertedAssets: Asset[] = [];
const convertedNotes = await Promise.all(Object.values(flatNotepad.notes)
.map(async note => {
let elements: NoteElement[] = [];
// Import inline assets
for (const element of note.elements) {
// If it's not a binary asset with inline base64, skip
if (element.type === 'markdown' || element.content === 'AS') {
elements.push(element);
continue;
}
try {
const asset = new Asset(await dataURItoBlob(element.content));
elements.push({
...element,
content: 'AS',
args: {
...element.args,
ext: asset.uuid
}
});
convertedAssets.push(asset);
} catch (e) {
console.warn(`Can't parse asset\n${e}`);
}
}
// Update the note with the new elements
return flatNotepad.notes[note.internalRef].clone({
elements
});
}));
convertedNotes.forEach(note => flatNotepad.notes[note.internalRef] = note);
return flatNotepad.toNotepad().clone({
assets: [
...notepad.assets,
...convertedAssets
],
notepadAssets: [
...notepad.notepadAssets,
...convertedAssets.map(asset => asset.uuid)
]
});
function parseSection(sectionObj: any): Section {
let section = new Section(sectionObj.$.title);
// Insert sub-sections recursively because notepads are trees
(sectionObj.section || []).forEach(item => section = section.addSection(parseSection(item)));
// Parse notes
(sectionObj.note || []).forEach(item => {
const posixTimeStr: string = item.$.time;
section = section.addNote(new Note(
item.$.title,
parseISO(posixTimeStr).getTime(),
[
...([
'markdown',
'drawing',
'image',
'file',
'recording',
'pdf'
]
.map(type =>
(item[type] || []).map(e => {
return {
type: type,
args: {
...e.$,
canOptimise: e.$.canOptimise && (e.$.canOptimise.toLowerCase() == 'true')
},
content: e._ || ''
} as NoteElement;
})
)
).reduce((acc: NoteElement[], element: NoteElement[]) => acc.concat(element))
],
[
...(item.bibliography[0].source || []).map(s => {
return {
id: s.$.id,
item: s.$.item,
content: s._
} as Source;
})
]
))
});
return section;
}
}
/**
* @param {string} xml The exported Evernote notepad's XML as a string
* @returns {Promise<Notepad>}
*/
export async function toNotepadFromEnex(xml: string): Promise<Notepad> {
const res = await parseXml(xml, { trim: true, normalize: false });
const exported = res['en-export'];
let notepad = new Notepad(`${exported.$.application} Import ${format(parse(exported.$['export-date'], ENEX_FORMAT, new Date()), IMPORTED_EVERNOTE_NOTEBOOK_TIME)}`);
let section = new Section('Imported Notes');
(await Promise.all<Note>((exported.note || [])
.map(async enexNote => {
let note = new Note((enexNote.title || ['Imported Note'])[0], parse(enexNote.created[0], ENEX_FORMAT, new Date()).getTime(), [
// Add the general note content (text/to-do)
{
type: 'markdown',
args: {
id: 'markdown1',
x: '10px',
y: '10px',
width: '600px',
height: 'auto',
fontSize: '16px'
},
content: enmlToMarkdown(enexNote.content[0])
}
]);
let fileCount = 0;
let imageCount = 0;
await Promise.all(
(enexNote.resource || []).reverse().map(async resource => {
const asset = new Asset(await dataURItoBlob(
`data:${resource.mime};base64,${resource.data[0]._.replace(/\r?\n|\r/g, '')}`
));
notepad = notepad.addAsset(asset);
const y = 10 + (335 * (fileCount + imageCount));
if (resource.mime[0].includes('image')) {
note = note.addElement({
type: 'image',
args: {
id: `image${++imageCount}`,
x: '650px',
y: y + 'px',
width: 'auto',
height: '300px',
ext: asset.uuid
},
content: 'AS'
});
} else {
// Add the resource as a file
let filename;
try {
if (resource['resource-attributes'][0]['file-name']) {
filename = resource['resource-attributes'][0]['file-name'][0];
} else if (resource['resource-attributes'][0]['source-url']) {
filename = resource['resource-attributes'][0]['source-url'][0].split('/').pop();
} else {
filename = `file${fileCount}.${resource.mime[0].split('/').pop()}`
}
} catch (e) {
filename = 'imported-file' + fileCount;
}
note = note.addElement({
type: 'file',
args: {
id: `file${++fileCount}`,
x: '650px',
y: y + 'px',
width: 'auto',
height: 'auto',
ext: asset.uuid,
filename
},
content: 'AS'
});
}
})
);
return note;
})))
.forEach(note => section = section.addNote(note));
return notepad.addSection(section);
function enmlToMarkdown(enml: string): string {
// Init Turndown service
const service = new TurndownService();
service.use(gfm);
// Setup rules, these are like converters from the old to-markdown lib
service.addRule('en-media', {
filter: 'en-media',
replacement: () => {
return '`there was an attachment here`';
}
});
service.addRule('crypt', {
filter: 'en-crypt',
replacement: () => '`there was encrypted text here`'
});
service.addRule('todo', {
filter: 'en-todo',
replacement: (content, node) =>
`- [${(node.getAttributeNode('checked') && node.getAttributeNode('checked').value === 'true') ? 'x' : ' '}] ${content}`
});
// Clean input
let lines = enml.split('\n');
lines = lines.slice(3, lines.length - 1);
const html = lines
.map(line => line.trim())
.join('\n')
.replace(/^<en-media.*\/>$/gmi, tag => `${tag.substr(0, tag.length - 2)}>t</en-media>`);
// Convert to markdown
return service.turndown(html);
}
}
function parseXml(xml: string, opts: OptionsV2 = {}): Promise<any> {
return new Promise<any>((resolve, reject) => {
parseString(xml, { trim: true, ...opts }, (err, res) => {
if (err) reject(err);
resolve(res);
});
});
}
}
export namespace Markdown {
/**
* @param {Array<MarkdownImport>} markdown A list of all the markdown to be imported into notes
*/
export function toNotepadFromMarkdown(markdown: MarkdownImport[]): Notepad {
let importCounter = 0;
const section = markdown
.map(({ title, content }) => new Note(`${title} (Import ${++importCounter})`).clone({
elements: [
{
type: 'markdown',
content,
args: { id: 'markdown1', x: '10px', y: '10px', width: '550px', height: 'auto', fontSize: '16px' }
}
]
}))
.reduce((section, note) => section.addNote(note), new Section('Imported Notes'));
return new Notepad('Markdown Import ' + format(new Date(), IMPORTED_MARKDOWN_NOTE_TIME)).addSection(section);
}
export type MarkdownImport = { title: string, content: string };
}
// Thanks to http://stackoverflow.com/a/12300351/998467
async function dataURItoBlob(dataURI: string) {
// convert base64 to raw binary data held in a string
// doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
let byteString = atob(dataURI.split(',')[1]);
// separate out the mime component
let mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
// write the bytes of the string to an ArrayBuffer
let ab = new ArrayBuffer(byteString.length);
let ia = new Uint8Array(ab);
for (let i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
// write the ArrayBuffer to a blob, and you're done
return new Blob([ia], { type: mimeString });
}
}