Skip to content

Commit 5d218b0

Browse files
committed
build docs for modules
1 parent 8f71fea commit 5d218b0

File tree

13 files changed

+15320
-3286
lines changed

13 files changed

+15320
-3286
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,8 @@ Currently, the following language features are supported:
4141
## Documentation
4242

4343
Documentation for diagnostics can be found [here](server/src/rules)
44+
45+
## Autocomplete
46+
47+
Documentation for built in names or modules are generated by the script `server/src/docs/build_docs.mjs`. This script is not automatically run during the build process, as it is rarely updated.
48+
If an update to the autocomplete is required, run `npm run docs`.

modules.json

Lines changed: 6010 additions & 0 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@
6666
"watch": "tsc -b -w",
6767
"lint": "eslint",
6868
"postinstall": "cd client && npm install && cd ../server && npm install && cd ..",
69-
"test": "node node_modules/mocha/bin/mocha.js dist/tests/*.js --ui tdd"
69+
"test": "node node_modules/mocha/bin/mocha.js dist/tests/*.js --ui tdd",
70+
"docs": "node ./server/src/docs/build_docs.mjs"
7071
},
7172
"devDependencies": {
7273
"@eslint/js": "^9.13.0",

server/src/docs/build_docs.mjs

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// @ts-check
22

33
import fs from 'fs/promises';
4-
import pathlib from "path";
54
import TurndownService from 'turndown';
65
import { JSDOM } from 'jsdom';
76
import patches from "./patches.json" with { type: "json" };
@@ -11,7 +10,6 @@ const FUNC_DECL = "func";
1110

1211
const BASE_URL = "https://docs.sourceacademy.org"
1312
const SRC_FILENAME = "global.html"
14-
const OUT_DIR = "./"
1513

1614
const TARGETS = [
1715
"source_1",
@@ -101,19 +99,69 @@ async function processDirGlobals(global, target, index) {
10199
const functions = document.getElementsByClassName('function-entry')
102100
Array.prototype.forEach.call(functions, ele => processFunction(names, ele, document));
103101

104-
const outFile = pathlib.join(OUT_DIR, target + '.json');
105-
await fs.writeFile(outFile, JSON.stringify(names, null, 2), 'utf-8')
106102
global[index] = (names);
107103
return undefined
108104
}
109105

110-
await fs.mkdir(OUT_DIR, { recursive: true })
106+
console.log('Building built in autocomplete documentation')
111107

112108
const global = [];
113109
// Exit with error code if the there was some error
114110
const errors = await Promise.all(TARGETS.map((x, i) => processDirGlobals(global, x, i)));
115111
if (errors.find(each => each !== undefined)) process.exit(1);
116-
const outFile = pathlib.join(OUT_DIR, FINAL_TARGET + '.json');
117-
await fs.writeFile(outFile, JSON.stringify(global, null, 2), 'utf-8')
112+
await fs.writeFile(FINAL_TARGET + '.json', JSON.stringify(global, null, 2), 'utf-8')
118113

119-
console.log('Finished processing autocomplete documentation')
114+
console.log('Finished processing built in autocomplete documentation')
115+
116+
console.log('Building modules autocomplete documentation')
117+
118+
const MODULE_LIST_URL = "https://raw.githubusercontent.com/source-academy/modules/refs/heads/master/modules.json";
119+
const MODULE_DOCS_URL = "https://source-academy.github.io/modules/jsons";
120+
const OUTFILE = "modules.json";
121+
122+
const names = Object.keys(await (await fetch(MODULE_LIST_URL)).json())
123+
const modules = {}
124+
125+
function mapKindToMeta(kind) {
126+
switch (kind) {
127+
case "variable":
128+
return "const";
129+
case "function":
130+
return "func";
131+
default:
132+
return "const";
133+
}
134+
}
135+
136+
async function buildDoc(name) {
137+
const doc = await (await fetch(`${MODULE_DOCS_URL}/${name}.json`)).json();
138+
const module = {};
139+
for (const key in doc) {
140+
const item = {
141+
"label": key,
142+
meta: mapKindToMeta(doc[key]["kind"]),
143+
title: `Auto-import from ${name}`,
144+
description: ""
145+
};
146+
147+
if (doc[key]["kind"] === "variable")
148+
item["description"] = `#### ${key}:${doc[key]['type']}\n${turndownService.turndown(doc[key]["description"])}`;
149+
else if (doc[key]["kind"] === "function") {
150+
const params = doc[key]['params'].map(x => x[0]);
151+
item["description"] = `#### ${key}(${params.join(', ')}) → ${doc[key]['retType']}\n${turndownService.turndown(doc[key]["description"])}`;
152+
item["parameters"] = params
153+
}
154+
155+
module[key] = item;
156+
}
157+
158+
return module;
159+
}
160+
161+
for (const name of names) {
162+
modules[name] = await buildDoc(name);
163+
}
164+
165+
await fs.writeFile(OUTFILE, JSON.stringify(modules, null, 2), 'utf-8')
166+
167+
console.log('Finished processing modules autocomplete documentation')

server/src/docs/modules.json

Lines changed: 6010 additions & 0 deletions
Large diffs are not rendered by default.

server/src/docs/modules/downloadModules.py

Lines changed: 0 additions & 41 deletions
This file was deleted.

server/src/docs/modules/modules.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)