From ee1eeb557a5a1414dc6076152958b410dc84746c Mon Sep 17 00:00:00 2001 From: charlesLoder Date: Wed, 18 Dec 2024 22:50:52 -0500 Subject: [PATCH] Update documentation site --- docs/astro.config.mjs | 12 ++++-- docs/prepend_base_path.js | 76 +++++++++++++++++++++++++++++++++ docs/src/content/docs/index.mdx | 4 +- 3 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 docs/prepend_base_path.js diff --git a/docs/astro.config.mjs b/docs/astro.config.mjs index c0efb96..ab229fe 100644 --- a/docs/astro.config.mjs +++ b/docs/astro.config.mjs @@ -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({ @@ -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 || ""}`, @@ -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, diff --git a/docs/prepend_base_path.js b/docs/prepend_base_path.js new file mode 100644 index 0000000..fa0ae09 --- /dev/null +++ b/docs/prepend_base_path.js @@ -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; + }); + } + }); + }; +} diff --git a/docs/src/content/docs/index.mdx b/docs/src/content/docs/index.mdx index b14e1d0..e0cd870 100644 --- a/docs/src/content/docs/index.mdx +++ b/docs/src/content/docs/index.mdx @@ -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 @@ -56,4 +56,4 @@ Syllabify Hebrew text ## Learn more -To learn more, see the [`Text`](/api/classes/text) class +