improve slashing + base href behavior
+ prefix sidebar links & mdx links with base + add trailing slashes
This commit is contained in:
parent
8a0cf14fd7
commit
ac41426a20
4 changed files with 15 additions and 8 deletions
|
|
@ -13,6 +13,7 @@ import AstroPWA from '@vite-pwa/astro';
|
|||
|
||||
const site = `https://strudel.cc/`; // root url without a path
|
||||
const base = '/'; // base path of the strudel site
|
||||
const baseNoTrailing = base.endsWith('/') ? base.slice(0, -1) : base;
|
||||
|
||||
// this rehype plugin converts relative anchor links to absolute ones
|
||||
// it wokrs by prepending the absolute page path to anchor links
|
||||
|
|
@ -24,13 +25,16 @@ function absoluteAnchors() {
|
|||
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
|
||||
return rehypeUrls((url) => {
|
||||
if (!url.href.startsWith('#')) {
|
||||
return;
|
||||
if (url.href.startsWith('/')) {
|
||||
const hrefWithTrailingSlash = url.href.endsWith('/') ? url.href : url.href + '/';
|
||||
return baseNoTrailing + hrefWithTrailingSlash;
|
||||
}
|
||||
if (url.href.startsWith('#')) {
|
||||
const absoluteUrl = `${baseNoTrailing}/${path}/${url.href}`;
|
||||
return absoluteUrl;
|
||||
}
|
||||
const baseWithSlash = base.endsWith('/') ? base : base + '/';
|
||||
const absoluteUrl = baseWithSlash + path + url.href;
|
||||
// console.log(url.href + ' -> ', absoluteUrl);
|
||||
return absoluteUrl;
|
||||
return;
|
||||
})(tree);
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue