Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Programmatically generate the Docs navbar dropdown #524

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 100 additions & 3 deletions build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { Buffer } from "node:buffer";
import { readFileSync } from "node:fs";
import fs from "node:fs/promises";
import https from "node:https";
import path from "node:path";
import { fileURLToPath } from "node:url";

Expand All @@ -16,6 +17,99 @@ import { PurgeCSS } from "purgecss";
// eslint-disable-next-line no-shadow
const __dirname = fileURLToPath(new URL(".", import.meta.url));

const loadJsonFromUrl = async (url) => {
const options = {
headers: { "User-Agent": "Mozilla/5.0" },
};
// return new pending promise
return new Promise((resolve, reject) => {
// select http or https module, depending on reqested url
const request = https.get(url, options, (response) => {
// handle http errors
if (response.statusCode < 200 || response.statusCode > 299) {
reject(
new Error("Failed to load page, status code: " + response.statusCode)
);
}
// temporary data holder
const body = [];
// on every content chunk, push it to the data array
response.on("data", (chunk) => body.push(chunk));
// we are done, resolve promise with those joined chunks
response.on("end", () => resolve(body.join("")));
});
// handle connection errors of the request
request.on("error", (err) => reject(err));
}).then((body) => JSON.parse(body));
};

const getCrates = async () => {
const artichokeTree = await loadJsonFromUrl(
"https://api.github.com/repos/artichoke/artichoke/git/trees/trunk"
);
const dirs = artichokeTree.tree.filter((item) => item.type === "tree");

const rustCrates = (
await loadJsonFromUrl("https://api.github.com/orgs/artichoke/repos")
)
.filter((repo) => repo.topics.includes("rust-crate"))
.filter((repo) => !repo.topics.includes("fork"))
.filter((repo) => repo.name !== "artichoke")
.map((repo) => {
return { name: repo.name, docs: repo.homepage };
});
rustCrates.sort((a, b) => a.name.localeCompare(b.name));

const makeDocEntry = (item) => {
const crateName = item.path.replaceAll("-", "_");
return {
name: item.path,
docs: `https://artichoke.github.io/artichoke/${crateName}`,
};
};

return [
{
section: "artichoke",
crates: [makeDocEntry({ path: "artichoke" })],
hasNext: true,
},
{
section: "core",
crates: dirs
.filter((item) => item.path.startsWith("artichoke"))
.map(makeDocEntry),
hasNext: true,
},
{
section: "spinoso",
crates: dirs
.filter((item) => item.path.startsWith("spinoso"))
.map(makeDocEntry),
hasNext: true,
},
{
section: "scolapasta",
crates: dirs
.filter((item) => item.path.startsWith("scolapasta"))
.map(makeDocEntry),
hasNext: true,
},
{
section: "mezzaluna",
crates: dirs
.filter((item) => item.path.startsWith("mezzaluna"))
.map(makeDocEntry),
hasNext: true,
},
{
section: "first party external",
crates: rustCrates,
hasNext: false,
},
];
};

