Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update documentation site #189

Merged
merged 1 commit into from
Dec 19, 2024
Merged
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
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" />
Loading