-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTranslators.ts
364 lines (317 loc) · 10.6 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
import { Asset, FlatNotepad, Note, Notepad, Section } from './index';
import { format, parse } from 'date-fns';
import { OptionsV2, parseString } from 'xml2js';
import { NoteElement, Source } from './Note';
import { pd } from 'pretty-data';
import { gfm } from 'turndown-plugin-gfm';
import TurndownService from 'turndown';
import { NotepadShell } from './interfaces';
import { decrypt } from './crypto';
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),
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);
let notepad = new Notepad(res.notepad.$.title, { lastModified: res.notepad.$.lastModified });
// Parse sections/notes
if (res.notepad.section) {
res.notepad.section.forEach(s => notepad = notepad.addSection(parseSection(s)));
}
// Parse assets
if (res.notepad.assets) {
((res.notepad.assets[0] || {}).asset || []).forEach(item => {
try {
notepad = notepad.addAsset(new Asset(dataURItoBlob(item._), item.$.uuid));
} catch (e) {
console.warn(`Can't parse the asset ${item.$.uuid}`);
}
});
}
// Convert inline assets to full-assets
const flatNotepad = notepad.flatten();
const convertedAssets: Asset[] = [];
Object.values(flatNotepad.notes)
.forEach(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(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`);
}
}
// Update the note with the new elements
flatNotepad.notes[note.internalRef] = flatNotepad.notes[note.internalRef].clone({
elements
});
});
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 =>
section = section.addNote(new Note(
item.$.title,
item.$.time,
[
...([
'markdown',
'drawing',
'image',
'file',
'recording'
]
.map(type =>
(item[type] || []).map(e => {
return {
type: type,
args: e.$,
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']), 'D MMM h:mmA')}`);
let section = new Section('Imported Notes');
(exported.note || [])
.map(enexNote => {
let note = new Note((enexNote.title || ['Imported Note'])[0], parse(enexNote.created[0]).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(pd.xml(enexNote.content[0]))
}
]);
let fileCount = 0;
let imageCount = 0;
(enexNote.resource || []).map(resource => {
const asset = new Asset(dataURItoBlob(
`data:${resource.mime};base64,${resource.data[0]._.replace(/\r?\n|\r/g, '')}`
));
notepad = notepad.addAsset(asset);
const y = 10 + (200 * (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: '200px',
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);
});
});
}
}
// Thanks to http://stackoverflow.com/a/12300351/998467
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([ab], { type: mimeString });
}
}