diff --git a/.github/workflows/site.yml b/.github/workflows/site.yml index 70a3ff9..242cb25 100644 --- a/.github/workflows/site.yml +++ b/.github/workflows/site.yml @@ -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 //: the website pages at // and the docs at +# //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 @@ -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' diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 0051ab7..465d6ac 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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 `//` -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 +`//`: the website pages at `//` and the docs at +`//docs/`. It builds the docs with `BASE_PATH=//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 diff --git a/docs/src/components/overrides/Header.astro b/docs/src/components/overrides/Header.astro index 46cfd4a..46fda3f 100644 --- a/docs/src/components/overrides/Header.astro +++ b/docs/src/components/overrides/Header.astro @@ -10,14 +10,17 @@ import { siteBar } from '../../../../theme/site-bar.mjs'; const base = import.meta.env.BASE_URL; +// The website serves the docs at 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, }, }); diff --git a/scripts/website.mjs b/scripts/website.mjs index 7d03561..5301bdb 100644 --- a/scripts/website.mjs +++ b/scripts/website.mjs @@ -5,7 +5,9 @@ // into the landing page's shell (header, footer, scripts) at //, with // its Markdown copy at /.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=docs/ so its links resolve under +// docs/. The root is / on Netlify and // 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'; @@ -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=${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, { @@ -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 (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, '&').replace(//g, '>').replace(/"/g, '"'); @@ -56,15 +61,16 @@ const withNav = (html, current) => html, //, siteBar({ + root, current, - search: { bundlePath: `/${MOUNT}/pagefind/`, baseUrl: `/${MOUNT}/`, merge: '/pagefind-website/' }, + search: { bundlePath: `${base}pagefind/`, baseUrl: base, merge: `${root}pagefind-website/` }, }), ' 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); @@ -111,12 +117,13 @@ for (const file of pages) { ``, 'Markdown alternate link', ); - html = withNav(swap(html, /
[\s\S]*<\/main>/, main, '
'), `/${name}/`); + html = withNav(rooted(swap(html, /
[\s\S]*<\/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. @@ -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', '', @@ -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 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})`, ); diff --git a/theme/search.js b/theme/search.js index 5dd7424..72afb9b 100644 --- a/theme/search.js +++ b/theme/search.js @@ -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 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'); @@ -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`; @@ -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, diff --git a/theme/site-bar.mjs b/theme/site-bar.mjs index bb1a84e..c2bd277 100644 --- a/theme/site-bar.mjs +++ b/theme/site-bar.mjs @@ -18,28 +18,33 @@ const CLOSE = `
-${WORDMARK}OpenShell +${WORDMARK}OpenShell