-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert-legacy.js
269 lines (232 loc) · 7.05 KB
/
convert-legacy.js
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
const input = require('./structure-editor-literature-review.nm.json')
const remark = require('../nm/node_modules/remark')
const map = {}
const treeToMap = (parent, depth) => node => {
map[node.id] = node;
node.parent = parent
// if (depth > 0) {
// node.children = []
// }
node.children.forEach(treeToMap(node.id, depth + 1))
}
treeToMap(input.root.id, 0)(input.root)
// TODO parse markdown yall
const processor = remark()
// .use(remarkReact)
.use({settings: {breaks: true}})
const singleLineProcessor = remark()
// .use(remarkReact)
.use({settings: {breaks: true}})
.use(disable, {block: ['list', 'atxHeading', 'setextHeading']})
function disable(options) {
var proto = this.Parser.prototype;
ignore('blockMethods', options.block);
ignore('inlineMethods', options.inline);
function ignore(key, list) {
var values = proto[key];
var index = -1;
var length = list && list.length;
var pos;
while (++index < length) {
pos = values.indexOf(list[index]);
if (pos !== -1) {
values.splice(pos, 1);
}
}
}
}
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
const emojis = {
':?:': '❓',
':!:': '❗️',
':+1:': '👍',
':-1:': '👎',
':f:': '🔥',
':100:': '💯',
':t:': '💭',
':":': '❝',
':i:': '💡',
':?!:': '🗣',
':)': '🙂',
':P': '😛',
':D': '😀',
':/': '😕',
':(': '🙁',
':|': '😐',
':umok:': '🙃',
';)': '😉',
'>.<': '😣',
':p:': '🎉',
}
const emojiRx = new RegExp(Object.keys(emojis).map(k => emojis[k]).join('|'), 'g')
// TODO use these
const emoji_names = {
party: '🎉',
tada: '🎉',
}
const emojiRegexes = Object.keys(emojis)
.map(k => [new RegExp('(\\s|^)' + escapeRegExp(k) + '\\s', 'g'), emojis[k]]);
const replaceReduce = (text, [rx, emo]) => text.replace(rx, t => (t[0] === ' ' ? t[0] : '') + emo + t.slice(-1))
const replaceEmojis = text => {
return emojiRegexes.reduce(replaceReduce, text)
}
const enlargeEmojis = text => {
return text.replace(emojiRx, x => '<span class="inline-emoji">' + x + '</span>')
}
// enlargeEmojis(marked(
// replaceEmojis(this.props.content + '')
// ))
const withAttribute = (name, value) => delta => delta.insert === '\n' ? delta : ({...delta, attributes: {...delta.attributes, [name]: value}});
const withBlockAttribute = (name, value) => delta => delta.insert !== '\n' ? delta : ({...delta, attributes: {...delta.attributes, [name]: value}});
const converters = {
html: node => [{insert: node.value}],
text: node => [{insert: node.value}],
break: node => [{insert: '\n'}],
root: node => convertNodes(node.children),
listItem: node => convertNodes(node.children),
paragraph: node => convertNodes(node.children).concat([{insert: '\n'}]),
// Inline formatting
link: node => convertNodes(node.children).map(withAttribute('link', node.url)),
// TODO maybe fix this or something
linkReference: node => convertNodes(node.children),
strong: node => convertNodes(node.children).map(withAttribute('bold', true)),
emphasis: node => convertNodes(node.children).map(withAttribute('emphasis', true)),
inlineCode: node => [{insert: node.value, attributes: {code: true}}],
// Block formatting
blockquote: node => convertNodes(node.children).map(withBlockAttribute('blockquote', true)),
list: node => convertNodes(node.children).map(withBlockAttribute('list', node.ordered ? 'ordered' : 'bullet')),
code: node => flatten(node.value.split('\n').map(line => [{insert: line}, {insert: '\n', attributes: {'code-block': true}}])),
}
const flatten = lists => [].concat(...lists)
const convertNodes = nodes => flatten(nodes.map(convertMdAst))
const convertMdAst = node => {
if (!converters[node.type]) {
console.log(JSON.stringify(node, null, 2))
fail
}
return converters[node.type](node)
};
const convertMarkdown = text => {
const ast = (text.indexOf('\n') === -1 ? singleLineProcessor : processor).parse(text);
return convertMdAst(ast)
};
const convertContents = node => {
switch (node.type) {
case 'code-playground':
return [].concat(...node.content.split('\n').map(line => line ? [
{insert: line},
{insert: '\n', attributes: {'code-block': 'plain'}}
] : [
{insert: '\n', attributes: {'code-block': 'plain'}}
])).reduce((lines, line) => {
if (lines.length > 0 &&
line.insert === '\n' &&
[...lines[lines.length - 1].insert].every(c => c === '\n')
) {
lines[lines.length - 1].insert += '\n'
} else {
lines.push(line)
}
return lines
}, [])
case 'image':
return [
{
"insert":{"image":node.imageSrc},
"attributes": node.minimized ? {"height":"100"} : undefined
},
{"insert": node.content + "\n"}
]
case undefined:
case 'base':
case 'normal':
case 'ordered_list':
case 'list':
case 'todo':
return convertMarkdown(replaceEmojis(node.content + ''))
case 'symlink':
return [{insert: {symlink: node.content}}, {insert: '\n'}]
case 'table':
return node.table.matrix.map(row => ({insert: row.join('|') + '\n'}))
default:
const contents = JSON.stringify({...node, children: undefined})
console.log(contents)
fail
return [{insert: contents + '\n'}]
}
};
const knownKeys = [
"id",
"parent",
"created",
"done",
"modified",
"children",
"source",
"imageSrc",
"collapsed",
"type",
"minimized",
"content",
"tags",
// TODO use
"disabled",
"display_collapsed",
"table",
"editor_collapsed",
]
const oldNodeToNewNode = (node, getTag) => {
Object.keys(node).forEach(k => {
if (!knownKeys.includes(k)) {
console.log('unknown key', k)
}
})
return {
"id": node.id,
"parent": node.parent,
"author": "114634699742006379805",
"created": node.created || Date.now(),
"completed": !!(node.done || node.disabled),
"trashed": null,
"modified": node.modified || Date.now(),
"childrenModified": node.modified,
"children": node.children.map(node => node.id),
"numberChildren": node.type === 'ordered_list',
"contents": {"ops": (node.source ? [
{insert: {source: node.source}},
] : []).concat(convertContents(node))},
"tags": node.tags ? node.tags.map(getTag) : [],
"prefix": node.type === 'todo' ? [
"Todo"
] : null,
"$schemaVersion": 1
}
}
const _newId = () => Math.random().toString(36).slice(2);
const newId = () => _newId() + _newId() + _newId();
const output = {
root: input.root.id,
nodes: {},
tags: {},
}
const tagsByTitle = {}
const mapTag = name => {
if (!tagsByTitle[name]) {
const id = newId();
tagsByTitle[name] = id
output.tags[id] = {
name,
created: Date.now(),
modified: Date.now(),
color: '#afa',
id,
"$schemaVersion": 1
}
}
return tagsByTitle[name]
}
Object.values(map).forEach(node => output.nodes[node.id] = oldNodeToNewNode(node, mapTag))
const fs = require('fs')
fs.writeFileSync('./structured.dump', JSON.stringify(output, null, 2))