diff --git a/.changeset/calm-rats-remain.md b/.changeset/calm-rats-remain.md new file mode 100644 index 0000000..b6393b0 --- /dev/null +++ b/.changeset/calm-rats-remain.md @@ -0,0 +1,5 @@ +--- +'starlight-obsidian': minor +--- + +Improves error message when the `output` directory configuration option is invalid. diff --git a/packages/starlight-obsidian/index.ts b/packages/starlight-obsidian/index.ts index f102143..ba825ef 100644 --- a/packages/starlight-obsidian/index.ts +++ b/packages/starlight-obsidian/index.ts @@ -6,6 +6,7 @@ import { z } from 'astro/zod' import { starlightObsidianIntegration } from './libs/integration' import { getObsidianPaths, getVault } from './libs/obsidian' +import { stripLeadingAndTrailingSlashes } from './libs/path' import { throwUserError } from './libs/plugin' import { addObsidianFiles, getSidebarFromConfig, getSidebarGroupPlaceholder, type SidebarGroup } from './libs/starlight' @@ -65,7 +66,18 @@ const starlightObsidianConfigSchema = z.object({ * * @default 'notes' */ - output: z.string().default('notes'), + output: z + .string() + .default('notes') + .refine( + (value) => { + const label = stripLeadingAndTrailingSlashes(value) + return label !== '' && label !== '.' && !label.startsWith('..') + }, + { + message: "The `output` directory cannot be empty, '.', or start with '..'.", + }, + ), /** * Whether the Starlight Obsidian plugin should skip the generation of the Obsidian vault pages. * diff --git a/packages/starlight-obsidian/libs/path.ts b/packages/starlight-obsidian/libs/path.ts index 96d53b0..bb985ad 100644 --- a/packages/starlight-obsidian/libs/path.ts +++ b/packages/starlight-obsidian/libs/path.ts @@ -52,3 +52,17 @@ export function slashify(filePath: string) { export function osPath(filePath: string) { return filePath.replaceAll('/', path.sep) } + +function stripLeadingSlash(href: string) { + if (href.startsWith('/')) href = href.slice(1) + return href +} + +function stripTrailingSlash(href: string) { + if (href.endsWith('/')) href = href.slice(0, -1) + return href +} + +export function stripLeadingAndTrailingSlashes(href: string): string { + return stripTrailingSlash(stripLeadingSlash(href)) +}