forked from datahub-project/datahub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateDocsDir.ts
651 lines (571 loc) · 19.8 KB
/
generateDocsDir.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
import { execSync } from "child_process";
import matter from "gray-matter";
import * as fs from "fs";
import * as path from "path";
import { Octokit } from "@octokit/rest";
import { throttling } from "@octokit/plugin-throttling";
import { retry } from "@octokit/plugin-retry";
// Note: this must be executed within the docs-website directory.
// Constants.
const HOSTED_SITE_URL = "https://datahubproject.io";
const GITHUB_EDIT_URL =
"https://github.com/datahub-project/datahub/blob/master";
const GITHUB_BROWSE_URL =
"https://github.com/datahub-project/datahub/blob/master";
const OUTPUT_DIRECTORY = "docs";
const STATIC_DIRECTORY = "genStatic/artifacts";
const SIDEBARS_DEF_PATH = "./sidebars.js";
const sidebars = require(SIDEBARS_DEF_PATH);
const sidebars_json = JSON.stringify(sidebars);
const sidebars_text = fs.readFileSync(SIDEBARS_DEF_PATH).toString();
const MyOctokit = Octokit.plugin(retry).plugin(throttling);
const octokit = new MyOctokit({
throttle: {
onRateLimit: (retryAfter: number, options: any) => {
// Retry twice after rate limit is hit.
if (options.request.retryCount <= 2) {
return true;
}
},
onAbuseLimit: () => {
console.warn("GitHub API hit abuse limit");
},
},
});
function actually_in_sidebar(filepath: string): boolean {
const doc_id = get_id(filepath);
return sidebars_json.indexOf(`"${doc_id}"`) >= 0;
}
function accounted_for_in_sidebar(filepath: string): boolean {
if (actually_in_sidebar(filepath)) {
return true;
}
// If we want to explicitly not include a doc in the sidebar but still have it
// available via a direct link, then keeping it commented out in the sidebar
// serves this purpose. To make it work, we search the text of the sidebar JS
// file.
const doc_id = get_id(filepath);
if (sidebars_text.indexOf(`"${doc_id}"`) >= 0) {
return true;
}
return false;
}
function list_markdown_files(): string[] {
let all_markdown_files = execSync("git ls-files --full-name .. | grep '.md$'")
.toString()
.trim()
.split("\n");
let all_generated_markdown_files = execSync(
"cd .. && ls docs/generated/**/**/*.md && ls docs/generated/**/*.md"
)
.toString()
.trim()
.split("\n");
all_markdown_files = [...all_markdown_files, ...all_generated_markdown_files];
if (!process.env.CI) {
// If not in CI, we also include "untracked" files.
const untracked_files = execSync(
"(git ls-files --full-name --others --exclude-standard .. | grep '.md$') || true"
)
.toString()
.trim()
.split("\n")
.filter((filepath) => !all_generated_markdown_files.includes(filepath));
if (untracked_files.length > 0) {
console.log(
`Including untracked files in docs list: [${untracked_files}]`
);
all_markdown_files = [...all_markdown_files, ...untracked_files];
}
// But we should also exclude any files that have been deleted.
const deleted_files = execSync(
"(git ls-files --full-name --deleted --exclude-standard .. | grep '.md$') || true"
)
.toString()
.trim()
.split("\n");
if (deleted_files.length > 0) {
console.log(`Removing deleted files from docs list: [${deleted_files}]`);
all_markdown_files = all_markdown_files.filter(
(filepath) => !deleted_files.includes(filepath)
);
}
}
const filter_patterns = [
// We don't need our issue and pull request templates.
/^\.github\//,
// Ignore everything within this directory.
/^docs-website\//,
// Don't want hosted docs for these.
/^contrib\//,
// Keep main docs for kubernetes, but skip the inner docs.
/^datahub-kubernetes\//,
// Various other docs/directories to ignore.
/^metadata-models\/docs\//, // these are used to generate docs, so we don't want to consider them here
/^metadata-ingestion\/archived\//, // these are archived, so we don't want to consider them here
/^metadata-ingestion\/docs\/sources\//, // these are used to generate docs, so we don't want to consider them here
/^metadata-ingestion\/tests\//,
/^metadata-ingestion-examples\//,
/^docker\/(?!README|datahub-upgrade|airflow\/local_airflow)/, // Drop all but a few docker docs.
/^docs\/docker\/README\.md/, // This one is just a pointer to another file.
/^docs\/README\.md/, // This one is just a pointer to the hosted docs site.
/^\s*$/, //Empty string
];
const markdown_files = all_markdown_files.filter((filepath) => {
return !filter_patterns.some((rule) => rule.test(filepath));
});
return markdown_files;
}
const markdown_files = list_markdown_files();
// for (let markdown_file of markdown_files) {
// console.log("markdown_file", markdown_file);
// }
function get_id(filepath: string): string {
// Removes the file extension (e.g. md).
const id = filepath.replace(/\.[^/.]+$/, "");
// console.log(id);
return id;
}
const hardcoded_slugs = {
"README.md": "/introduction",
};
function get_slug(filepath: string): string {
// The slug is the URL path to the page.
// In the actual site, all slugs are prefixed with /docs.
// There's no need to do this cleanup, but it does make the URLs a bit more aesthetic.
if (filepath in hardcoded_slugs) {
return hardcoded_slugs[filepath as keyof typeof hardcoded_slugs];
}
let slug = get_id(filepath);
if (slug.startsWith("docs/")) {
slug = slug.slice(5);
}
slug = `/${slug}`;
if (slug.endsWith("/README")) {
slug = slug.slice(0, -7);
}
slug = slug.toLowerCase();
return slug;
}
const hardcoded_titles = {
"README.md": "Introduction",
"docs/actions/README.md": "Introduction",
"docs/actions/concepts.md": "Concepts",
"docs/actions/quickstart.md": "Quickstart",
"docs/saas.md": "DataHub Cloud",
};
// titles that have been hardcoded in sidebars.js
// (for cases where doc is reference multiple times with different titles)
const sidebarsjs_hardcoded_titles = [
"metadata-ingestion/README.md",
"metadata-ingestion/source_docs/s3.md",
"docs/api/graphql/overview.md",
];
const hardcoded_hide_title = ["README.md"];
const hardcoded_descriptions = {
// Only applied if title is also overridden.
"README.md":
"DataHub is a data discovery application built on an extensible metadata platform that helps you tame the complexity of diverse data ecosystems.",
};
// FIXME: Eventually, we'd like to fix all of the broken links within these files.
const allowed_broken_links = [
"docs/developers.md",
"docs/how/customize-elasticsearch-query-template.md",
"docs/how/graph-onboarding.md",
"docs/how/search-onboarding.md",
"docs/how/build-metadata-service.md",
];
function markdown_guess_title(
contents: matter.GrayMatterFile<string>,
filepath: string
): void {
if (sidebarsjs_hardcoded_titles.includes(filepath)) {
return;
}
if (contents.data.title) {
contents.data.sidebar_label = contents.data.title;
return;
}
let title: string;
if (filepath in hardcoded_titles) {
title = hardcoded_titles[filepath as keyof typeof hardcoded_titles];
if (filepath in hardcoded_descriptions) {
contents.data.description =
hardcoded_descriptions[filepath as keyof typeof hardcoded_descriptions];
}
if (hardcoded_hide_title.includes(filepath)) {
contents.data.hide_title = true;
}
} else {
// Find first h1 header and use it as the title.
const headers = contents.content.match(/^# (.+)$/gm);
if (!headers) {
throw new Error(
`${filepath} must have at least one h1 header for setting the title`
);
}
if (headers.length > 1 && contents.content.indexOf("```") < 0) {
throw new Error(`too many h1 headers in ${filepath}`);
}
title = headers[0].slice(2).trim();
}
contents.data.title = title;
let sidebar_label = title;
if (sidebar_label.startsWith("DataHub ")) {
sidebar_label = sidebar_label.slice(8).trim();
}
if (sidebar_label.startsWith("About DataHub ")) {
sidebar_label = sidebar_label.slice(14).trim();
}
if (sidebar_label != title) {
contents.data.sidebar_label = sidebar_label;
}
}
function markdown_add_edit_url(
contents: matter.GrayMatterFile<string>,
filepath: string
): void {
const editUrl = `${GITHUB_EDIT_URL}/${filepath}`;
contents.data.custom_edit_url = editUrl;
}
function markdown_add_slug(
contents: matter.GrayMatterFile<string>,
filepath: string
): void {
if (contents.data.slug) {
return;
}
const slug = get_slug(filepath);
contents.data.slug = slug;
}
// function copy_platform_logos(): void {
// execSync("mkdir -p " + OUTPUT_DIRECTORY + "/imgs/platform-logos");
// execSync(
// "cp -r ../datahub-web-react/src/images " +
// OUTPUT_DIRECTORY +
// "/imgs/platform-logos"
// );
// }
function new_url(original: string, filepath: string): string {
if (original.toLowerCase().startsWith(HOSTED_SITE_URL)) {
// For absolute links to the hosted docs site, we transform them into local ones.
// Note that HOSTED_SITE_URL does not have a trailing slash, so after the replacement,
// the url will start with a slash.
return original.replace(HOSTED_SITE_URL, "");
}
if (original.startsWith("http://") || original.startsWith("https://")) {
if (
(original
.toLowerCase()
.startsWith("https://github.com/datahub-project/datahub/blob") ||
original
.toLowerCase()
.startsWith("https://github.com/datahub-project/datahub/tree")) &&
(original.endsWith(".md") || original.endsWith(".pdf"))
) {
throw new Error(`absolute link (${original}) found in ${filepath}`);
}
return original;
}
if (original.startsWith("#")) {
// These are anchor links that reference within the document itself.
return original;
}
// Now we assume this is a local reference.
const suffix = path.extname(original);
if (
suffix == "" ||
[
".java",
".conf",
".xml",
".pdl",
".json",
".py",
".ts",
".yml",
".yaml",
".sh",
".env",
".sql",
// Using startsWith since some URLs will be .ext#LINENO
].some((ext) => suffix.startsWith(ext))
) {
// A reference to a file or directory in the Github repo.
const relation = path.dirname(filepath);
const updated_path = path.normalize(`${relation}/${original}`);
const check_path = updated_path.replace(/#.+$/, "");
if (
!fs.existsSync(`../${check_path}`) &&
actually_in_sidebar(filepath) &&
!allowed_broken_links.includes(filepath)
) {
// Detects when the path is a dangling reference, according to the locally
// checked out repo.
throw new Error(
`broken github repo link to ${updated_path} in ${filepath}`
);
}
const updated_url = `${GITHUB_BROWSE_URL}/${updated_path}`;
return updated_url;
} else if (suffix.startsWith(".md")) {
// Leave as-is.
// We use startsWith above so that we can allow anchor tags on links.
return original;
} else if ([".png", ".svg", ".gif", ".pdf"].includes(suffix)) {
// Let docusaurus bundle these as static assets.
const up_levels = (filepath.match(/\//g) ?? []).length;
const relation = path.dirname(filepath);
const updated = path.normalize(
`${"../".repeat(up_levels + 2)}/${relation}/${original}`
);
//console.log(`Rewriting ${original} ${filepath} as ${updated}`);
return updated;
} else {
throw new Error(`unknown extension - ${original} in ${filepath}`);
}
}
function markdown_rewrite_urls(
contents: matter.GrayMatterFile<string>,
filepath: string
): void {
const new_content = contents.content
.replace(
// Look for the [text](url) syntax. Note that this will also capture images.
//
// We do a little bit of parenthesis matching here to account for parens in URLs.
// See https://stackoverflow.com/a/17759264 for explanation of the second capture group.
/\[(.*?)\]\(((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*)\)/g,
(_, text, url) => {
const updated = new_url(url.trim(), filepath);
return `[${text}](${updated})`;
}
)
.replace(
// Also look for the [text]: url syntax.
/^\[([^^\n\r]+?)\]\s*:\s*(.+?)\s*$/gm,
(_, text, url) => {
const updated = new_url(url, filepath);
return `[${text}]: ${updated}`;
}
);
contents.content = new_content;
}
function markdown_enable_specials(
contents: matter.GrayMatterFile<string>,
filepath: string
): void {
const new_content = contents.content
.replace(/^<!--HOSTED_DOCS_ONLY$/gm, "")
.replace(/^HOSTED_DOCS_ONLY-->$/gm, "");
contents.content = new_content;
}
function markdown_process_inline_directives(
contents: matter.GrayMatterFile<string>,
filepath: string
): void {
const new_content = contents.content.replace(
/^{{\s+inline\s+(\S+)\s+(show_path_as_comment\s+)?\s*}}$/gm,
(_, inline_file_path: string, show_path_as_comment: string) => {
if (!inline_file_path.startsWith("/")) {
throw new Error(`inline path must be absolute: ${inline_file_path}`);
}
// console.log(`Inlining ${inline_file_path} into ${filepath}`);
const referenced_file = fs.readFileSync(
path.join("..", inline_file_path),
"utf8"
);
// TODO: Add support for start_after_line and end_before_line arguments
// that can be used to limit the inlined content to a specific range of lines.
let new_contents = "";
if (show_path_as_comment) {
new_contents += `# Inlined from ${inline_file_path}\n`;
}
new_contents += referenced_file;
return new_contents;
}
);
contents.content = new_content;
}
function markdown_sanitize_and_linkify(content: string): string {
// MDX escaping
content = content.replace(/</g, "<");
// Link to user profiles.
content = content.replace(/@([\w-]+)\b/g, "[@$1](https://github.com/$1)");
// Link to issues/pull requests.
content = content.replace(
/#(\d+)\b/g,
"[#$1](https://github.com/datahub-project/datahub/pull/$1)"
);
// Prettify bare links to PRs.
content = content.replace(
/(\s+)(https:\/\/github\.com\/linkedin\/datahub\/pull\/(\d+))(\s+|$)/g,
"$1[#$3]($2)$4"
);
return content;
}
function pretty_format_date(datetime: string): string {
const d = new Date(Date.parse(datetime));
return d.toISOString().split("T")[0];
}
function make_link_anchor(text: string): string {
return text.replace(/\./g, "-");
}
async function generate_releases_markdown(): Promise<
matter.GrayMatterFile<string>
> {
const contents = matter(`---
title: DataHub Releases
sidebar_label: Releases
slug: /releases
custom_edit_url: https://github.com/datahub-project/datahub/blob/master/docs-website/generateDocsDir.ts
---
# DataHub Releases
## Summary\n\n`);
const releases_list = await octokit.rest.repos.listReleases({
owner: "datahub-project",
repo: "datahub",
});
// We only embed release notes for releases in the last 3 months.
const release_notes_date_cutoff = new Date(
Date.now() - 1000 * 60 * 60 * 24 * 30 * 3
);
// Construct a summary table.
const releaseNoteVersions = new Set();
contents.content += "| Version | Release Date | Links |\n";
contents.content += "| ------- | ------------ | ----- |\n";
for (const release of releases_list.data) {
if (release.prerelease || release.draft) {
continue;
}
const release_date = new Date(Date.parse(release.created_at));
let row = `| **${release.tag_name}** | ${pretty_format_date(
release.created_at
)} |`;
if (release_date > release_notes_date_cutoff) {
row += `[Release Notes](#${make_link_anchor(release.tag_name)}), `;
releaseNoteVersions.add(release.tag_name);
}
row += `[View on GitHub](${release.html_url}) |\n`;
contents.content += row;
}
contents.content += "\n\n";
// Full details
for (const release of releases_list.data) {
let body: string;
if (releaseNoteVersions.has(release.tag_name)) {
body = release.body ?? "";
body = markdown_sanitize_and_linkify(body);
// Redo the heading levels. First we find the min heading level, and then
// adjust the markdown headings so that the min heading is level 3.
const heading_regex = /^(#+)\s/gm;
const max_heading_level = Math.min(
3,
...[...body.matchAll(heading_regex)].map((v) => v[1].length)
);
body = body.replace(
heading_regex,
`${"#".repeat(3 - max_heading_level)}$1 `
);
} else {
// Link to GitHub.
body = `View the [release notes](${release.html_url}) for ${release.name} on GitHub.`;
}
const info = `## [${release.name}](${
release.html_url
}) {#${make_link_anchor(release.tag_name)}}
Released on ${pretty_format_date(release.created_at)} by [@${
release.author.login
}](${release.author.html_url}).
${body}\n\n`;
contents.content += info;
}
return contents;
}
function write_markdown_file(
contents: matter.GrayMatterFile<string>,
output_filepath: string
): void {
const pathname = path.dirname(output_filepath);
fs.mkdirSync(pathname, { recursive: true });
try {
fs.writeFileSync(output_filepath, contents.stringify(""));
} catch (error) {
console.log(`Failed to write file ${output_filepath}`);
console.log(`contents = ${contents}`);
throw error;
}
}
function copy_python_wheels(): void {
// Copy the built wheel files to the static directory.
const wheel_dirs = [
"../metadata-ingestion/dist",
"../metadata-ingestion-modules/airflow-plugin/dist",
"../metadata-ingestion-modules/dagster-plugin/dist",
"../metadata-ingestion-modules/gx-plugin/dist",
];
const wheel_output_directory = path.join(STATIC_DIRECTORY, "wheels");
fs.mkdirSync(wheel_output_directory, { recursive: true });
for (const wheel_dir of wheel_dirs) {
const wheel_files = fs.readdirSync(wheel_dir);
for (const wheel_file of wheel_files) {
const src = path.join(wheel_dir, wheel_file);
const dest = path.join(wheel_output_directory, wheel_file);
// console.log(`Copying artifact ${src} to ${dest}...`);
fs.copyFileSync(src, dest);
}
}
}
(async function main() {
for (const filepath of markdown_files) {
//console.log("Processing:", filepath);
const contents_string = fs.readFileSync(`../${filepath}`).toString();
const contents = matter(contents_string);
markdown_guess_title(contents, filepath);
markdown_add_slug(contents, filepath);
markdown_add_edit_url(contents, filepath);
markdown_rewrite_urls(contents, filepath);
markdown_enable_specials(contents, filepath);
markdown_process_inline_directives(contents, filepath);
//copy_platform_logos();
// console.log(contents);
const out_path = `${OUTPUT_DIRECTORY}/${filepath}`;
write_markdown_file(contents, out_path);
}
// Generate the releases history.
{
const contents = await generate_releases_markdown();
write_markdown_file(contents, `${OUTPUT_DIRECTORY}/releases.md`);
markdown_files.push("releases.md");
}
// Error if a doc is not accounted for in a sidebar.
const autogenerated_sidebar_directories = [
"docs/generated/metamodel",
"docs/generated/ingestion",
"docs/actions/actions",
"docs/actions/events",
"docs/actions/sources",
"docs/actions/guides",
"metadata-ingestion/archived",
"metadata-ingestion/sink_docs",
"docs/what",
"docs/wip",
];
for (const filepath of markdown_files) {
if (
autogenerated_sidebar_directories.some((dir) => filepath.startsWith(dir))
) {
// The sidebars for these directories is automatically generated,
// so we don't need check that they're in the sidebar.
continue;
}
if (!accounted_for_in_sidebar(filepath)) {
throw new Error(
`File not accounted for in sidebar: ${filepath} - try adding it to docs-website/sidebars.js`
);
}
}
// Generate static directory.
copy_python_wheels();
// TODO: copy over the source json schemas + other artifacts.
})();