-
-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathprember-urls.js
More file actions
94 lines (82 loc) · 2.78 KB
/
Copy pathprember-urls.js
File metadata and controls
94 lines (82 loc) · 2.78 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
'use strict';
const fs = require('fs');
const path = require('path');
/**
* Prember URL enumeration for ember-cli-addon-docs.
*
* Automatically discovers all documentation pages by reading
* the generated docs JSON and search index from the build output.
*
* Usage in ember-cli-build.js:
*
* const app = new EmberAddon(defaults, {
* prember: {
* urls: require('ember-cli-addon-docs/lib/prember-urls'),
* },
* });
*
* @param {Object} options
* @param {string} options.distDir - Path to the built dist directory
* @returns {string[]} Array of URLs to pre-render
*/
module.exports = function premberUrls({ distDir }) {
let urls = new Set(['/']);
// Discover guide/template pages and API pages from the search index.
// The search index contains all indexed documents with their route info.
let searchIndexPath = path.join(
distDir,
'ember-cli-addon-docs',
'search-index.json',
);
if (fs.existsSync(searchIndexPath)) {
let searchIndex = JSON.parse(fs.readFileSync(searchIndexPath, 'utf8'));
for (let doc of Object.values(searchIndex.documents || {})) {
if (doc.type === 'template' && doc.route) {
// Skip internal/non-page routes
if (
doc.route === 'application' ||
doc.route === 'not-found' ||
doc.route.startsWith('templates.') ||
doc.route.startsWith('pods.')
) {
continue;
}
let routePath = doc.route.replace(/\./g, '/').replace(/\/index$/, '');
if (routePath === 'index') routePath = '';
urls.add('/' + routePath);
}
}
}
// Discover API pages from the main project's docs JSON navigationIndex.
// We read all JSON files in docs/ and use the project ID to build URLs.
// The main project maps to /docs/api/, additional projects would need
// custom URL mapping from the consuming app.
let docsDir = path.join(distDir, 'docs');
if (fs.existsSync(docsDir)) {
let files = fs
.readdirSync(docsDir)
.filter((f) => f.endsWith('.json'))
.sort();
for (let i = 0; i < files.length; i++) {
let docsJson = JSON.parse(
fs.readFileSync(path.join(docsDir, files[i]), 'utf8'),
);
let projectData = Array.isArray(docsJson.data)
? docsJson.data[0]
: docsJson.data;
let navIndex = projectData?.attributes?.navigationIndex || [];
// Only generate /docs/api/ URLs for the first (alphabetically)
// project. Additional projects have their own route prefixes
// that we can't determine automatically.
if (files.length === 1 || i === 0) {
urls.add('/docs');
for (let section of navIndex) {
for (let item of section.items) {
urls.add(`/docs/api/${item.path}`);
}
}
}
}
}
return [...urls];
};