diff --git a/quartz/components/GameCultGraphSpaShell.tsx b/quartz/components/GameCultGraphSpaShell.tsx index a54cb41..6c5494b 100644 --- a/quartz/components/GameCultGraphSpaShell.tsx +++ b/quartz/components/GameCultGraphSpaShell.tsx @@ -4,6 +4,13 @@ export type GameCultGraphSpaShellOptions = { stylesheetHref: string moduleSrc: string rootClassName?: string + config?: { + title?: string + architectureDescription?: string + allowedSlugPrefixes?: string[] + blockedSlugPrefixes?: string[] + blockedPathSegments?: string[] + } } export default ((options?: Partial) => { @@ -18,6 +25,13 @@ export default ((options?: Partial) => { <>
+ {shellOptions.config ? ( + ) diff --git a/quartz/graph-spa/src/main.tsx b/quartz/graph-spa/src/main.tsx index 5affb30..547c252 100644 --- a/quartz/graph-spa/src/main.tsx +++ b/quartz/graph-spa/src/main.tsx @@ -23,6 +23,14 @@ type QuartzContentEntry = { type QuartzContentIndex = Record +type GraphSpaConfig = { + title?: string + architectureDescription?: string + allowedSlugPrefixes?: string[] + blockedSlugPrefixes?: string[] + blockedPathSegments?: string[] +} + type ArticleState = | { status: "idle" @@ -45,9 +53,19 @@ type ArticleState = declare global { interface Window { fetchData?: Promise + gameCultGraphSpaConfig?: GraphSpaConfig } } +const DEFAULT_BLOCKED_SLUG_PREFIXES = ["Inspirations/", "GameCult-Quartz/"] +const DEFAULT_BLOCKED_PATH_SEGMENTS = [ + "/.git/", + "/node_modules/", + "node_modules/", + "/quartz-site/", + "/site/quartz/", +] + type GraphErrorBoundaryState = { error: Error | null } @@ -105,6 +123,42 @@ function normalizeSlug(slug: string) { return slug } +function graphSpaConfig() { + return window.gameCultGraphSpaConfig ?? {} +} + +function slugMatchesPrefix(slug: string, prefix: string) { + const normalizedPrefix = normalizeSlug(prefix.replace(/\/$/, "")) + + return slug === normalizedPrefix || slug.startsWith(`${normalizedPrefix}/`) +} + +function isAllowedGraphSlug(slug: string, entry: QuartzContentEntry, config = graphSpaConfig()) { + const normalizedSlug = normalizeSlug(slug) + const blockedPrefixes = [...DEFAULT_BLOCKED_SLUG_PREFIXES, ...(config.blockedSlugPrefixes ?? [])] + + if (blockedPrefixes.some((prefix) => slugMatchesPrefix(normalizedSlug, prefix))) { + return false + } + + const filePath = (entry.filePath ?? "").replace(/\\/g, "/") + const blockedPathSegments = [ + ...DEFAULT_BLOCKED_PATH_SEGMENTS, + ...(config.blockedPathSegments ?? []), + ] + + if (blockedPathSegments.some((segment) => filePath.includes(segment))) { + return false + } + + const allowedPrefixes = config.allowedSlugPrefixes ?? [] + + return ( + allowedPrefixes.length === 0 || + allowedPrefixes.some((prefix) => slugMatchesPrefix(normalizedSlug, prefix)) + ) +} + function sectionForSlug(slug: string) { return slug.includes("/") ? slug.split("/")[0] : "Root" } @@ -267,7 +321,7 @@ function buildGraphState(contentIndex: QuartzContentIndex): EpiphanyGraphsState return [normalizedSlug, { ...entry, slug: normalizedSlug }] as [string, QuartzContentEntry] }) - .filter(([slug]) => !slug.startsWith("Inspirations/")) + .filter(([slug, entry]) => isAllowedGraphSlug(slug, entry)) .sort(([left], [right]) => left.localeCompare(right)) const entryBySlug = new Map(entries) @@ -355,10 +409,15 @@ async function loadGraphState() { : await fetch("/static/contentIndex.json").then( (response) => response.json() as Promise, ) + const filteredContentIndex = Object.fromEntries( + Object.entries(contentIndex).filter(([slug, entry]) => + isAllowedGraphSlug(normalizeSlug(entry.slug || slug), entry), + ), + ) return { - contentIndex, - graphState: buildGraphState(contentIndex), + contentIndex: filteredContentIndex, + graphState: buildGraphState(filteredContentIndex), } } @@ -573,7 +632,7 @@ function App() {