This repository was archived by the owner on Sep 9, 2022. It is now read-only.
forked from github/docs
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
122 lines (109 loc) · 4.01 KB
/
Copy pathindex.js
File metadata and controls
122 lines (109 loc) · 4.01 KB
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
import { fileURLToPath } from 'url'
import fs from 'fs'
import path from 'path'
import { readCompressedJsonFileFallback } from '../read-json-file.js'
import { getAutomatedPageMiniTocItems } from '../get-mini-toc-items.js'
import { allVersions } from '../all-versions.js'
import languages from '../languages.js'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const schemasPath = path.join(__dirname, 'static/decorated')
const ENABLED_APPS_FILENAME = path.join(__dirname, 'static/apps/enabled-for-apps.json')
const contentPath = path.join(process.cwd(), 'content/rest')
/*
Loads the schemas from the static/decorated folder into a single
object organized by version.
Example:
{
[version]: {
category: {
subcategory: [operations],
}
}
}
*/
const restOperationData = new Map()
Object.keys(languages).forEach((language) => {
restOperationData.set(language, new Map())
Object.keys(allVersions).forEach((version) => {
// setting to undefined will allow us to perform checks
// more easily later on
restOperationData.get(language).set(version, new Map())
})
})
export const categoriesWithoutSubcategories = fs
.readdirSync(contentPath)
.filter((file) => {
return file.endsWith('.md') && !file.includes('index.md') && !file.includes('README.md')
})
.map((filteredFile) => filteredFile.replace('.md', ''))
const restOperations = new Map()
export default async function getRest(version, category, subCategory) {
const openApiVersion = getOpenApiVersion(version)
if (!restOperations.has(openApiVersion)) {
const filename = `${openApiVersion}.json`
// The `readCompressedJsonFileFallback()` function
// will check for both a .br and .json extension.
restOperations.set(
openApiVersion,
readCompressedJsonFileFallback(path.join(schemasPath, filename))
)
}
if (subCategory) {
return restOperations.get(openApiVersion)[category][subCategory]
} else if (category) {
return restOperations.get(openApiVersion)[category]
} else {
return restOperations.get(openApiVersion)
}
}
// Right now there is not a 1:1 mapping of openapi to docs versions,
// so we need to return an array of versions. The enterprise-cloud
// version doesn't yet exist in openapi so we map it to free-pro-team.
function getDocsVersions(openApiVersion) {
const versions = Object.values(allVersions)
.filter((version) => version.openApiVersionName === openApiVersion)
.map((item) => item.version)
return versions
}
function getOpenApiVersion(version) {
if (!(version in allVersions)) {
throw new Error(`Unrecognized version '${version}'. Not found in ${Object.keys(allVersions)}`)
}
return allVersions[version].openApiVersionName
}
// Generates the miniToc for a rest reference page.
export async function getRestMiniTocItems(
category,
subCategory,
restOperations,
language,
version,
context
) {
if (!restOperationData.get(language).get(version).has(category)) {
restOperationData.get(language).get(version).set(category, new Map())
}
if (!restOperationData.get(language).get(version).get(category).get(subCategory)) {
const languageTree = restOperationData.get(language)
const titles = restOperations.map((operation) => operation.title)
const restOperationsMiniTocItems = await getAutomatedPageMiniTocItems(titles, context, 3)
languageTree.get(version).get(category).set(subCategory, {
restOperationsMiniTocItems,
})
restOperationData.set(restOperationData, languageTree)
}
return restOperationData.get(language).get(version).get(category).get(subCategory)
}
export async function getEnabledForApps() {
// The `readCompressedJsonFileFallback()` function
// will check for both a .br and .json extension.
const appsData = readCompressedJsonFileFallback(ENABLED_APPS_FILENAME)
for (const version in appsData) {
const docsVersions = getDocsVersions(version)
docsVersions.forEach((docsVersion) => {
appsData[docsVersion] = appsData[version]
})
delete appsData[version]
}
return appsData
}