Skip to content

Commit 2be523d

Browse files
committed
Add readme-building utility script
1 parent e2f3f50 commit 2be523d

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

bin/build-readme.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Function to build github-proof readmes that contain the package's API
2+
// docs as HTML.
3+
4+
const {core, nonCore} = require("./packages")
5+
const {gather, gatherMany} = require("gettypes")
6+
const {build, browserImports} = require("builddocs")
7+
const {join} = require("path"), fs = require("fs")
8+
9+
exports.buildReadme = function(pkg) {
10+
let imports = [type => {
11+
let sibling = type.typeSource && core.find(name => type.typeSource.startsWith("../" + name + "/"))
12+
if (sibling) return "https://codemirror.net/6/docs/ref#" + sibling + "." + type.type
13+
}, type => {
14+
if (/\blezer[\/-]tree\b/.test(type.typeSource)) return `https://lezer.codemirror.net/docs/ref/#tree.${type.type}`
15+
if (/\blezer\b/.test(type.typeSource)) return `https://lezer.codemirror.net/docs/ref/#lezer.${type.type}`
16+
if (/\bstyle-mod\b/.test(type.typeSource)) return "https://github.com/marijnh/style-mod#documentation"
17+
}, browserImports]
18+
19+
let template = fs.readFileSync(join(pkg.dir, pkg.name == "legacy-modes" ? "mode" : "src", "README.md"), "utf8")
20+
let html = ""
21+
22+
if (pkg.name == "legacy-modes") {
23+
let mods = fs.readdirSync(join(pkg.dir, "mode")).filter(f => /\.d.ts$/.test(f)).map(file => {
24+
let name = /^(.*)\.d\.ts$/.exec(file)[1]
25+
return {name, filename: join(pkg.dir, "mode", file), basedir: pkg.dir}
26+
}), items = gatherMany(mods)
27+
for (let i = 0; i < mods.length; i++) {
28+
let {name} = mods[i]
29+
html += `\n<h3 id="${name}">mode/<a href="#${name}">${name}</a></h3>\n` + build({
30+
name: pkg.name,
31+
anchorPrefix: name + ".",
32+
allowUnresolvedTypes: false,
33+
imports
34+
}, items[i])
35+
}
36+
template += "\n$$$"
37+
} else {
38+
let placeholders = /\n@[^]*@\w+|\n@\w+/.exec(template)
39+
html = build({
40+
mainText: placeholders[0],
41+
name: pkg.name,
42+
anchorPrefix: "",
43+
allowUnresolvedTypes: false,
44+
imports
45+
}, gather({filename: pkg.main, basedir: pkg.dir}))
46+
template = template.slice(0, placeholders.index) + "\n$$$" + template.slice(placeholders.index + placeholders[0].length)
47+
}
48+
49+
html = html.replace(/<\/?span.*?>/g, "")
50+
.replace(/id="(.*?)"/g, (_, id) => `id="user-content-${id.toLowerCase()}"`)
51+
.replace(/href="#(.*?)"/g, (_, id) => {
52+
let first = /^[^^.]*/.exec(id)[0]
53+
if (core.includes(first)) return `href="https://codemirror.net/6/docs/ref/#${id}"`
54+
if (first == pkg.name && id.length > first.length) id = id.slice(first.length + 1)
55+
return `href="#user-content-${id.toLowerCase()}"`
56+
})
57+
58+
return template.replace("$$$", html)
59+
}

bin/cm.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const child = require("child_process"), fs = require("fs"), fsp = fs.promises, p
66

77
let root = join(__dirname, "..")
88

9-
const {loadPackages} = require("./packages")
9+
const {loadPackages, nonCore} = require("./packages")
1010

1111
let {packages, packageNames, buildPackages} = loadPackages()
1212

@@ -24,6 +24,7 @@ function start() {
2424
commit,
2525
push,
2626
grep,
27+
"build-readme": buildReadme,
2728
run: runCmd,
2829
"--help": () => help(0)
2930
}[command]
@@ -33,13 +34,14 @@ function start() {
3334

3435
function help(status) {
3536
console.log(`Usage:
36-
cm install [--ssh] Clone and symlink the packages, install dependencies, build
37+
cm install [--ssh] Clone and symlink the packages, install deps, build
3738
cm packages Emit a list of all pkg names
3839
cm build Build the bundle files
3940
cm clean Delete files created by the build
4041
cm devserver Start a dev server on port 8090
4142
cm release <package> [--edit] [--version <version>]
4243
Create commits to tag a release
44+
cm build-readme <pkg> Regenerate the readme file for a non-core package
4345
cm commit <args> Run git commit in all packages that have changes
4446
cm push Run git push in packages that have new commits
4547
cm run <command> Run the given command in each of the package dirs
@@ -387,4 +389,10 @@ function runCmd(cmd, ...args) {
387389
}
388390
}
389391

392+
function buildReadme(name) {
393+
if (!nonCore.includes(name)) help(1)
394+
let pkg = packageNames[name]
395+
fs.writeFileSync(join(pkg.dir, "README.md"), require("./build-readme").buildReadme(pkg))
396+
}
397+
390398
start()

bin/packages.js

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class Pkg {
5757
}
5858
}
5959
}
60+
exports.Pkg = Pkg
6061

6162
exports.loadPackages = function loadPackages() {
6263
let packages = exports.all.map(n => new Pkg(n))

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
"license": "(MIT OR GPL-3.0)",
1313
"devDependencies": {
1414
"@types/mocha": "^5.2.0",
15+
"builddocs": "git://github.com/marijnh/builddocs.git#v1",
1516
"eslint4b-prebuilt": "^6.7.1",
17+
"gettypes": "git://github.com/adrianheine/gettypes.git",
1618
"esmoduleserve": "^0.2.0",
1719
"mocha": "^7.1.1",
1820
"rollup": "^2.35.1",

0 commit comments

Comments
 (0)