Skip to content

Commit

Permalink
feat(core): added support for base url and slashes (#584)
Browse files Browse the repository at this point in the history
* add support for base url and slashes

* add support for base url and slashes

* Create gentle-ravens-compare.md

* add support for base url and slashes

* add support for base url and slashes

---------

Co-authored-by: David Boyne <[email protected]>
  • Loading branch information
boyney123 and David Boyne authored Jul 12, 2024
1 parent f9edeac commit 9d61581
Show file tree
Hide file tree
Showing 17 changed files with 72 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .changeset/gentle-ravens-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@eventcatalog/core": patch
---

feat(core): added support for base url and slashes
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ eventcatalog.styles.css

.vscode/*

git-push.sh
3 changes: 2 additions & 1 deletion astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import remarkGFM from 'remark-gfm';
import pagefind from "astro-pagefind";
import { mermaid } from "./src/remark-plugins/mermaid"


import expressiveCode from 'astro-expressive-code';
import config from './eventcatalog.config';

// https://astro.build/config
export default defineConfig({
base: '/',
base: config.base || '/',
server: { port: 3000 },

// https://docs.astro.build/en/reference/configuration-reference/#site
Expand Down
1 change: 1 addition & 0 deletions bin/eventcatalog.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface Config {
organizationName: string;
homepageLink: string;
editUrl: string;
base?: string;
logo: {
alt: string;
src: string;
Expand Down
3 changes: 2 additions & 1 deletion examples/default/eventcatalog.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export default {
title: 'EventCatalog',
trailingSlash: false,
tagline: 'Discover, Explore and Document your Event Driven Architectures',
organizationName: 'Your Company',
homepageLink: 'https://eventcatalog.dev/',
Expand All @@ -9,6 +8,8 @@ export default {
alt: 'EventCatalog Logo',
src: 'logo.svg'
},
base: '/',
trailingSlash: false,
docs: {
sidebar: {
// Should the sub heading be rendered in the docs sidebar?
Expand Down
3 changes: 2 additions & 1 deletion src/components/DocsNavigation.astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getServices } from '@utils/services/services';
import { getTeams } from '@utils/teams';
import { getUsers } from '@utils/users';
import config, { type CatalogConfig } from '@eventcatalog';
import { buildUrl } from '@utils/url-builder';
const events = await getEvents({ getAllVersions: false });
const commands = await getCommands({ getAllVersions: false });
Expand Down Expand Up @@ -59,7 +60,7 @@ const sideNav = withHeadings.reduce((acc, item) => {
items: item.collection === 'users' ? [] : item.headings,
visible: visibleCollections[item.collection],
// @ts-ignore
href: item.data.version ? `/docs/${item.collection}/${item.data.id}/${item.data.version}` : `/docs/${item.collection}/${item.data.id}`,
href: item.data.version ? buildUrl(`/docs/${item.collection}/${item.data.id}/${item.data.version}`) : buildUrl(`/docs/${item.collection}/${item.data.id}`),
};
group.push(navigationItem);
Expand Down
12 changes: 6 additions & 6 deletions src/components/Header.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@
import catalog from '@eventcatalog';
// import Search from 'astro-pagefind/components/Search';
import Search from '@components/Search.astro';
import { buildUrl } from '@utils/url-builder';
const currentPath = Astro.url.pathname;
const navItems = [
{
label: 'Docs',
href: '/docs',
href: buildUrl('/docs'),
},
{
label: 'Visualiser',
href: '/visualiser',
href: buildUrl('/visualiser'),
},
{
label: 'Discover',
href: '/discover/events',
href: buildUrl('/discover/events'),
}
];
---

<nav class="md:fixed top-0 w-full z-20 bg-white border-b border-gray-200 py-4 font-bold text-xl max-w-[70em]">
<div class="flex justify-between items-center">
<div class="w-1/3 flex space-x-2 items-center">
<a href="/docs" class="flex space-x-2 items-center">
<img src="/logo.png" class="w-8" />
<a href={buildUrl('/docs')} class="flex space-x-2 items-center">
<img src={buildUrl('/logo.png', true)} class="w-8" />
<span class="hidden sm:inline-block text-[1em]">{catalog.title}</span>
</a>
</div>
Expand Down
5 changes: 3 additions & 2 deletions src/components/MDX/NodeGraph/NodeGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { CollectionEntry } from 'astro:content';
import { navigate } from 'astro:transitions/client';
import type { CollectionTypes } from '@types';
import DownloadButton from './DownloadButton';
import { buildUrl } from '@utils/url-builder';

interface Props {
nodes: any;
Expand All @@ -21,10 +22,10 @@ interface Props {
}

const getDocUrlForCollection = (collectionItem: CollectionEntry<CollectionTypes>) => {
return `/docs/${collectionItem.collection}/${collectionItem.data.id}/${collectionItem.data.version}`;
return buildUrl(`/docs/${collectionItem.collection}/${collectionItem.data.id}/${collectionItem.data.version}`);
};
const getVisualiserUrlForCollection = (collectionItem: CollectionEntry<CollectionTypes>) => {
return `/visualiser/${collectionItem.collection}/${collectionItem.data.id}/${collectionItem.data.version}`;
return buildUrl(`/visualiser/${collectionItem.collection}/${collectionItem.data.id}/${collectionItem.data.version}`);
};

// const NodeGraphBuilder = ({ title, subtitle, includeBackground = true, includeControls = true }: Props) => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Seo.astro
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ if (typeof _image === 'string') {
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="viewport" content="width=device-width" />
<link rel="icon" type="image/svg+xml" href="/favicon.ico" />
<link rel="icon" type="image/svg+xml" href={`${import.meta.env.BASE_URL}/favicon.ico`} />

<SEO
title={title}
Expand Down
7 changes: 4 additions & 3 deletions src/components/Tables/columns/DomainTableColumns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { RectangleGroupIcon } from '@heroicons/react/20/solid';
import { createColumnHelper } from '@tanstack/react-table';
import type { CollectionEntry } from 'astro:content';
import { filterByName, filterCollectionByName } from '../filters/custom-filters';
import { buildUrl } from '@utils/url-builder';

const columnHelper = createColumnHelper<CollectionEntry<'domains'>>();

Expand All @@ -16,7 +17,7 @@ export const columns = () => [
return (
<div className=" group ">
<a
href={`/docs/${messageRaw.collection}/${messageRaw.data.id}/${messageRaw.data.version}`}
href={buildUrl(`/docs/${messageRaw.collection}/${messageRaw.data.id}/${messageRaw.data.version}`)}
className={`group-hover:text-${color}-500 flex space-x-1 items-center`}
>
<div className={`flex items-center border border-gray-300 shadow-sm rounded-md group-hover:border-${color}-400`}>
Expand Down Expand Up @@ -74,7 +75,7 @@ export const columns = () => [
return (
<li key={consumer.data.id} className="py-1 group ">
<a
href={`/docs/${consumer.collection}/${consumer.data.id}/${consumer.data.version}`}
href={buildUrl(`/docs/${consumer.collection}/${consumer.data.id}/${consumer.data.version}`)}
className="group-hover:text-purple-500 flex space-x-1 items-center "
>
<div className={`flex items-center border border-gray-300 rounded-md`}>
Expand Down Expand Up @@ -104,7 +105,7 @@ export const columns = () => [
return (
<a
className="hover:text-purple-500 hover:underline px-4 font-light"
href={`/visualiser/${domain.collection}/${domain.data.id}/${domain.data.version}`}
href={buildUrl(`/visualiser/${domain.collection}/${domain.data.id}/${domain.data.version}`)}
>
Visualiser &rarr;
</a>
Expand Down
9 changes: 5 additions & 4 deletions src/components/Tables/columns/MessageTableColumns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { CollectionMessageTypes } from '@types';
import type { CollectionEntry } from 'astro:content';
import { useMemo } from 'react';
import { filterByName, filterCollectionByName } from '../filters/custom-filters';
import { buildUrl } from '@utils/url-builder';

const columnHelper = createColumnHelper<CollectionEntry<CollectionMessageTypes>>();

Expand All @@ -19,7 +20,7 @@ export const columns = () => [
return (
<div className=" group ">
<a
href={`/docs/${messageRaw.collection}/${messageRaw.data.id}/${messageRaw.data.version}`}
href={buildUrl(`/docs/${messageRaw.collection}/${messageRaw.data.id}/${messageRaw.data.version}`)}
className={`group-hover:text-${color}-500 flex space-x-1 items-center`}
>
<div className={`flex items-center border border-gray-300 shadow-sm rounded-md group-hover:border-${color}-400`}>
Expand Down Expand Up @@ -75,7 +76,7 @@ export const columns = () => [
return (
<li className="py-2 group flex items-center space-x-2" key={producer.data.id}>
<a
href={`/docs/${producer.collection}/${producer.data.id}/${producer.data.version}`}
href={buildUrl(`/docs/${producer.collection}/${producer.data.id}/${producer.data.version}`)}
className="group-hover:text-purple-500 flex space-x-1 items-center "
>
<div className="flex items-center border border-gray-300 shadow-sm rounded-md">
Expand Down Expand Up @@ -115,7 +116,7 @@ export const columns = () => [
return (
<li key={consumer.data.id} className="py-1 group font-light ">
<a
href={`/docs/${consumer.collection}/${consumer.data.id}/${consumer.data.version}`}
href={buildUrl(`/docs/${consumer.collection}/${consumer.data.id}/${consumer.data.version}`)}
className="group-hover:text-purple-500 flex space-x-1 items-center "
>
<div className="flex items-center border border-gray-300 shadow-sm rounded-md">
Expand Down Expand Up @@ -145,7 +146,7 @@ export const columns = () => [
return (
<a
className="hover:text-purple-500 hover:underline px-4 font-light"
href={`/visualiser/${domain.collection}/${domain.data.id}/${domain.data.version}`}
href={buildUrl(`/visualiser/${domain.collection}/${domain.data.id}/${domain.data.version}`)}
>
Visualiser &rarr;
</a>
Expand Down
9 changes: 5 additions & 4 deletions src/components/Tables/columns/ServiceTableColumns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createColumnHelper } from '@tanstack/react-table';
import type { CollectionEntry } from 'astro:content';
import { useMemo } from 'react';
import { filterByName, filterCollectionByName } from '../filters/custom-filters';
import { buildUrl } from '@utils/url-builder';

const columnHelper = createColumnHelper<CollectionEntry<'services'>>();

Expand All @@ -18,7 +19,7 @@ export const columns = () => [
return (
<div className="group font-light">
<a
href={`/docs/${messageRaw.collection}/${messageRaw.data.id}/${messageRaw.data.version}`}
href={buildUrl(`/docs/${messageRaw.collection}/${messageRaw.data.id}/${messageRaw.data.version}`)}
className={`group-hover:text-${color}-500 flex space-x-1 items-center`}
>
<div className={`flex items-center border border-gray-300 shadow-sm rounded-md group-hover:border-${color}-400`}>
Expand Down Expand Up @@ -76,7 +77,7 @@ export const columns = () => [
return (
<li key={consumer.data.id} className="py-1 group font-light ">
<a
href={`/docs/${consumer.collection}/${consumer.data.id}/${consumer.data.version}`}
href={buildUrl(`/docs/${consumer.collection}/${consumer.data.id}/${consumer.data.version}`)}
className="group-hover:text-purple-500 flex space-x-1 items-center "
>
<div className={`flex items-center border border-gray-300 shadow-sm rounded-md`}>
Expand Down Expand Up @@ -118,7 +119,7 @@ export const columns = () => [
return (
<li key={consumer.data.id} className="py-1 group font-light">
<a
href={`/docs/${consumer.collection}/${consumer.data.id}/${consumer.data.version}`}
href={buildUrl(`/docs/${consumer.collection}/${consumer.data.id}/${consumer.data.version}`)}
className="group-hover:text-purple-500 flex space-x-1 items-center "
>
<div className={`flex items-center border border-gray-300 shadow-sm rounded-md`}>
Expand Down Expand Up @@ -148,7 +149,7 @@ export const columns = () => [
return (
<a
className="hover:text-purple-500 hover:underline px-4 font-light"
href={`/visualiser/${domain.collection}/${domain.data.id}/${domain.data.version}`}
href={buildUrl(`/visualiser/${domain.collection}/${domain.data.id}/${domain.data.version}`)}
>
Visualiser &rarr;
</a>
Expand Down
2 changes: 1 addition & 1 deletion src/layouts/CustomDocsPageLayout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import catalog from '@eventcatalog';
<meta charset="UTF-8" />
<meta name="description" content="Astro description" />
<meta name="viewport" content="width=device-width" />
<link rel="icon" type="image/svg+xml" href="/favicon.ico" />
<link rel="icon" type="image/svg+xml" href={`${import.meta.env.BASE_URL}/favicon.ico`} />
<meta name="generator" content={Astro.generator} />
<title>EventCatalog</title>
</head>
Expand Down
9 changes: 5 additions & 4 deletions src/layouts/DiscoverLayout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getCommands } from '@utils/commands';
import { getDomains } from '@utils/domains/domains';
import { getEvents } from '@utils/events';
import { getServices } from '@utils/services/services';
import { buildUrl } from '@utils/url-builder';
const events = await getEvents();
const commands = await getCommands();
Expand All @@ -20,31 +21,31 @@ const currentPath = Astro.url.pathname;
const tabs = [
{
label: `Events (${events.length})`,
href: '/discover/events',
href: buildUrl('/discover/events'),
isActive: currentPath === '/discover/events',
icon: EnvelopeIcon,
activeColor: 'orange',
enabled: events.length > 0,
},
{
label: `Commands (${commands.length})`,
href: '/discover/commands',
href: buildUrl('/discover/commands'),
isActive: currentPath === '/discover/commands',
icon: EnvelopeIcon,
activeColor: 'blue',
enabled: commands.length > 0,
},
{
label: `Services (${services.length})`,
href: '/discover/services',
href: buildUrl('/discover/services'),
isActive: currentPath === '/discover/services',
icon: ServerIcon,
activeColor: 'pink',
enabled: services.length > 0,
},
{
label: `Domains (${domains.length})`,
href: '/discover/domains',
href: buildUrl('/discover/domains'),
isActive: currentPath === '/discover/domains',
icon: RectangleGroupIcon,
activeColor: 'yellow',
Expand Down
3 changes: 2 additions & 1 deletion src/layouts/VisualiserLayout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getCommands } from '@utils/commands';
import { getDomains } from '@utils/domains/domains';
import { getEvents } from '@utils/events';
import { getServices } from '@utils/services/services';
import { buildUrl } from '@utils/url-builder';
import type { CollectionEntry } from 'astro:content';
import { ViewTransitions } from 'astro:transitions';
Expand Down Expand Up @@ -55,7 +56,7 @@ const getColor = (collection: string) => {
label: item.data.name,
version: item.data.version,
color: getColor(item.collection),
href: `/visualiser/${item.collection}/${item.data.id}/${item.data.version}`,
href: buildUrl(`/visualiser/${item.collection}/${item.data.id}/${item.data.version}`),
active: isCurrent
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/pages/discover/[type]/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export async function getStaticPaths() {
const events = await getEvents();
const commands = await getCommands();
const services = await getServices();
const domains = await getDomains();
const domains = await getDomains();
const buildPages = (collection: CollectionEntry<CollectionTypes>[]) => {
return collection.map((item) => ({
Expand Down
26 changes: 26 additions & 0 deletions src/utils/url-builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import config from '@eventcatalog';

const cleanUrl = (url: string) => {
return url.replace(/\/+/g, '/');
};

// Custom URL builder as Astro does not support this stuff out the box
export const buildUrl = (url: string, ignoreTrailingSlash = false) => {
// Should a trailingSlash be added to urls?
const trailingSlash = config.trailingSlash || false;

let newUrl = url;

// If the base URL is not the root, we need to append it
if (import.meta.env.BASE_URL !== '/') {
newUrl = `${import.meta.env.BASE_URL}${url}`;
}

// Should we add a trailing slash to the url?
if (trailingSlash && !ignoreTrailingSlash) {
if (url.endsWith('/')) return newUrl;
return cleanUrl(`${newUrl}/`);
}

return cleanUrl(newUrl);
};

0 comments on commit 9d61581

Please sign in to comment.