forked from julianlam/adf-to-md
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
162 lines (123 loc) · 3.45 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
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
'use strict';
const Converter = module.exports;
function _convert(node, warnings) {
const content = node.content || [];
switch (node.type) {
case 'doc':
return content.map(node => _convert(node, warnings)).join('\n\n');
case 'text':
return `${_convertMarks(node, warnings)}`;
case 'paragraph':
return content.map(node => _convert(node, warnings)).join('');
case 'heading':
return `${'#'.repeat(node.attrs.level)} ${content.map(node => _convert(node, warnings)).join('')}`;
case 'hardBreak':
return '\n';
case 'inlineCard':
case 'blockCard':
case 'embedCard':
return `[${node.attrs.url}](${node.attrs.url})`;
case 'blockquote':
return `> ${content.map(node => _convert(node, warnings)).join('\n> ')}`;
case 'bulletList':
case 'orderedList':
return `${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} ${content.map(node => _convert(node, warnings).trimEnd()).join(` `)}`;
}
case 'codeBlock': {
const language = node.attrs ? ` ${node.attrs.language}` : '';
return `\`\`\`${language}\n${content.map(node => _convert(node, warnings)).join('\n')}\n\`\`\``;
}
case 'rule':
return '\n\n---\n';
case 'emoji':
return node.attrs.shortName;
case 'table':
return content.map(node => _convert(node, warnings)).join('');
case 'tableRow': {
let output = '|';
let thCount = 0;
output += content.map((subNode) => {
thCount += subNode.type === 'tableHeader' ? 1 : 0;
return _convert(subNode, warnings);
}).join('');
output += thCount ? `\n${'|:-:'.repeat(thCount)}|\n` : '\n';
return output;
}
case 'tableHeader':
return `${content.map(node => _convert(node, warnings)).join('')}|`;
case 'tableCell':
return `${content.map(node => _convert(node, warnings)).join('')}|`;
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 {
result: _convert(adf, warnings),
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');
}
};