Skip to content

Commit 6ceb1f9

Browse files
committed
Generate index for Algolia with docs and blog
1 parent e5927f6 commit 6ceb1f9

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
public
22
.DS_STORE
33
.idea
4+
dist
5+
node_modules

package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "graphql-java-page",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index-generator.js",
6+
"scripts": {
7+
"algolia:index": "node scripts/index-generator.js"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/graphql-java/graphql-java-page.git"
12+
},
13+
"keywords": [],
14+
"author": "",
15+
"license": "ISC",
16+
"bugs": {
17+
"url": "https://github.com/graphql-java/graphql-java-page/issues"
18+
},
19+
"homepage": "https://github.com/graphql-java/graphql-java-page#readme"
20+
}

scripts/index-generator.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
const promisify = require('util').promisify;
2+
const path = require('path');
3+
const fs = require('fs');
4+
const readdir = promisify(fs.readdir);
5+
const readFile = promisify(fs.readFile);
6+
const writeFile = promisify(fs.writeFile);
7+
const exists = promisify(fs.exists);
8+
const mkdir = promisify(fs.mkdir);
9+
10+
const contentDir = path.resolve('.', 'content');
11+
12+
const getContentDir = dirPath => path.resolve(contentDir, dirPath);
13+
14+
const baseUrl = 'https://www.graphql-java.com';
15+
16+
const generateUrl = (section, fileName) =>
17+
[baseUrl, section, fileName].join('/').replace('.md', '');
18+
19+
const extractTitle = content => {
20+
const regex = /title.*\"(.*)\"/;
21+
22+
const match = regex.exec(content);
23+
24+
if (match) {
25+
return match[1];
26+
}
27+
28+
return '';
29+
};
30+
31+
const removeMetadata = content =>
32+
content.replace(/[\-\+]{3}.*[\-\+]{3}/s, '');
33+
34+
const truncate = content => {
35+
const MAX_CONTENT_LENGTH = 1000;
36+
return content
37+
.substring(0, MAX_CONTENT_LENGTH);
38+
};
39+
40+
const indexBlog = async () => {
41+
const indexedBlogs = await indexDir('blog')
42+
43+
return indexedBlogs.map(indexedBlog => ({
44+
...indexedBlog,
45+
category: 'Blogs'
46+
}));
47+
}
48+
49+
const indexDocs = async () => {
50+
const docContentDir = getContentDir('documentation');
51+
52+
const docDirs = await readdir(docContentDir)
53+
54+
const promises = docDirs.map(docDir => {
55+
if (fs.statSync(path.resolve(docContentDir, docDir)).isDirectory()) {
56+
return indexDir(`documentation/${docDir}`);
57+
}
58+
});
59+
60+
const indexedDirs = await Promise.all(promises);
61+
62+
return indexedDirs
63+
.filter(Boolean)
64+
.reduce((acc, indexedDocs) => acc.concat(indexedDocs))
65+
.map(indexedDoc => ({
66+
...indexedDoc,
67+
category: 'Documentation'
68+
}));
69+
}
70+
71+
const execute = async () => {
72+
const OUTPUT_DIR = path.resolve('.', 'dist');
73+
74+
const indexedBlog = await indexBlog();
75+
const indexedDocs = await indexDocs();
76+
const indexedAll = indexedBlog.concat(indexedDocs);
77+
78+
if (!(await exists(OUTPUT_DIR))) {
79+
await mkdir(OUTPUT_DIR);
80+
}
81+
82+
await writeFile(path.resolve(OUTPUT_DIR, 'algolia-index.json'), JSON.stringify(indexedAll));
83+
}
84+
85+
const indexDir = async dirPath => {
86+
const contentDir = getContentDir(dirPath)
87+
const fileNames = await readdir(contentDir);
88+
89+
const filePromises = fileNames
90+
.map(name => ({
91+
name,
92+
path: path.resolve(contentDir, name),
93+
url: generateUrl(dirPath, name)
94+
}))
95+
.map(async fileObj => ({
96+
...fileObj,
97+
rawContent: await readFile(fileObj.path, 'utf8')
98+
}
99+
));
100+
101+
let fileObjects = await Promise.all(filePromises);
102+
103+
return fileObjects.map(({ name, path, url, rawContent }) => ({
104+
name,
105+
path,
106+
url,
107+
title: extractTitle(rawContent),
108+
content: truncate(removeMetadata(rawContent))
109+
}));
110+
}
111+
112+
execute();

0 commit comments

Comments
 (0)