Rework rehype plugin and clean up changes

This commit is contained in:
Priere 2023-12-05 20:52:21 -05:00
parent 2c43fb2512
commit 018a45e2d4
8 changed files with 36 additions and 33 deletions

View file

@ -15,25 +15,34 @@ const site = `https://strudel.cc/`; // root url without a path
const base = '/'; // base path of the strudel site const base = '/'; // base path of the strudel site
const baseNoTrailing = base.endsWith('/') ? base.slice(0, -1) : base; const baseNoTrailing = base.endsWith('/') ? base.slice(0, -1) : base;
// this rehype plugin converts relative anchor links to absolute ones // this rehype plugin fixes relative links
// it wokrs by prepending the absolute page path to anchor links // it works by prepending the base + page path to anchor links
// example: #gain -> /learn/effects/#gain // and by prepending the base path to all other relative links
// this is necessary when using a base href like <base href={base} /> // this is necessary when using a base href like <base href={base} />
// in this setup, relative anchor links will always link to base, instead of the current page // examples with base as "mybase":
function absoluteAnchors() { // #gain -> /mybase/learn/effects/#gain
// /some/page -> /mybase/some/page
function relativeURLFix() {
return (tree, file) => { return (tree, file) => {
const chunks = file.history[0].split('/src/pages/'); // file.history[0] is the file path const chunks = file.history[0].split('/src/pages/'); // file.history[0] is the file path
const path = chunks[chunks.length - 1].slice(0, -4); // only path inside src/pages, without .mdx const path = chunks[chunks.length - 1].slice(0, -4); // only path inside src/pages, without .mdx
return rehypeUrls((url) => { return rehypeUrls((url) => {
if (url.href.startsWith('/')) { // NOTE: the base argument to the URL constructor is ignored if the input is already
const hrefWithTrailingSlash = url.href.endsWith('/') ? url.href : url.href + '/'; // absolute and used if not, which facilitates the comparison below
return baseNoTrailing + hrefWithTrailingSlash; // true if url is relative
if (new URL(site).origin === new URL(url.href, site).origin) {
let newHref = baseNoTrailing;
if (url.href.startsWith('#')) {
// special case: a relative anchor link to the current page
newHref += `/${path}/${url.href}`;
} else {
// any other relative link
// NOTE: this does strip off serialized queries and fragments
newHref += (url.pathname.endsWith('/') ? url.pathname : url.pathname + '/');
}
// console.log(url.href + ' -> ', newHref);
return newHref;
} }
if (url.href.startsWith('#')) {
const absoluteUrl = `${baseNoTrailing}/${path}/${url.href}`;
return absoluteUrl;
}
// console.log(url.href + ' -> ', absoluteUrl);
return; return;
})(tree); })(tree);
}; };
@ -44,7 +53,7 @@ const options = {
remarkToc, remarkToc,
// E.g. `remark-frontmatter` // E.g. `remark-frontmatter`
], ],
rehypePlugins: [rehypeSlug, [rehypeAutolinkHeadings, { behavior: 'append' }], absoluteAnchors], rehypePlugins: [rehypeSlug, [rehypeAutolinkHeadings, { behavior: 'append' }], relativeURLFix],
}; };
// https://astro.build/config // https://astro.build/config

View file

@ -3,8 +3,7 @@ import { pwaInfo } from 'virtual:pwa-info';
import '../styles/index.css'; import '../styles/index.css';
const { BASE_URL } = import.meta.env; const { BASE_URL } = import.meta.env;
const base = BASE_URL; const baseNoTrailing = BASE_URL.endsWith('/') ? BASE_URL.slice(0, -1) : BASE_URL;
const baseNoTrailing = base.endsWith('/') ? base.slice(0, -1) : base;
--- ---
<!-- Global Metadata --> <!-- Global Metadata -->
@ -22,7 +21,7 @@ const baseNoTrailing = base.endsWith('/') ? base.slice(0, -1) : base;
<link rel="apple-touch-icon" href={`${baseNoTrailing}/icons/apple-icon-180.png`} sizes="180x180" /> <link rel="apple-touch-icon" href={`${baseNoTrailing}/icons/apple-icon-180.png`} sizes="180x180" />
<meta name="theme-color" content="#222222" /> <meta name="theme-color" content="#222222" />
<base href={base} /> <base href={BASE_URL} />
<!-- Scrollable a11y code helper --> <!-- Scrollable a11y code helper -->
<script src{`${baseNoTrailing}/make-scrollable-code-focusable.js`} is:inline></script> <script src{`${baseNoTrailing}/make-scrollable-code-focusable.js`} is:inline></script>

View file

