generated from discourse/discourse-plugin-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_app_events_docs_markdown.mjs
292 lines (255 loc) · 7.86 KB
/
create_app_events_docs_markdown.mjs
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import "dotenv/config";
import path from "path";
import fs from "fs";
import { markdownTable } from "markdown-table";
const discourseGithubRepoBase =
"https://github.com/discourse/discourse/blob/main";
const emptyValuePlaceholder = "-";
const miscEventGroup = "other events";
function extractEventGroup(eventId) {
if (eventId.startsWith("LIGHTBOX")) {
return "lightbox";
} else if (eventId.startsWith("this.composerEventPrefix")) {
return "composer";
} else if (eventId.split(":").length === 1) {
return miscEventGroup;
}
return eventId.split(":")[0];
}
function createDocumentationForEventGroup(eventGroup, details) {
const markdown = `### ${eventGroup}\n`;
const detailsByEventId = details.reduce((acc, detail) => {
acc[detail.eventId] ||= [];
acc[detail.eventId].push(detail);
return acc;
}, {});
return (
markdown +
Object.entries(detailsByEventId)
.sort(([a], [b]) => a.localeCompare(b))
.map(([eventId, appEvents]) => {
return createDocumentationForAppEvent(eventId, appEvents);
})
.join("\n\n")
);
}
function createDocumentationForAppEvent(eventId, appEventCalls) {
return [createMainBody(appEventCalls), createDetails(appEventCalls)]
.filter((doc) => doc)
.join("\n\n");
}
function createMainBody(appEventCalls) {
const { eventId, filePath, lineNumber, args, description } =
consolidateAppEventCalls(appEventCalls);
const codeUrl = `${discourseGithubRepoBase}${filePath}#L${lineNumber}`;
if (args.length === 0) {
return [
`${"#".repeat(4)} ${eventId} [:link:](${codeUrl})`,
description,
"No arguments passed to this event.",
]
.filter((doc) => doc)
.join("\n\n");
}
const headers = [
"Position",
"Argument",
"Type",
"Always Present",
"Description",
];
const rows = args.reduce((result, arg) => {
const argDesc = arg.description || emptyValuePlaceholder;
if (arg.argType === "object") {
const objArgName = `objectArg${arg.argPosition}`;
const argRow = [
arg.argPosition,
objArgName,
arg.argType,
arg.isAlwaysPresent,
argDesc,
];
const objArgRows = arg.argValue.map((nestedArg) => {
const nestedArgDesc = nestedArg.description || emptyValuePlaceholder;
return [
emptyValuePlaceholder,
`${objArgName}.${nestedArg.key}`,
nestedArg.valueType,
nestedArg.isAlwaysPresent,
nestedArgDesc,
];
});
result.push(argRow, ...objArgRows);
} else {
const argRow = [
arg.argPosition,
arg.argValue,
arg.argType,
arg.isAlwaysPresent,
argDesc,
];
result.push(argRow);
}
return result;
}, []);
const argsTable = markdownTable([headers, ...rows]);
return [
`${"#".repeat(4)} ${eventId} [:link:](${codeUrl})`,
description,
argsTable,
]
.filter((doc) => doc)
.join("\n\n");
}
function consolidateAppEventCalls(appEventCalls) {
const { eventId, filePath, lineNumber, description } = appEventCalls[0];
// TODO: description should be more generic, take from first for now
let consolidatedAppEvent = { eventId, filePath, lineNumber, description };
// use argPosition as key as obj arg have no string argValue
let argCountMap = {};
consolidatedAppEvent.args = appEventCalls.reduce((result, appEvent) => {
appEvent.args.forEach((arg) => {
const position = arg.argPosition;
const existingArg = result[position];
const existingArgLength = existingArg ? existingArg.argValue.length : 0;
if (arg.argValue.length > existingArgLength) {
result[position] = { ...arg };
//TODO: also see if we should be considering descriptions and nestedArgs
}
// Add to count map that will be used to determine isAlwaysPresent later for the arg
argCountMap[position] ||= 0;
argCountMap[position]++;
if (arg.argType === "object") {
arg.argValue.forEach((nestedArg) => {
const key = `objectArg${position}.${nestedArg.key}`;
// argCountMap[key] += 1;
argCountMap[key] ||= 0;
argCountMap[key]++;
});
}
});
return result;
}, []);
consolidatedAppEvent.args.forEach((arg) => {
if (arg.argType === "object") {
arg.argValue.forEach((nestedArg) => {
const key = `objectArg${arg.argPosition}.${nestedArg.key}`;
nestedArg.isAlwaysPresent =
argCountMap[key] === appEventCalls.length ? "True" : "False";
});
}
arg.isAlwaysPresent =
argCountMap[arg.argPosition] === appEventCalls.length ? "True" : "False";
});
return consolidatedAppEvent;
}
function createDetails(appEvents) {
if (appEvents.length <= 1) {
return null;
}
const details = appEvents
.map((e) => createDetailsDocumentation(e))
.join("\n\n");
return [
"<details><summary>Detailed List</summary>",
details,
"</details>",
].join("\n\n");
}
function createDetailsDocumentation(appEvent, headingLevel = 5) {
const { filePath, lineNumber, args, description } = appEvent;
const codeUrl = `${discourseGithubRepoBase}${filePath}#L${lineNumber}`;
if (args.length === 0) {
return [
`${"#".repeat(
headingLevel
)} ${filePath}#${lineNumber} [:link:](${codeUrl})`,
description,
"No arguments passed to this event.",
]
.filter((doc) => doc)
.join("\n\n");
}
const headers = ["Position", "Argument", "Type", "Description"];
const rows = args.reduce((result, arg) => {
const argDesc = arg.description || emptyValuePlaceholder;
if (arg.argType === "object") {
const objArgName = `objectArg${arg.argPosition}`;
const argRow = [arg.argPosition, objArgName, arg.argType, argDesc];
const objArgRows = arg.argValue.map((nestedArg) => {
const nestedArgDesc = nestedArg.description || emptyValuePlaceholder;
return [
emptyValuePlaceholder,
`${objArgName}.${nestedArg.key}`,
nestedArg.valueType,
nestedArgDesc,
];
});
result.push(argRow, ...objArgRows);
} else {
arg.argValue ||= arg.argType; // primarily to handle null
const argRow = [arg.argPosition, arg.argValue, arg.argType, argDesc];
result.push(argRow);
}
return result;
}, []);
const argsTable = markdownTable([headers, ...rows]);
const headingTitle = `${filePath}#${lineNumber}`;
return [
`${"#".repeat(headingLevel)} ${headingTitle} [:link:](${codeUrl})`,
description,
argsTable,
]
.filter((doc) => doc)
.join("\n\n");
}
(async () => {
if (process.argv.length != 2) {
console.log(
"Usage: node create_app_events_docs_markdown.mjs, Remember to define DISCOURSE_CORE in an .env file"
);
process.exit(1);
}
const appEventsDetailsFilePath = path.join(
".",
"lib",
"app_events_docs_generator",
"app_events",
"app_events_details.json"
);
const appEventsDetails = JSON.parse(
fs.readFileSync(appEventsDetailsFilePath, "utf8")
);
// Group appEventsDetails by filePath, so that each file is only updated once
const groupedByEventGroup = appEventsDetails.reduce((result, detail) => {
const key = extractEventGroup(detail.eventId);
result[key] ||= [];
result[key].push(detail);
return result;
}, {});
const docs = Object.keys(groupedByEventGroup)
.sort((a, b) => {
if (a === miscEventGroup) {
return 1;
} else if (b === miscEventGroup) {
return -1;
}
return a.localeCompare(b);
})
.map((eventGroup) => {
return createDocumentationForEventGroup(
eventGroup,
groupedByEventGroup[eventGroup]
);
})
.join("\n\n\n");
// write docs to file
const docsPath = path.join(
".",
"lib",
"app_events_docs_generator",
"app_events",
"app_events_docs.md"
);
fs.writeFileSync(docsPath, docs);
})();