const makeLocale = (language, twitter, isDefaultLocale = false) => {
const urlPrefix = isDefaultLocale ? "/" : `/${language.toLowerCase()}/`;
const pathPrefix = isDefaultLocale ? "" : `${language.toLowerCase()}`;
Expand Down Expand Up @@ -105,7 +199,7 @@ const includeMarkdown = (source) => {
return marked(content.toString());
};

const renderTemplate = async (template, locale) => {
const renderTemplate = async (template, locale, crates) => {
const t = JSON.parse(await fs.readFile(locale.stringsPath));

const context = {
Expand All @@ -116,6 +210,7 @@ const renderTemplate = async (template, locale) => {
defaultLocale: locales.find((locale) => locale.default),
t,
includeMarkdown,
crates,
};
let content = await renderFile(template, context, {
views: path.join(__dirname, "src"),
Expand All @@ -141,6 +236,8 @@ const renderTemplate = async (template, locale) => {
};

const build = async () => {
const crates = await getCrates();

await Promise.all(
locales.map(async (locale) => {
await fs.mkdir(
Expand All @@ -167,13 +264,13 @@ const build = async () => {

await Promise.all(
locales.map(async (locale) => {
let index = await renderTemplate("index.html", locale);
let index = await renderTemplate("index.html", locale, crates);
const indexOut = path.normalize(
path.join(__dirname, "dist", locale.pathPrefix, "index.html")
);
await fs.writeFile(indexOut, index);

let install = await renderTemplate("install.html", locale);
let install = await renderTemplate("install.html", locale, crates);
const installOut = path.normalize(
path.join(__dirname, "dist", locale.pathPrefix, "install", "index.html")
);
Expand Down
2 changes: 1 addition & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
<%= it.t.skip_to_main %>
</a>
<%~ includeFile("partials/nav.html", { page: "home", t: it.t.nav, locale:
it.locale, locales: it.locales }) %>
it.locale, locales: it.locales, crates: it.crates }) %>

<!--
This layout is heavily inspired by the Bootstrap v5 docs homepage, which
Expand Down
2 changes: 1 addition & 1 deletion src/install.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
<%= it.t.skip_to_main %>
</a>
<%~ includeFile("partials/nav.html", { page: "install", t: it.t.nav, locale:
it.locale, locales: it.locales }) %>
it.locale, locales: it.locales, crates: it.crates }) %>

<!--
This layout is heavily inspired by the Bootstrap v5 docs homepage, which
Expand Down
109 changes: 8 additions & 101 deletions src/partials/nav.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,115 +61,22 @@
>
<%= it.t.docs %>
</a>
<!-- prettier-ignore -->
<ul
class="dropdown-menu dropdown-menu-end"
aria-labelledby="navbarDropdown"
>
<% for (const section of it.crates) { %> <% for (const crate of section.crates) { %>
<li>
<a
class="dropdown-item"
href="https://artichoke.github.io/artichoke/artichoke/"
>
artichoke
</a>
</li>
<li>
<a
class="dropdown-item"
href="https://artichoke.github.io/artichoke/artichoke_core/"
>
artichoke-core
</a>
</li>
<li>
<a
class="dropdown-item"
href="https://artichoke.github.io/artichoke/artichoke_backend/"
>
artichoke-backend
<a class="dropdown-item" href="<% crate.docs %>">
<%= crate.name %>
</a>
</li>
<% } %>
<% if (section.hasNext) { %>
<li><hr class="dropdown-divider" /></li>
<li>
<a
class="dropdown-item"
href="https://artichoke.github.io/artichoke/spinoso_array/"
>
spionso-array
</a>
</li>
<li>
<a
class="dropdown-item"
href="https://artichoke.github.io/artichoke/spinoso_env/"
>
spionso-env
</a>
</li>
<li>
<a
class="dropdown-item"
href="https://artichoke.github.io/artichoke/spinoso_exception/"
>
spionso-exception
</a>
</li>
<li>
<a
class="dropdown-item"
href="https://artichoke.github.io/artichoke/spinoso_math/"
>
spionso-math
</a>
</li>
<li>
<a
class="dropdown-item"
href="https://artichoke.github.io/artichoke/spinoso_random/"
>
spionso-random
</a>
</li>
<li>
<a
class="dropdown-item"
href="https://artichoke.github.io/artichoke/spinoso_regexp/"
>
spionso-regexp
</a>
</li>
<li>
<a
class="dropdown-item"
href="https://artichoke.github.io/artichoke/spinoso_securerandom/"
>
spionso-securerandom
</a>
</li>
<li>
<a
class="dropdown-item"
href="https://artichoke.github.io/artichoke/spinoso_string/"
>
spionso-string
</a>
</li>
<li>
<a
class="dropdown-item"
href="https://artichoke.github.io/artichoke/spinoso_symbol/"
>
spionso-symbol
</a>
</li>
<li>
<a
class="dropdown-item"
href="https://artichoke.github.io/artichoke/spinoso_time/"
>
spionso-time
</a>
</li>
<% } %>
<% } %>
</ul>
</li>
<li class="nav-item dropdown">
Expand Down