/
index.ts
206 lines (187 loc) · 5.41 KB
/
index.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
import marked from "marked";
import fs from "fs";
import { HTMLElement, parse } from "node-html-parser";
import { DateTime } from "luxon";
export type ChangeLog = {
title: string;
versions: {
version: string | null;
title: string;
date: string | null;
body: string;
changes: { type: CategoryName; body: string }[];
categories: Record<CategoryName, string[]>;
}[];
};
export type CategoryName =
| "New"
| "Changed"
| "Improved"
| "Fixed"
| "Removed"
| "Security"
| string;
export type Options = (
| {
/** Text of the change log. Provide either this or `filePath`. */
text: string;
}
| {
/** Path to change log. This is read synchronously as UTF-8. */
filePath: string;
}
) &
Partial<{
/** If non-null, re-order change categories within each version. */
categorySortOrder: string[] | null;
/** Default title, if the source text doesn't have one. */
defaultTitle: string;
/** If true, recognize e.g. `Changed:` as equivalent to `### Changed`. */
recognizeColonSections: boolean;
/** Omit versions whose titles are equal to "Unreleased", ignoring case. */
omitUnreleasedVersions: boolean;
/** The format of ChangeLog.changes[].body and ChangeLog.categories[key]. */
outputFormat: "html" | "text";
}>;
const defaultOptions: Omit<Required<Options>, "text"> = {
defaultTitle: "Release Notes",
categorySortOrder: [
"New",
"Changed",
"Improved",
"Fixed",
"Removed",
"Security",
],
omitUnreleasedVersions: true,
outputFormat: "html",
recognizeColonSections: true,
};
const dateFormats = [
"yyyy-LL-dd",
"LL-dd-yyyy",
"LL-dd-yy",
"LL/dd/yyyy",
"LL/dd/yy",
];
/** Parse a change log. At least one of `text` and `filePath` must be provided.
*/
export function parseChangeLog(filepathOrOptions: string | Options): ChangeLog {
const options = {
filePath: undefined,
text: undefined,
...defaultOptions,
...(typeof filepathOrOptions === "string"
? { filePath: filepathOrOptions }
: filepathOrOptions),
};
let text = options.filePath
? fs.readFileSync(options.filePath, "utf-8")
: options.text!;
const { categorySortOrder: order } = options;
if (options.recognizeColonSections) {
text = text.replace(
/^(Added|New|Changed|Improved|Fixed|Removed|Security):$/gim,
"### $1"
);
}
const html = marked(text, { headerIds: false, smartypants: true });
const htmlRoot = parse(html);
const title = htmlRoot.querySelector("h1")?.text || options.defaultTitle!;
const versionSectionTitles = findSections("h2", htmlRoot);
let versions = versionSectionTitles.map(
({ header, body }): ChangeLog["versions"][0] => {
const title = header.text.replace(/^\[(.*)\]$/, "$1");
let versionCandidate = title,
version: string | null = null,
date: string | null = null;
const m = title.match(/(.+?)(?:\s+-+\s+|\s*[–—]\s*)(.+)/);
if (m) {
versionCandidate = m[1];
date =
dateFormats
.map((fmt) => DateTime.fromFormat(m[2], fmt))
.find(Boolean)
?.toLocaleString(DateTime.DATE_FULL) || m[2];
}
versionCandidate =
versionCandidate.match(/^\[(.+)\]$/)?.[1] || versionCandidate;
if (versionCandidate.match(/^\d+(\.\d+){0,2}\S*$/))
version = versionCandidate;
// collect and sort changes
let changes = findSections("h3", parse(body)).flatMap(
({ header, body }): { type: CategoryName; body: string }[] => {
const type = header.text;
const ul = parse(body).querySelector("ul");
return ul
? ul
.querySelectorAll("> li")
.map((li) => ({ type, body: getHTML(li) }))
: [];
}
);
if (order) {
changes = changes.sort(
({ type: k1 }, { type: k2 }) => order.indexOf(k1) - order.indexOf(k2)
);
}
// collect changes into categories
const entries = changes.map(
({
type,
body,
}: {
type: CategoryName;
body: string;
}): [CategoryName, string] => [type, body]
);
const categories = collect(entries);
return { title, version, date, body, changes, categories };
}
);
if (options.omitUnreleasedVersions) {
versions = versions.filter(
(version) => version.title && !/^unreleased$/i.test(version.title)
);
}
return { title, versions };
function getHTML(elt: HTMLElement): string {
switch (options.outputFormat) {
case "html":
return elt.innerHTML;
case "text":
return elt.text;
}
}
}
function collect<K extends string | number | symbol, V>(
pairs: [K, V][]
): Record<K, V[]> {
const result: Record<K, V[]> = {} as Record<K, V[]>;
for (const [key, value] of pairs) {
if (!result[key]) result[key] = [];
result[key].push(value);
}
return result;
}
function findSections(
selector: string,
parent: HTMLElement
): { header: HTMLElement; body: string }[] {
const headers = parent.querySelectorAll(selector);
return headers.map((header, i) => {
const nextHeader = headers[i + 1];
const body = [];
for (
let elt = header.nextElementSibling;
elt && elt !== nextHeader;
elt = elt.nextElementSibling
) {
body.push(elt);
}
return {
header,
body: body.map((node) => node.outerHTML).join("\n"),
};
});
}