|
| 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 | +} |
0 commit comments