@ -18,8 +18,7 @@ const langCode = 'en'; // getLanguageFromURL(currentPage);
const sidebar = SIDEBAR[langCode]; const sidebar = SIDEBAR[langCode];
const { BASE_URL } = import.meta.env; const { BASE_URL } = import.meta.env;
const base = BASE_URL; const baseNoTrailing = BASE_URL.endsWith('/') ? BASE_URL.slice(0, -1) : BASE_URL;
const baseNoTrailing = base.endsWith('/') ? base.slice(0, -1) : base;
--- ---
<nav <nav
@ -27,7 +26,7 @@ const baseNoTrailing = base.endsWith('/') ? base.slice(0, -1) : base;
title="Top Navigation" title="Top Navigation"
> >
<div class="flex overflow-visible items-center grow" style="overflow:visible"> <div class="flex overflow-visible items-center grow" style="overflow:visible">
<a href="/" class="flex items-center text-2xl space-x-2"> <a href={`${baseNoTrailing}/`} class="flex items-center text-2xl space-x-2">
<h1 class="font-bold flex space-x-2 items-baseline text-xl"> <h1 class="font-bold flex space-x-2 items-baseline text-xl">
<span>🌀</span> <span>🌀</span>
<div class="flex space-x-1 items-baseline"> <div class="flex space-x-1 items-baseline">

View file

@ -7,8 +7,8 @@ import HeadCommon from '../../components/HeadCommon.astro';
const myPatterns = await getMyPatterns(); const myPatterns = await getMyPatterns();
const { BASE_URL } = import.meta.env; const { BASE_URL } = import.meta.env;
const base = BASE_URL; const baseNoTrailing = BASE_URL.endsWith('/') ? BASE_URL.slice(0, -1) : BASE_URL;
const baseNoTrailing = base.endsWith('/') ? base.slice(0, -1) : base;--- ---
<head> <head>
<HeadCommon /> <HeadCommon />
@ -31,7 +31,7 @@ const baseNoTrailing = base.endsWith('/') ? base.slice(0, -1) : base;---
<div class="absolute w-full h-full flex justify-center items-center"> <div class="absolute w-full h-full flex justify-center items-center">
<span class="bg-slate-800 p-2 rounded-md text-white">{name}</span> <span class="bg-slate-800 p-2 rounded-md text-white">{name}</span>
</div> </div>
<img src={`${baseNoTrailing}/swatch/${name}.png`} /> <img src={`${baseNoTrailing}/swatch/${name}.png/`} />
</a> </a>
)) ))
} }

View file

@ -7,7 +7,9 @@ import { MiniRepl } from '../../docs/MiniRepl';
# Welcome # Welcome
<img src="/icons/strudel_icon.png" className="w-32 animate-pulse md:float-right ml-8" /> <div className="w-32 animate-pulse md:float-right ml-8">
![Strudel Icon](/icons/strudel_icon.png)
</div>
Welcome to the Strudel documentation pages! Welcome to the Strudel documentation pages!
You've come to the right place if you want to learn how to make music with code. You've come to the right place if you want to learn how to make music with code.

View file

@ -14,8 +14,7 @@ import { FilesTab } from './FilesTab';
const TAURI = window.__TAURI__; const TAURI = window.__TAURI__;
const { BASE_URL } = import.meta.env; const { BASE_URL } = import.meta.env;
const base = BASE_URL; const baseNoTrailing = BASE_URL.endsWith('/') ? BASE_URL.slice(0, -1) : BASE_URL;
const baseNoTrailing = base.endsWith('/') ? base.slice(0, -1) : base;
export function Footer({ context }) { export function Footer({ context }) {
const footerContent = useRef(); const footerContent = useRef();
@ -158,7 +157,7 @@ function WelcomeTab() {
</p> </p>
<p> <p>
To learn more about what this all means, check out the{' '} To learn more about what this all means, check out the{' '}
<a href={`${baseNoTrailing}/workshop/getting-started`} target="_blank"> <a href={`${baseNoTrailing}/workshop/getting-started/`} target="_blank">
interactive tutorial interactive tutorial
</a> </a>
. Also feel free to join the{' '} . Also feel free to join the{' '}

View file

@ -12,10 +12,6 @@ import './Repl.css';
const { BASE_URL } = import.meta.env; const { BASE_URL } = import.meta.env;
const baseNoTrailing = BASE_URL.endsWith('/') ? BASE_URL.slice(0, -1) : BASE_URL; const baseNoTrailing = BASE_URL.endsWith('/') ? BASE_URL.slice(0, -1) : BASE_URL;
const { BASE_URL } = import.meta.env;
const base = BASE_URL;
const baseNoTrailing = base.endsWith('/') ? base.slice(0, -1) : base;
export function Header({ context }) { export function Header({ context }) {
const { const {
embedded, embedded,

View file

@ -4,8 +4,7 @@ import './piano.mjs';
import './files.mjs'; import './files.mjs';
const { BASE_URL } = import.meta.env; const { BASE_URL } = import.meta.env;
const base = BASE_URL; const baseNoTrailing = BASE_URL.endsWith('/') ? BASE_URL.slice(0, -1) : BASE_URL;
const baseNoTrailing = base.endsWith('/') ? base.slice(0, -1) : base;
export async function prebake() { export async function prebake() {
// https://archive.org/details/SalamanderGrandPianoV3 // https://archive.org/details/SalamanderGrandPianoV3