Skip to content
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
17 changes: 11 additions & 6 deletions .github/workflows/site.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# SPDX-License-Identifier: Apache-2.0

# Build the whole site from sources.yml and publish it to GitHub Pages.
# Build the whole site from sources.yml and publish it to GitHub Pages, as
# Netlify does but under /<repo>/: the website pages at /<repo>/ and the docs at
# /<repo>/docs/.
#
# Runs on pull requests (build only), on changes to main, hourly (to pick up
# new commits on each source's dev branch), and on demand. A source repo can
Expand Down Expand Up @@ -44,25 +46,28 @@ jobs:
- name: Unit tests
run: npm test
- name: Assemble (fetches each source at its ref)
run: node assembler/assemble.mjs --base /${{ github.event.repository.name }}/
run: node assembler/assemble.mjs --base /${{ github.event.repository.name }}/docs/
- name: Type-check
run: npm run typecheck
- name: Build site
- name: Build docs
run: npm run site
env:
BASE_PATH: /${{ github.event.repository.name }}/
BASE_PATH: /${{ github.event.repository.name }}/docs/
SITE_URL: https://${{ github.repository_owner }}.github.io
WEBSITE: "1"
- name: Build website
run: npm run website
- name: Record the commits this build used
run: |
echo '### Sources' >> "$GITHUB_STEP_SUMMARY"
echo '| Version | Ref | Commit | Format |' >> "$GITHUB_STEP_SUMMARY"
echo '|---|---|---|---|' >> "$GITHUB_STEP_SUMMARY"
node -e 'for (const [k, v] of Object.entries(require("./build/assembled/sources.lock.json"))) console.log(`| ${k} | ${v.repo}@${v.ref} | ${v.sha.slice(0, 12)} | ${v.format} |`)' >> "$GITHUB_STEP_SUMMARY"
cp build/assembled/sources.lock.json docs/dist/
cp build/assembled/sources.lock.json build/website/
- uses: actions/upload-pages-artifact@v5
if: github.event_name != 'pull_request'
with:
path: docs/dist
path: build/website

deploy:
if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main'
Expand Down
10 changes: 7 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,13 @@ override the Netlify UI: Netlify runs `npm run build && npm run website` with
`BASE_PATH=/docs/` and `WEBSITE=1`, and publishes `build/website`. Netlify also
builds a deploy preview for each pull request.

The [Site workflow](.github/workflows/site.yml) builds the docs on every pull
request and publishes them to GitHub Pages from `main`, at `/<repository>/`
and without the website pages.
The [Site workflow](.github/workflows/site.yml) builds the same website on
every pull request and publishes it to GitHub Pages from `main`, under
`/<repository>/`: the website pages at `/<repository>/` and the docs at
`/<repository>/docs/`. It builds the docs with `BASE_PATH=/<repository>/docs/`;
`scripts/website.mjs` takes the site root from that base and prefixes the
website pages' root-relative links with it, so write those links from `/`, such
as `/docs/about/overview/`.

## Commits and pull requests

Expand Down
5 changes: 4 additions & 1 deletion docs/src/components/overrides/Header.astro
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
import { siteBar } from '../../../../theme/site-bar.mjs';

const base = import.meta.env.BASE_URL;
// The website serves the docs at <root>docs/ (scripts/website.mjs).
const root = base.replace(/docs\/$/, '');
// Search this build's Pagefind index; on the full website (WEBSITE=1), merge in
// the website pages' index too.
const bar = siteBar({
root,
current: base,
search: {
bundlePath: `${base}pagefind/`,
baseUrl: base,
merge: process.env.WEBSITE ? '/pagefind-website/' : undefined,
merge: process.env.WEBSITE ? `${root}pagefind-website/` : undefined,
},
});

Expand Down
31 changes: 19 additions & 12 deletions scripts/website.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
// into the landing page's shell (header, footer, scripts) at /<name>/, with
// its Markdown copy at /<name>.md, like the docs pages. /llms.txt links the
// docs index and lists the website pages.
// The docs build must use BASE_PATH=/docs/ so its links resolve under /docs/.
// The docs build must use BASE_PATH=<root>docs/ so its links resolve under
// <root>docs/. The root is / on Netlify and /<repo>/ on GitHub Pages; links in
// the website pages are written from / and get the root prepended here.
// Usage: node scripts/website.mjs docs/dist build/website
import fs from 'node:fs';
import path from 'node:path';
Expand All @@ -24,10 +26,13 @@ const repo = path.resolve(import.meta.dirname, '..');
const pagesDir = path.join(repo, 'website');

