# Obsidian

Publish an Obsidian vault as docs.

`@vuesho/obsidian` publishes an Obsidian vault as it is. The server reads the notes while the site runs, and renders Obsidian's wikilinks, embeds and callouts without converting the files.

## Setup [#setup]

### Install the Package [#install-the-package-step]

```npm
npm i @vuesho/obsidian
```

### Connect the Vault [#connect-the-vault-step]

Connect the vault in the config:

```ts title="vuesho.config.ts"
import { createObsidian, obsidianPlugin } from "@vuesho/obsidian";
import { createSite, recommendedPreset } from "vuesho/config";

const vault = createObsidian({ dir: "vault" }); // [!code highlight]

export default createSite(vault.source({ staleTime: 60_000 })).plugins(
  recommendedPreset(),
  obsidianPlugin(), // [!code highlight]
);
```

To use the notes next to the Markdown in `content/`, give their source a `baseDir`, as in [Multiple Collections](/markdown/collections#multiple-collections):

```ts title="vuesho.config.ts"
createSite({
  docs: docs.toSource(),
  vault: vault.source({ baseDir: "vault", staleTime: 60_000 }), // [!code highlight]
}).plugins(recommendedPreset(), obsidianPlugin());
```

A note's path in the vault sets its URL, and media files are served under `/obsidian-files`:

| File                      | URL                                       |
| ------------------------- | ----------------------------------------- |
| `index.md`                | `/`                                       |
| `notes/Deep Note.md`      | `/notes/Deep Note`                        |
| `attachments/diagram.png` | `/obsidian-files/attachments/diagram.png` |

`dir` follows the same rules as [`createLocalContent()`](/content-sources/local-files#reading-a-folder). On a host without a file system, like Cloudflare Workers, use a [static build](/deploying#static-hosting), which renders the notes when the site builds.

## Notes [#notes]

Notes support this Obsidian syntax:

| Syntax                                           | Renders as                          |
| ------------------------------------------------ | ----------------------------------- |
| `[[Note]]`, `[[Note#Heading\|text]]`             | A link to the note or its heading   |
| `![[image.png]]`                                 | The image                           |
| `![[Note]]`, `![[Note#Heading]]` on its own line | The note, or the heading's section  |
| `![[Note#^id]]` on its own line                  | The block or list item marked `^id` |
| `![[Note]]` in a line                            | A link to the note                  |
| `[text](Note.md#Heading)`, `![](image.png)`      | A link to the note, or the image    |
| `> [!warning] Title`                             | A callout                           |
| A paragraph ending with `^id`                    | A block that `^id` links to         |
| `%% comment %%`                                  | Nothing                             |

Links and aliases resolve as in Obsidian, and so do Markdown links without a scheme or a leading `/`. A link that resolves to nothing, HTML, and a code block of an unknown language, like `dataview`, render as text.

### Frontmatter [#frontmatter]

The frontmatter is read as leniently as Obsidian reads it, and a note without a `title` uses its file name.

`schema` validates the frontmatter with a [Standard Schema](https://standardschema.dev), and types `page.data`:

```ts title="vuesho.config.ts"
import { createObsidian } from "@vuesho/obsidian";
import { z } from "zod";

const vault = createObsidian({
  dir: "vault",
  // [!code highlight:4]
  schema: z.object({
    title: z.string().optional(),
    status: z.enum(["draft", "done"]),
  }),
});
```

An invalid note stops the server from starting, and fails the reload in development. On a running server, the server logs an error and keeps the note's last valid version, or leaves out a new note.

### Sidebar [#sidebar]

A `meta.json` or `meta.<lang>.json` in a vault folder orders the sidebar and names the folder, as in [Navigation](/navigation). `metaSchema` validates it. The vault's other data files, in `.json`, `.yaml`, `.yml` or `.toml`, are neither pages nor served.

### Rendering [#rendering]

The notes render with the Markdown elements of `vuesho/components` and Obsidian's callouts. To use other components, replace the `obsidian` body in your [theme](/theme#views-and-page-bodies). The body receives the note as a hast tree in `tree`, and `toVNodes()` from `vuesho/ui/hast` renders the tree with your components. `@vuesho/obsidian/ui` has the callouts.

The notes use only the `highlight` option of the config's `markdown`. `markdownItPlugins` adds [markdown-it plugins](/markdown#markdown-it-plugins), which run after Obsidian's syntax:

```ts title="vuesho.config.ts"
import { katex } from "@mdit/plugin-katex";
import { createObsidian } from "@vuesho/obsidian";

const vault = createObsidian({ dir: "vault", markdownItPlugins: [katex] }); // [!code highlight]
```

## Media [#media]

The vault's media files, like images, are served under `/obsidian-files`, or `files.path`, and copied into a static build. The route never serves notes, data files, hidden files like those of `.obsidian`, or files outside `include`.

An SVG file is sent with a `Content-Security-Policy` that blocks its scripts, as [Local Files](/content-sources/local-files#serving-files) shows.

<Callout type="warn" title="Private Files">
Every other file that `include` matches is public. To keep private files out, narrow `include`, like `["**/*.{md,mdx}", "attachments/**"]`.
</Callout>

`url` returns the URL of a media file served elsewhere, like a copy in `public/` or on a CDN. Return `undefined` to serve the file from the route:

```ts title="vuesho.config.ts"
const vault = createObsidian({ dir: "vault", url: (file) => `/vault/${file}` });
```

## Multiple Vaults [#multiple-vaults]

Give each vault a different `baseDir` and `files.path`:

```ts title="vuesho.config.ts"
import { createObsidian, obsidianPlugin } from "@vuesho/obsidian";

const work = createObsidian({ dir: "vaults/work", files: { path: "/work-files" } });
const personal = createObsidian({ dir: "vaults/personal", files: { path: "/personal-files" } });

createSite({
  work: work.source({ baseDir: "work" }),
  personal: personal.source({ baseDir: "personal" }),
}).plugins(recommendedPreset(), obsidianPlugin()); // [!code highlight]
```

## Refreshing Content [#refreshing-content]

In development, editing a note reloads the page. In production, the server reads the vault again once `staleTime` passes, or when a webhook calls `revalidateLoader()`, as in [Dynamic Rendering](/dynamic-rendering#refreshing-content).

## Options [#options]

### `createObsidian()` [#createobsidian]

### CreateObsidianOptions

| Prop                 | Type                                                        | Description                                                                                                                                                                                                                                                                        |
| -------------------- | ----------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `dir`                | `string`                                                    | The folder of the vault, relative to the root of the project. The build copies the files of it that the server reads into a built server's output, while a vault outside the project, given by its absolute path, is read where it is.                                             |
| `schema?`            | `StandardSchemaV1<unknown, unknown> \| undefined`           | Validates the frontmatter of each note, like a Zod object. A note without a `title` has the name of its file. Default: `the frontmatter as Obsidian tolerates it, with every field kept`                                                                                           |
| `metaSchema?`        | `StandardSchemaV1<unknown, unknown> \| undefined`           | Validates each meta file. Default: `` the fields of `metaSchema` ``                                                                                                                                                                                                                |
| `include?`           | `string[] \| undefined`                                     | Glob patterns of the files of the vault, relative to `dir`. Files and folders starting with `.` are left out. Default: `["**\/*"]`                                                                                                                                                 |
| `url?`               | `((vaultPath: string) => string \| undefined) \| undefined` | Gives the URL of a medium by its path from the vault, for media served elsewhere, like a copy of the vault in `public/` or a CDN. Return `undefined` to serve the medium from the route of the files. Default: `the route of the files`                                            |
| `markdownItPlugins?` | `MarkdownItPlugin[] \| undefined`                           | markdown-it plugins that the notes are parsed with, after the syntax of Obsidian, as `remarkPlugins` of `fumadocs-obsidian`. The Markdown of the config doesn't apply to the notes.                                                                                                |
| `files?`             | `{ path: string; } \| undefined`                            | The route that serves the media of the vault, like `![[diagram.png]]`, which are the files of `include` that aren't notes or data files, and no other vault of the site. A server reads them from the vault, and a build writes them. Default: `` `{ path: "/obsidian-files" }` `` |

### `vault.source()` [#vaultsource]

### LocalSourceOptions

| Prop         | Type                  | Description                                                                                                                                                                                                                                   |
| ------------ | --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `baseDir?`   | `string \| undefined` | The folder for the pages in the site's file tree, which sets their URLs.                                                                                                                                                                      |
| `staleTime?` | `number \| undefined` | How long the server keeps what it read, in milliseconds. The first request after that reads the folder again and parses the changed files. In development, any change in the folder reads it again. Default: `` until `revalidateLoader()` `` |
