-
Notifications
You must be signed in to change notification settings - Fork 12
/
check-content.js
220 lines (197 loc) · 6.37 KB
/
check-content.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
'use strict'
const klaw = require('klaw')
const path = require('path')
const fs = require('fs')
// mode could be warn, debug or throw
checkSidebar('./docs', './sidebars.json', '', false, 'throw')
const filesToExcludeFromSidebarCheck = [
"monitoring/paas_alerting_rules",
"info/licenses-reports/.gitkeep",
'cli/miactl/*',
"info/licenses/*",
"runtime_suite/*",
"runtime_suite_applications/*",
"runtime_suite_templates/*",
"runtime_suite_libraries/*",
"runtime_suite_tools/*",
"microfrontend-composer/back-kit/*",
"microfrontend-composer/composer/*",
"infrastructure/self-hosted/installation-chart/*",
"getting-started/videos/*",
"getting-started/videos/subtitle/*",
"console/videos/*",
"console/videos/subtitle/*",
"fast_data/videos/*",
"fast_data/videos/subtitle/*",
"microfrontend-composer/videos/*",
"microfrontend-composer/videos/subtitle/*",
"standalone-resources/*",
"fast_data/runtime_management/snippets/*",
"fast_data/snippets/*",
"development_suite/api-console/api-design/videos/subtitle/*"
]
const checkIdRegexp = new RegExp('^---(\\n.*)+id:\\s+([-\\w. ]+)(.*\\n)+---$', 'm')
const checkImg = new RegExp('\\[[^\\]]*\\]\\((?<filename>.*?)(?=\\"|\\))(?<optionalpart>\\".*\\")?\\)', 'mg')
const checkImgTag = new RegExp('<img src=".+" \\/>')
const checkImportImg = new RegExp('import .+ from \'(.+(?:\\.png|\\.svg|\\.jpeg|\\.jpg|\\.bmp))\'', 'gi')
async function checkSidebar(folder, sidebarFilePath, sidebarBasePath, removeImages, mode) {
const absolutePathPrefix = path.join(process.cwd(), folder, '/')
const sidebarFiles = getSidebarLinkedFiles(sidebarFilePath, sidebarBasePath)
const footerFiles = await getFooterLinkedFiles()
const images = []
for await (const file of klaw(folder)) {
if (file.stats.isDirectory()) {
continue
}
const ext = path.extname(file.path)
if (/^.mdx?/.test(ext)) {
continue
}
const relativeFilePath = file.path.replace(absolutePathPrefix, '')
images[relativeFilePath] = {
file: file.path,
isUsed: false
}
}
const files = []
for await (const file of klaw(folder)) {
if (file.stats.isDirectory()) {
continue
}
const ext = path.extname(file.path)
if (!/^.mdx?/.test(ext)) {
continue
}
const content = fs.readFileSync(file.path, 'utf8')
const header = content.match(checkIdRegexp)
const manageMatchedImage = img => {
const absolutePath = path.resolve(path.parse(file.path).dir, img)
images[absolutePath.replace(absolutePathPrefix, '')] = {
file: absolutePath,
isUsed: true
}
}
content.match(checkImg)?.map(img => img.replace(/^\[.*\]\(/, '').replace(/\)$/, '')).forEach(manageMatchedImage)
content.match(checkImgTag)
?.map(img => img.replace("<img src=\"", "").replace("\" />", "").trim())
.forEach(manageMatchedImage)
const imagesFromImport = [...content.matchAll(checkImportImg)].map(match => match[1])
imagesFromImport.forEach(manageMatchedImage)
let filePath = file.path
if (header && header[2]) {
filePath = filePath.replace(path.basename(file.path), header[2])
}
const relativeFilePath = filePath.replace(absolutePathPrefix, '')
if (/^.mdx?/.test(ext)) {
files.push(relativeFilePath.replace(path.extname(file.path), ''))
}
}
const filesNotInSidebar = []
for (const fileName of files) {
if (excludeFromSidebarCheck(fileName)) {
continue
}
const fileNameWithoutExtension = fileName
if (!sidebarFiles[fileNameWithoutExtension] && !footerFiles[fileNameWithoutExtension]) {
filesNotInSidebar.push(fileNameWithoutExtension)
if (mode === 'warn') {
// eslint-disable-next-line no-console
console.warn(`${fileName} is not in the sidebar`)
}
}
}
if (mode === 'debug') {
fs.writeFileSync('./filesNotInSidebar.json', JSON.stringify(filesNotInSidebar, null, 2))
}
const imagesNotLinked = []
for (const img in images) {
if(img.includes('.gitkeep') || excludeFromSidebarCheck(img)) {
continue
}
if (!images[img].isUsed) {
imagesNotLinked.push(images[img].file.replace(absolutePathPrefix, ''))
if (mode === 'warn') {
// eslint-disable-next-line no-console
console.warn('image not linked:', images[img].file)
}
if (removeImages) {
fs.rmSync(images[img].file)
}
}
}
if (mode === 'debug') {
fs.writeFileSync('./images.json', JSON.stringify(imagesNotLinked, null, 2))
}
if (mode === 'throw' && filesNotInSidebar.length > 0 || imagesNotLinked.length > 0) {
const err = new Error('Sidebar not linked to files and images not linked')
err.filesNotInSidebar = JSON.stringify(filesNotInSidebar, null, 2)
err.imagesNotLinked = JSON.stringify(imagesNotLinked, null, 2)
throw err
}
}
function getItems(items, prefix) {
let files = {}
for (let item of items) {
if (item.type === 'autogenerated') {
continue
}
if (item.items) {
files = {
...files,
...getItems(item.items, prefix)
}
} else {
files[item.id.replace(prefix, '')] = true
}
if (item.link?.id) {
files[item.link.id.replace(prefix, '')] = true
}
}
return files
}
function getSidebarLinkedFiles(sidebarFilePath, prefix = '') {
const sidebar = require(sidebarFilePath)
let files = {}
Object.values(sidebar).forEach(category => {
category.forEach(detail => {
if (typeof detail === 'string') {
files[detail] = true
return
}
if (detail.items) {
files = {
...files,
...getItems(detail.items, prefix)
}
} else if (detail.id) {
files[detail.id.replace(prefix, '')] = true
}
if (detail.link?.id) {
files[detail.link.id.replace(prefix, '')] = true
}
})
})
return files
}
async function getFooterLinkedFiles() {
const config = await require('./docusaurus.config')()
let files = {}
config.themeConfig.footer.links.forEach(({items}) => {
items.forEach(item => {
if (item.to) {
files[item.to.replace(/^\/docs\//, '')] = true
}
})
})
return files
}
function excludeFromSidebarCheck(fileName) {
if (filesToExcludeFromSidebarCheck.includes(fileName)) {
return true
}
return Boolean(filesToExcludeFromSidebarCheck.find(pattern => {
if (pattern.endsWith('/*') && fileName.startsWith(pattern.slice(0, -2))) {
return true
}
}))
}