forked from julianlam/adf-to-md
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
123 lines (93 loc) · 2.54 KB
/
index.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
'use strict';
const Converter = module.exports;
function _convert(node, warnings) {
switch (node.type) {
case 'doc':
return node.content.map(node => _convert(node, warnings)).join('\n\n');
case 'text':
return `${_convertMarks(node, warnings)}`;
case 'paragraph':
return node.content.map(node => _convert(node, warnings)).join('');
case 'hardBreak':
return '\n';
case 'blockquote':
return `> ${node.content.map(node => _convert(node, warnings)).join('\n> ')}`;
case 'bulletList':
case 'orderedList':
return `${node.content.map((subNode) => {
const converted = _convert.call(node, subNode, warnings);
if (node.type === 'orderedList') {
if (!node.attrs) {
node.attrs = {
order: 1,
};
}
node.attrs.order += 1;
}
return converted;
}).join('\n')}`;
case 'listItem': {
const order = this.attrs ? this.attrs.order || 1 : 1;
const symbol = this.type === 'bulletList' ? '*' : `${order}.`;
return ` ${symbol} ${node.content.map(node => _convert(node, warnings).trimEnd()).join(` `)}`;
}
case 'codeBlock': {
const language = node.attrs ? ` ${node.attrs.language}` : '';
return `\`\`\`${language}\n${node.content.map(node => _convert(node, warnings)).join('\n')}\n\`\`\``;
}
default:
console.log('adding warning for', node.type);
warnings.add(node.type);
return '';
}
}
function _convertMarks(node, warnings) {
if (!node.hasOwnProperty('marks') || !Array.isArray(node.marks)) {
return node.text;
}
return node.marks.reduce((converted, mark) => {
switch (mark.type) {
case 'code':
converted = `\`${converted}\``;
break;
case 'em':
converted = `_${converted}_`;
break;
case 'link':
converted = `[${converted}](${mark.attrs.href})`;
break;
case 'strike':
converted = `~${converted}~`;
break;
case 'strong':
converted = `**${converted}**`;
break;
default: // not supported
warnings.add(mark.type);
break;
}
return converted;
}, node.text);
}
Converter.convert = (adf) => {
const warnings = new Set();
Converter.validate(adf);
// todo: do stuff with warnings
return _convert(adf, warnings);
};
Converter.validate = (adf) => {
// Super naive validation -- someday validate against this: https://unpkg.com/@atlaskit/adf-schema@22.0.1/dist/json-schema/v1/full.json
let ok = true;
if (!adf || typeof adf !== 'object') {
ok = false;
}
if (adf.type !== 'doc') {
ok = false;
}
if (adf.version !== 1) {
ok = false;
}
if (!ok) {
throw new Error('adf-validation-failed');
}
};