const { base } = JSON.parse(fs.readFileSync(path.join(docs, 'versions.json'), 'utf8'));
if (base !== `/${MOUNT}/`) {
console.error(`${docs} was built with base ${base}; rebuild it with BASE_PATH=/${MOUNT}/`);
if (!base.endsWith(`/${MOUNT}/`)) {
console.error(`${docs} was built with base ${base}; rebuild it with BASE_PATH=<root>${MOUNT}/, such as /${MOUNT}/`);
process.exit(1);
}
const root = base.slice(0, -`${MOUNT}/`.length);
// Root-relative links and assets in the website pages, served under root.
const rooted = (html) => (root === '/' ? html : html.replace(/(\s(?:href|src)=")\/(?!\/)/g, `$1${root}`));

fs.rmSync(out, { recursive: true, force: true });
fs.cpSync(pagesDir, out, {
Expand All @@ -40,7 +45,7 @@ for (const f of ['favicon.svg', 'apple-touch-icon.png', 'cncf'])
fs.cpSync(path.join(repo, 'docs/static', f), path.join(out, f), { recursive: true });
fs.cpSync(docs, path.join(out, MOUNT), { recursive: true });
// Hosts serve /404.html for any missing path. The docs one links its assets
// absolutely (/docs/_astro/...), so it works at the root too.
// absolutely (<root>docs/_astro/...), so it works at the root too.
fs.copyFileSync(path.join(docs, '404.html'), path.join(out, '404.html'));

const htmlEscape = (s) => s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
Expand All @@ -56,15 +61,16 @@ const withNav = (html, current) =>
html,
/<!-- site-bar -->/,
siteBar({
root,
current,
search: { bundlePath: `/${MOUNT}/pagefind/`, baseUrl: `/${MOUNT}/`, merge: '/pagefind-website/' },
search: { bundlePath: `${base}pagefind/`, baseUrl: base, merge: `${root}pagefind-website/` },
}),
'<!-- site-bar --> placeholder',
);

// The landing page.
const shell = fs.readFileSync(path.join(pagesDir, 'index.html'), 'utf8');
fs.writeFileSync(path.join(out, 'index.html'), withNav(shell, '/'));
fs.writeFileSync(path.join(out, 'index.html'), withNav(rooted(shell), root));

// Markdown pages.
const render = unified().use(remarkParse).use(remarkGfm).use(remarkRehype).use(rehypeStringify);
Expand Down Expand Up @@ -111,12 +117,13 @@ for (const file of pages) {
`<link rel="alternate" type="text/markdown" href="/${name}.md" />`,
'Markdown alternate link',
);
html = withNav(swap(html, /<main>[\s\S]*<\/main>/, main, '<main>'), `/${name}/`);
html = withNav(rooted(swap(html, /<main>[\s\S]*<\/main>/, main, '<main>')), `${root}${name}/`);
fs.mkdirSync(path.join(out, name), { recursive: true });
fs.writeFileSync(path.join(out, name, 'index.html'), html);
// Pagefind URLs are relative to the index's baseUrl (the root, see search.js).
rendered.push([`/${name}/`, html]);
fs.writeFileSync(path.join(out, `${name}.md`), `# ${meta.title}\n\n${body.replace(/:[a-z][a-z0-9-]*: /g, '')}\n`);
listed.push(`- [${meta.title}](/${name}.md): ${meta.description}`);
listed.push(`- [${meta.title}](${root}${name}.md): ${meta.description}`);
}

// /llms.txt: a link to the docs index (/docs/llms.txt) and the website pages.
Expand All @@ -135,7 +142,7 @@ fs.writeFileSync(
'',
'## Docs',
'',
`- [OpenShell documentation](/${MOUNT}/llms.txt): Index of every docs page, latest release of each docset first.`,
`- [OpenShell documentation](${base}llms.txt): Index of every docs page, latest release of each docset first.`,
'',
'## Website',
'',
Expand All @@ -144,13 +151,13 @@ fs.writeFileSync(
].join('\n'),
);

// Search: index the Markdown pages into /pagefind-website/, which the docs search
// merges with its own index (pagefind.mergeIndex in docs/astro.config.mjs).
// Search: index the Markdown pages into <root>pagefind-website/, which the site
// bar's search merges with the docs index (theme/search.js).
const { index } = await pagefind.createIndex();
for (const [url, content] of rendered) await index.addHTMLFile({ url, content });
await index.writeFiles({ outputPath: path.join(out, 'pagefind-website') });
await pagefind.close();

console.log(
`website -> ${path.relative(process.cwd(), out)} (landing at /, ${pages.length} Markdown pages, docs at /${MOUNT}/)`,
`website -> ${path.relative(process.cwd(), out)} (landing at ${root}, ${pages.length} Markdown pages, docs at ${base})`,
);
6 changes: 3 additions & 3 deletions theme/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Search for the site bar (site-bar.mjs), on every page. The dialog's data
// attributes say which Pagefind index to search (data-bundle, data-base-url)
// and, on the full website, which index to merge (data-merge: the website
// pages in /pagefind-website/). The Pagefind UI loads on first open.
// pages in <root>pagefind-website/, and data-merge-base-url: their root). The Pagefind UI loads on first open.
const dialog = document.querySelector('.search-dialog');
const container = dialog?.querySelector('#site-search');
const kbd = document.querySelector('.search-button__kbd');
Expand All @@ -14,7 +14,7 @@ const ranking = { pageLength: 0.1, termFrequency: 0.1, termSaturation: 2, termSi
let ready;
const load = () =>
(ready ??= new Promise((resolve, reject) => {
const { bundle, baseUrl, merge } = dialog.dataset;
const { bundle, baseUrl, merge, mergeBaseUrl } = dialog.dataset;
const css = document.createElement('link');
css.rel = 'stylesheet';
css.href = `${bundle}pagefind-ui.css`;
Expand All @@ -27,7 +27,7 @@ const load = () =>
bundlePath: bundle,
baseUrl,
// Website pages count half, so a short page doesn't outrank the docs.
mergeIndex: merge ? [{ bundlePath: merge, baseUrl: '/', indexWeight: 0.5, ranking }] : [],
mergeIndex: merge ? [{ bundlePath: merge, baseUrl: mergeBaseUrl, indexWeight: 0.5, ranking }] : [],
showSubResults: true,
showImages: false,
ranking,
Expand Down
13 changes: 9 additions & 4 deletions theme/site-bar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,33 @@ const CLOSE = `<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M6 6l12 12M1

/**
* @param {object} opts
* @param {string} [opts.root] path the website is served under, '/' unless it
* shares a host with other sites (GitHub Pages: '/<repo>/'); prefixes the
* site links in nav.json, which are written from the website root
* @param {string} [opts.current] href of the current site link (e.g. '/docs/'), marked as the current page
* @param {{ bundlePath: string, baseUrl: string, merge?: string }} opts.search
* the Pagefind index to search, and optionally a second index to merge
* (the website pages' /pagefind-website/)
* (the website pages' <root>pagefind-website/, with URLs under root)
*/
export function siteBar({ current, search }) {
export function siteBar({ root = '/', current, search }) {
const at = (href) => root + href.replace(/^\//, '');
const links = nav.links
.map(
(l) =>
`<a href="${esc(l.href)}" class="site-bar__link"${l.href === current ? ' aria-current="page"' : ''}>${esc(l.label)}</a>`,
`<a href="${esc(at(l.href))}" class="site-bar__link"${at(l.href) === current ? ' aria-current="page"' : ''}>${esc(l.label)}</a>`,
)
.join('');
const data = [
['bundle', search.bundlePath],
['base-url', search.baseUrl],
['merge', search.merge],
['merge-base-url', search.merge && root],
]
.filter(([, v]) => v)
.map(([k, v]) => ` data-${k}="${esc(v)}"`)
.join('');
return `<div class="site-bar">
<a href="/" class="site-bar__wordmark" translate="no">${WORDMARK}<span>OpenShell</span></a>
<a href="${esc(root)}" class="site-bar__wordmark" translate="no">${WORDMARK}<span>OpenShell</span></a>
<nav class="site-bar__links" aria-label="Site">${links}</nav>
<button type="button" class="search-button" data-search-open>${SEARCH}<span class="search-button__label">Search</span><kbd class="search-button__kbd">Ctrl K</kbd></button>
<div class="site-bar__end">
Expand Down
Loading