This repository has been archived by the owner on Sep 27, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
api.js
204 lines (179 loc) · 4.99 KB
/
api.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
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
const { red, yellow } = require('kleur')
const axios = require('axios')
const { utils } = require('dimer-vue')
const BASE_URL = 'http://localhost:4444'
/**
* A mapping of zones and the template component they must use
*/
const ZONE_TEMPLATE_MAPPING = {
pages: './src/templates/Pages.vue',
blog: './src/templates/Blog.vue',
guides: './src/templates/Guides.vue',
api: './src/templates/Api.vue',
releases: './src/templates/Guides.vue'
}
/**
* We decided to stick to a single version for the docs. Every
* major release can get it's own website (a copy of it) and
* every minor release can have inline notes.
*/
const WEBSITE_VERSION = 'master'
/**
* Returns an array of zones and their respective version. Each
* zone has a single version only.
*/
async function getZones () {
const { data } = await axios.get(`${BASE_URL}/zones.json`)
return data.map(({ name, slug, versions }) => {
const component = ZONE_TEMPLATE_MAPPING[slug]
const version = versions.find((version) => version.no === WEBSITE_VERSION)
if (!component) {
throw new Error(
`${slug} doesn't have a component assigned to it. Open "frontend/api.js" to assign a component`,
)
}
if (!version) {
throw new Error(`${slug} must have a ${WEBSITE_VERSION} in order to be compiled.`)
}
return {
name: name,
slug: slug,
component,
version,
}
})
}
/**
* Returns a tree of categories and optionally their docs with complete
* content.
*/
async function getZoneGroupsAndCategories (zoneSlug, versionNo, includeContent) {
if (!zoneSlug || !versionNo) {
throw new Error(
`Cannot fetch categories. zoneSlug and versionNo are required.`
)
}
const params = {}
if (includeContent) {
params.load_content = true
}
const { data } = await axios(`${BASE_URL}/${zoneSlug}/versions/${versionNo}.json`, { params })
const groups = []
/**
* Looping over categories to group them by their group
*/
data.filter((category) => {
category.docs.forEach((doc) => {
if (!doc.group) {
console.log(red(`${doc.permalink} is ignored, since it has no group`))
return
}
if (doc.group === 'Security') {
return
}
if (doc.draft) {
console.log(yellow(`${doc.permalink} is ignored, since it is in draft mode`))
return
}
let group = groups.find(({ name }) => name === doc.group)
if (!group) {
group = {
name: doc.group,
categories: []
}
groups.push(group)
}
if (!group.under_progress && doc.under_progress) {
group.under_progress = true
group.last_updated_on = doc.last_updated_on
}
let category = group.categories.find(({ name }) => name === doc.category)
if (!category) {
category = {
name: doc.category,
docs: []
}
group.categories.push(category)
}
category.docs.push(doc)
})
})
return groups
}
/**
* Returns the content for a doc
*/
async function getDoc (zoneSlug, versionNo, permalink) {
if (!zoneSlug || !versionNo || !permalink) {
throw new Error(
`Cannot fetch doc. "zoneSlug", "versionNo" and "permalink" are required.`
)
}
const { data } = await axios(`${BASE_URL}/${zoneSlug}/versions/${versionNo}/${permalink}.json`)
return data
}
/**
* Returns the page data for the doc
*/
function getDocPageData (zone, doc, groups) {
if (!zone) {
throw new Error('Make sure to define the zone when getting page data')
}
if (!zone.component) {
throw new Error('The zone must have a component property in order to generate page data')
}
/**
* Extracting toc from the doc content
*/
const toc = utils.extractNode(doc.content, (node) => {
return node.tag === 'div'
&& node.props.className
&& node.props.className.includes('toc-container')
})
/**
* Remove h1
*/
utils.extractNode(doc.content, (node) => {
return node.tag === 'h1'
})
const docGroup = groups.find(({ name }) => name === doc.group) || {}
return {
path: `/${doc.permalink}`,
component: zone.component,
context: {
doc: {
title: doc.title,
content: doc.content,
group: doc.group,
permalink: doc.permalink,
meta: doc.meta || null,
},
toc: toc,
groups: groups.map((group) => {
return {
name: group.name,
permalink: group.categories[0].docs[0].permalink,
}
}),
under_progress: docGroup.under_progress,
last_updated_on: docGroup.last_updated_on,
categories: docGroup.categories.map((category) => {
return {
name: category.name,
docs: category.docs.map((doc) => {
return {
title: doc.title,
permalink: doc.permalink,
}
})
}
}),
},
}
}
module.exports = {
getZones: getZones,
getZoneGroupsAndCategories: getZoneGroupsAndCategories,
getDoc: getDoc,
getDocPageData: getDocPageData,
}