Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/calm-rats-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'starlight-obsidian': minor
---

Improves error message when the `output` directory configuration option is invalid.
14 changes: 13 additions & 1 deletion packages/starlight-obsidian/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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.
*
Expand Down
14 changes: 14 additions & 0 deletions packages/starlight-obsidian/libs/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}