-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Copy pathdocsHeaderDecoration.js
92 lines (80 loc) · 2.75 KB
/
docsHeaderDecoration.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
const { getFromPaths, toAttributes } = require("../helpers/objects");
const { isDocsPage, getRegistryEntry } = require("./utils");
const {
isPypiConnector,
getLatestPythonCDKVersion,
parseCDKVersion,
} = require("../connector_registry");
const visit = require("unist-util-visit").visit;
/**
* Convert a boolean to a string
*
* Why? Because MDX doesn't support passing boolean values properly
*/
const boolToBoolString = (bool) => (bool ? "TRUE" : "FALSE");
const plugin = () => {
const transformer = async (ast, vfile) => {
const docsPageInfo = isDocsPage(vfile);
if (!docsPageInfo.isDocsPage) return;
const registryEntry = await getRegistryEntry(vfile);
const latestPythonCdkVersion = await getLatestPythonCDKVersion();
if (!registryEntry) return;
let firstHeading = true;
visit(ast, "heading", (node) => {
if (firstHeading && node.depth === 1 && node.children.length === 1) {
const originalTitle = node.children[0].value;
const originalId = node.data.hProperties.id;
const rawCDKVersion = getFromPaths(
registryEntry,
"packageInfo_[oss|cloud].cdk_version"
);
const syncSuccessRate = getFromPaths(
registryEntry,
"generated_[oss|cloud].metrics.[all|cloud|oss].sync_success_rate"
);
const usageRate = getFromPaths(
registryEntry,
"generated_[oss|cloud].metrics.[all|cloud|oss].usage"
);
const lastUpdated = getFromPaths(
registryEntry,
"generated_[oss|cloud].source_file_info.metadata_last_modified"
);
const sbomUrl = getFromPaths(
registryEntry,
"generated_[oss|cloud].sbomUrl"
)
const { version, isLatest, url } = parseCDKVersion(
rawCDKVersion,
latestPythonCdkVersion
);
const attrDict = {
isOss: registryEntry.is_oss,
isCloud: registryEntry.is_cloud,
isPypiPublished: isPypiConnector(registryEntry),
supportLevel: registryEntry.supportLevel_oss,
dockerImageTag: registryEntry.dockerImageTag_oss,
iconUrl: registryEntry.iconUrl_oss,
github_url: registryEntry.github_url,
issue_url: registryEntry.issue_url,
originalTitle,
originalId,
cdkVersion: version,
isLatestCDKString: boolToBoolString(isLatest),
cdkVersionUrl: url,
syncSuccessRate,
usageRate,
lastUpdated,
sbomUrl,
};
firstHeading = false;
node.children = [];
node.type = "mdxJsxFlowElement";
node.name = "HeaderDecoration";
node.attributes = toAttributes(attrDict);
}
});
};
return transformer;
};
module.exports = plugin;