-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
131 lines (109 loc) · 3.37 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
const removePosition = require('unist-util-remove-position');
const { select } = require('unist-util-select');
const { createRoot, createPreface, createRelease, createAction, createGroup, createChange } = require('@geut/chast');
function remarkToChan() {
return tree => {
const newTree = removePosition(tree, true);
return createRoot([parsePreface(newTree), ...parseReleases(newTree)]);
};
}
function parsePreface(tree) {
const value = tree.children.slice(0, 3);
if (value.length !== 3) {
return null;
}
return createPreface(tree.children.slice(0, 3));
}
function parseReleases(tree) {
const definitions = tree.children.filter(node => node.type === 'definition');
const releases = tree.children.filter(node => ['heading', 'list'].includes(node.type) && node.depth !== 1);
const headingReleases = releases.filter(node => node.type === 'heading' && node.depth === 2);
return headingReleases.map(node => {
const fromIdx = releases.indexOf(node);
const nextNode = headingReleases[headingReleases.indexOf(node) + 1];
const endIdx = nextNode ? releases.indexOf(nextNode) : undefined;
// the changes on the keepachangelog are next to the action group as a list type
const actions = releases
.slice(fromIdx + 1, endIdx)
.reduce((result, next) => {
if (next.type === 'heading') {
result.push(next);
return result;
}
result[result.length - 1].changes = next;
return result;
}, [])
.filter(action => action.changes);
const props = parseHeadingRelease(node, definitions);
return createRelease(props, actions.map(action => parseAction(action)));
});
}
function parseHeadingRelease(heading, definitions) {
let link = select(':root > linkReference', heading);
let text = select(':root > text', heading);
let unreleased = link && link.identifier === 'unreleased' ? true : null;
if (!link) {
// first release
const [version, date] = text.value.split(' - ');
return {
identifier: version,
version,
date
};
}
if (link.identifier === 'yanked') {
const [version, date] = text.value.trim().split(' - ');
return {
identifier: version,
version,
date,
yanked: true
};
}
const definition = definitions.find(def => def.identifier === link.identifier);
return {
identifier: link.identifier,
version: link.label,
url: definition ? definition.url : null,
date: unreleased
? null
: text.value
.trim()
.replace('- ', '')
.trim(),
unreleased
};
}
function parseAction(action) {
const { changes, children } = action;
const name = children[0].value;
return createAction({ name }, changes && parseChanges(changes.children));
}
function parseChanges(changes) {
return changes.map(change => {
const groupList = select(':root > list', change);
if (groupList) {
const name = select(':first-child > text', change).value;
return createGroup({ name }, groupList.children.map(change => createChange(change.children)));
}
return createChange(change.children);
});
}
module.exports = remarkToChan;
/**
* root {
* preface {}
* release [version, date, link?] {
* action [name=(ADDED, CHANGED, REMOVED)] {
* group {
* change {
*
* }
* }
* change {
*
* }
* }
* }
* }
**/