Skip to content

Commit

Permalink
Merge pull request #189 from charlesLoder/fix-base-path
Browse files Browse the repository at this point in the history
Update documentation site
  • Loading branch information
charlesLoder authored Dec 19, 2024
2 parents e8c178f + ee1eeb5 commit 6dfd5e1
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
12 changes: 9 additions & 3 deletions docs/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import starlight from "@astrojs/starlight";
import { defineConfig } from "astro/config";
import starlightTypeDoc, { typeDocSidebarGroup } from "starlight-typedoc";
import { remarkBasePath } from "./prepend_base_path.js";

const basePath = process.env.NODE_ENV === "production" ? "/havarotjs" : "";

// https://astro.build/config
export default defineConfig({
Expand All @@ -10,7 +13,10 @@ export default defineConfig({
build: {
assets: "assets"
},
base: process.env.NODE_ENV === "production" ? "havarotjs" : "",
base: basePath,
markdown: {
remarkPlugins: [[remarkBasePath, { base: basePath }]]
},
integrations: [
starlight({
title: `havarotjs v${process.env.npm_package_version || ""}`,
Expand All @@ -30,11 +36,11 @@ export default defineConfig({
sidebar: [
{
label: "Getting started",
link: "/"
link: `/`
},
{
label: "Changelog",
link: "/changelog"
link: `/changelog`
},
// Add the generated sidebar group to the sidebar.
typeDocSidebarGroup,
Expand Down
76 changes: 76 additions & 0 deletions docs/prepend_base_path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// @ts-check
import { CONTINUE, visit } from "unist-util-visit";

/**
* Check if url is absolute
*
* @param {string} url
* @returns {boolean}
*/
function isAbsoluteUrl(url) {
return url.startsWith("http://") || url.startsWith("https://");
}

/**
* Check if url is a heading link
*
* @param {string} url
*/
function isHeadingLink(url) {
return url.startsWith("#");
}

/**
* Check if node is a link node
*
*/
function isLinkNode(node) {
return node.type === "link" && !!node.url;
}

/**
* Check if url starts with base path
*
* @param {string} str
* @param {string} base
*/
function startsWithBasePath(str, base) {
return str.startsWith(base);
}

export function remarkBasePath(options = {}) {
const { base = "" } = options;

return (tree) => {
visit(tree, (node) => {
// Handle link nodes
if (isLinkNode(node)) {
if (!isAbsoluteUrl(node.url) && !isHeadingLink(node.url) && !startsWithBasePath(node.url, base)) {
node.url = `${base}${node.url}`;
}
return CONTINUE;
}

const hasAttrs = !!node.attributes && Array.isArray(node.attributes);
if (!hasAttrs) {
return;
}
const hasHref = node?.attributes?.find((a) => a.name === "href");

if (hasHref) {
node.attributes = node.attributes.map((a) => {
if (
a.name === "href" &&
!isAbsoluteUrl(a.value) &&
!isHeadingLink(a.value) &&
!startsWithBasePath(a.value, base)
) {
a.value = `${base}${a.value}`;
return a;
}
return a;
});
}
});
};
}
4 changes: 2 additions & 2 deletions docs/src/content/docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Getting Started
next: false
---

import { Code, TabItem, Tabs } from "@astrojs/starlight/components";
import { Code, TabItem, Tabs, LinkCard } from "@astrojs/starlight/components";

Syllabify Hebrew text

Expand Down Expand Up @@ -56,4 +56,4 @@ Syllabify Hebrew text

## Learn more

To learn more, see the [`Text`](/api/classes/text) class
<LinkCard title="Text" href="/api/classes/text/" description="To learn more see the <code>Text</code> class" />

0 comments on commit 6dfd5e1

Please sign in to comment.