-
Notifications
You must be signed in to change notification settings - Fork 126
/
preprocess-api-history.ts
247 lines (206 loc) · 6.99 KB
/
preprocess-api-history.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
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
import logger from '@docusaurus/logger';
import Ajv, { JSONSchemaType, ValidateFunction } from 'ajv';
import { readFile, writeFile } from 'fs/promises';
import { fromHtml } from 'hast-util-from-html';
import globby from 'globby';
import type { Html, Root } from 'mdast';
import { fromMarkdown } from 'mdast-util-from-markdown';
import path from 'path';
import type { Node, Literal } from 'unist';
import { visit } from 'unist-util-visit';
import { parse as parseYaml } from 'yaml';
const apiHistoryRegex = /<!--[\s\S]*?(```[\s\S]*?```)[\s\S]*?-->/;
interface ChangeSchema {
'pr-url': string;
'breaking-changes-header'?: string;
description?: string;
}
interface ApiHistory {
added?: ChangeSchema[];
deprecated?: ChangeSchema[];
changes?: ChangeSchema[];
}
interface LiteralString extends Literal {
value: string;
}
let hasWarned = false;
// Copied from here: <https://github.com/electron/website/blob/feat/api-history/scripts/tasks/add-frontmatter.ts#L16-L23>
const getMarkdownFiles = async (startPath: string) => {
const filesPaths = await globby(path.posix.join(startPath, 'api', '/*.md'));
const files: Map<string, string> = new Map();
for (const filePath of filesPaths) {
const content = await readFile(filePath, 'utf-8');
files.set(filePath, content);
}
return files;
};
// Copied from here but slightly different: <https://github.com/electron/lint-roller/blob/bac245478ba69017c2a82e8ba1b2884a37647494/bin/lint-markdown-api-history.ts#L42-L67>
function findPossibleApiHistoryBlocks(tree: Root) {
const codeBlocks: Html[] = [];
visit(
tree,
(node: Node) =>
node.type === 'html' &&
(node as LiteralString).value.includes('```') &&
(node as LiteralString).value.toLowerCase().includes('yaml') &&
(node as LiteralString).value.toLowerCase().includes('history'),
(node) => {
codeBlocks.push(node as Html);
}
);
return codeBlocks;
}
function findValidApiHistoryBlocks(
possibleHistoryBlocks: Html[],
filePath: string,
validateAgainstSchema?: ValidateFunction<ApiHistory>
) {
const validHistoryBlocks: Html[] = [];
// All of the validation logic copied from here: <https://github.com/electron/lint-roller/blob/bac245478ba69017c2a82e8ba1b2884a37647494/bin/lint-markdown-api-history.ts#L117-L176>
for (const possibleHistoryBlock of possibleHistoryBlocks) {
const {
children: [htmlComment],
} = fromHtml(possibleHistoryBlock.value);
if (htmlComment == null) {
hasWarned = true;
logger.warn(
`(Skipping block) Error parsing possible history block (found null/undefined htmlComment) in ${logger.green(
filePath
)}`
);
continue;
}
if (htmlComment.type !== 'comment') {
hasWarned = true;
logger.warn(
`(Skipping block) Possible API History block is not in a HTML comment (${logger.green(
filePath
)})`
);
continue;
}
const {
children: [codeBlock],
} = fromMarkdown(htmlComment.value);
if (codeBlock == null) {
hasWarned = true;
logger.warn(
`(Skipping block) Error parsing possible history block (found null/undefined codeBlock) in ${logger.green(
filePath
)}`
);
continue;
}
if (
codeBlock.type !== 'code' ||
codeBlock.lang?.toLowerCase() !== 'yaml' ||
codeBlock.meta?.trim().toLowerCase() !== 'history'
) {
hasWarned = true;
logger.warn(
`(Skipping block) Error parsing possible history block (codeBlock wasn't code, yaml, or history) in ${logger.green(
filePath
)}`
);
continue;
}
let unsafeHistory = null;
try {
unsafeHistory = parseYaml(codeBlock.value);
} catch (error) {
hasWarned = true;
logger.warn(
`(Skipping block) Error parsing YAML in possible history block (${logger.green(
filePath
)})`
);
continue;
}
if (typeof validateAgainstSchema !== 'undefined') {
const isValid = validateAgainstSchema(unsafeHistory);
if (!isValid) {
hasWarned = true;
logger.warn(
`(Skipping block) Error validating YAML in possible history block (${logger.green(
filePath
)})`
);
continue;
}
}
validHistoryBlocks.push(possibleHistoryBlock);
}
return validHistoryBlocks;
}
export const preprocessApiHistory = async (startPath: string) => {
const schema = path.resolve(startPath, 'api-history.schema.json');
let validateAgainstSchema: ValidateFunction<ApiHistory> | undefined;
try {
const ajv = new Ajv();
const ApiHistorySchemaFile = await readFile(schema, 'utf-8');
const ApiHistorySchema = JSON.parse(
ApiHistorySchemaFile
) as JSONSchemaType<ApiHistory>;
validateAgainstSchema = ajv.compile(ApiHistorySchema);
} catch (error) {
logger.warn(
`Error reading API history schema, continuing without schema validation:\n${error}`
);
}
const files = await getMarkdownFiles(startPath);
for (const [filePath, content] of files) {
if (!content.includes('<!--')) continue;
const tree = fromMarkdown(content);
const possibleHistoryBlocks = findPossibleApiHistoryBlocks(tree as Root);
const validHistoryBlocks = findValidApiHistoryBlocks(
possibleHistoryBlocks,
filePath,
validateAgainstSchema
);
if (validHistoryBlocks.length === 0) continue;
let newContent = content;
let fileLengthDifference = 0;
// Strip HTML comment tags from history blocks
for (const validHistoryBlock of validHistoryBlocks) {
const apiHistoryRegexMatches =
validHistoryBlock.value.match(apiHistoryRegex);
if (apiHistoryRegexMatches?.length !== 2) {
hasWarned = true;
logger.warn(
`(Skipping block) Error extracting the API history block inside HTML comment in ${logger.green(
filePath
)}`
);
continue;
}
const [, historyBlockWithoutTags] = apiHistoryRegexMatches;
if (
validHistoryBlock.position?.start.offset == null ||
validHistoryBlock.position?.end.offset == null
) {
hasWarned = true;
logger.warn(
`(Skipping block) Error getting the start and end position of the API history block in ${logger.green(
filePath
)}`
);
continue;
}
const start = newContent.substring(
0,
validHistoryBlock.position.start.offset + fileLengthDifference
);
const end = newContent.substring(
validHistoryBlock.position.end.offset + fileLengthDifference
);
// Stripping the HTML comment tags of a history block will offset the position of the next history block
fileLengthDifference +=
historyBlockWithoutTags!.length - validHistoryBlock.value.length;
newContent = start + historyBlockWithoutTags + end;
}
await writeFile(filePath, newContent, 'utf-8');
}
if (hasWarned) {
logger.warn('Some API history blocks were skipped due to errors.');
}
};