# Blog

A blog beside your docs.

`@vuesho/blog` publishes a content source's pages as blog posts. Each post has a page with its authors, its tags, and links to the newer and older posts. The plugin also adds a post index, a tags page and a page per tag.

## Setup [#setup]

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

```npm
npm i @vuesho/blog
```

### Add the Posts [#add-the-posts-step]

Put the posts in a separate folder, like `content/blog`:

```files
content
├── index.md
└── blog
    └── hello.md
```

A [collection](/markdown/collections) reads the folder as a separate source, and `blogPlugin()` takes the source's key. The blog needs its own `baseDir`, since two sources can't both serve pages at the site root:

```ts title="vuesho.config.ts"
import { blogPlugin } from "@vuesho/blog";
import { createSite, recommendedPreset } from "vuesho/config";
import { defineCollections, defineDocs } from "vuesho/content";

const docs = defineDocs();
const blog = defineCollections({ type: "doc", dir: "content/blog" });

export default createSite({
  docs: docs.toSource(),
  blog: blog.toSource({ baseDir: "blog" }), // [!code highlight]
}).plugins(recommendedPreset(), blogPlugin({ source: "blog" }));
```

With `baseDir: "blog"`, `content/blog/hello.md` is the page `/blog/hello`.

### Hide the Posts From the Sidebar [#hide-the-posts-from-the-sidebar-step]

`!blog` in the root [meta file](/navigation) hides the posts from the sidebar, but keeps their pages:

```json title="content/meta.json"
{
  "pages": ["...", "!blog"]
}
```

### Link the Blog [#link-the-blog-step]

Add a link to the index in the navbar:

```ts title="vuesho.config.ts"
createSite(sources).config({
  ui: { layoutProps: { links: [{ text: "Blog", url: "/blog" }] } },
});
```

## Writing Posts [#writing-posts]

A post is a Markdown page. `blogPlugin()` adds three fields to its frontmatter: `tags`, `authors` and `image`.

```md title="content/blog/hello.md"
---
title: Hello World
description: Our first post.
date: 2026-11-01 # Orders the posts, newest first
tags: [Announcement] # Each tag has a page
authors: [fuma] # Keys of the `authors` option of `blogPlugin()`
image: /covers/hello.png # The cover image
---

Our blog is open.
```

A post without a `date` comes first. The fields are typed on `page.data`, and a wrong type fails the build.

<Callout title="Frontmatter Schemas">
If your collection has a [frontmatter schema](/markdown/collections#frontmatter-schema), leave `tags`, `authors` and `image` out of it, since the plugin declares them.
</Callout>

### Authors [#authors]

The `authors` option maps each ID in a post's `authors` to a profile. An ID without a profile shows as written:

```ts title="vuesho.config.ts"
blogPlugin({
  source: "blog",
  authors: {
    fuma: {
      name: "Fuma Nama",
      title: "Maintainer",
      url: "https://fuma-nama.dev",
      image: "https://github.com/fuma-nama.png",
    },
  },
});
```

### Posts From Other Sources [#posts-from-other-sources]

The posts can come from any [content source](/content-sources), or from several. `getTags`, `getAuthors` and `getImage` read a post's tags, authors and cover from other data, like a CMS field. When one returns `undefined`, the plugin reads the frontmatter field:

```ts title="vuesho.config.ts"
blogPlugin({
  source: ["blog", "news"],
  getTags: (post) => (post.type === "news" ? ["News"] : undefined),
  getImage: (post) => (post.type === "news" ? post.data.cover : undefined),
});
```

A post's date comes from the [`getPageCreatedAt` hook](/content-hooks), which reads `date` from a Markdown page's frontmatter.

## Blog Pages [#blog-pages]

The plugin adds three pages beside the posts:

- `/blog`: the post index, newest first
- `/blog/tags`: every tag, with its post count
- `/blog/tags/announcement`: the posts with a tag, like `Announcement`

`paths` changes their URLs, and `false` removes a page. With `tags: false`, a post's tags show without links:

```ts title="vuesho.config.ts"
blogPlugin({
  source: "blog",
  paths: { index: "/news", tags: false }, // [!code highlight]
});
```

With [Internationalization](/i18n), each language has these pages, like `/cn/blog`. When the posts come from a [dynamic source](/dynamic-rendering), the pages render on request.

### Listing Posts [#listing-posts]

`useBlogPosts()` returns the posts in the page's language, newest first, for a [custom page](/custom-pages) like a landing page:

```vue title="src/pages/index.vue"
<script setup lang="ts">
import { useBlogPosts } from "@vuesho/blog/theme";
import { Link } from "vuesho/client";

const posts = useBlogPosts();
</script>

<template>
  <ul>
    <li v-for="post in posts?.slice(0, 3)" :key="post.url">
      <Link :href="post.url">{{ post.title }}</Link>
    </li>
  </ul>
</template>
```

To render the posts' bodies, return them from the custom page's `contentOf`, as in [Custom Pages](/custom-pages#embedded-content).

### Feeds and Search [#feeds-and-search]

Posts are in the sitemap, search and the [RSS feed](/seo#rss-feed), like other pages. To limit the feed to posts, set `rss.filter`:

```ts title="vuesho.config.ts"
recommendedPreset({
  rss: {
    filter: ({ page }) => page.type === "blog", // [!code highlight]
    channel: { title: "Acme Blog" },
  },
});
```

## Customizing the UI [#customizing-the-ui]

A post renders with the [theme's](/theme) `blog.post` layout, unless its frontmatter sets another `layout`. The index, the tags page and each tag's page are the views `blog.index`, `blog.tags` and `blog.tag`. Your theme can replace any of them:

```ts title="src/theme.ts"
import { defineTheme } from "vuesho/theme";
import defaultTheme from "vuesho/theme/default";
import type app from "../vuesho.config";

export default defineTheme<typeof app>({
  extends: defaultTheme,
  views: { "blog.index": () => import("./BlogIndex.vue") }, // [!code highlight]
  layouts: { "blog.post": () => import("./BlogPost.vue") }, // [!code highlight]
});
```

A replacement can wrap the built-in pages' components from `@vuesho/blog/ui`: `BlogIndexView`, `BlogTagsView`, `BlogTagView` and `BlogPostView`. A view receives its page as the `page` prop, with the view's props in `page.props`:

```vue title="src/BlogIndex.vue"
<script setup lang="ts">
import { BlogIndexView } from "@vuesho/blog/ui";
import type { PageOf } from "vuesho/client";
import { HomeShell } from "vuesho/theme";
import type app from "../vuesho.config";

defineProps<{ page: PageOf<typeof app, "blog.index"> }>();
</script>

<template>
  <HomeShell>
    <BlogIndexView v-bind="page.props">
      <template #heading>News</template>
      <template #description>What we're working on.</template>
    </BlogIndexView>
  </HomeShell>
</template>
```

A post's layout receives the post in `page.pageData.blog`, with the newer and older posts:

```vue title="src/BlogPost.vue"
<script setup lang="ts">
import { BlogPostView } from "@vuesho/blog/ui";
import type { PageOf } from "vuesho/client";
import { HomeShell, toTocItems } from "vuesho/theme";
import type app from "../vuesho.config";

defineProps<{ page: PageOf<typeof app, "blog"> }>();
</script>

<template>
  <HomeShell>
    <BlogPostView v-if="page.pageData.blog" v-bind="page.pageData.blog" :toc="toTocItems(page.toc)">
      <component :is="page.Content" />
    </BlogPostView>
  </HomeShell>
</template>
```

`@vuesho/blog/types` exports the data types, like `BlogPost`, `BlogAuthor` and `BlogPostViewProps`. To edit the components themselves, copy them into your project with [`vuesho add`](/cli#vuesho-add):

```npm
npx vuesho add components/layouts --from @vuesho/blog
```

## Translations [#translations]

The UI has language packs for `zh-CN` and `zh-TW`. For another language, or to change a label, set `ui.translations`, as in [Internationalization](/i18n):

```ts title="vuesho.config.ts"
createSite(sources).config({
  site: { lang: "ja" },
  ui: {
    translations: {
      "All Tags(blog tags page)": "すべてのタグ",
      "Newer post(blog post navigation)": "新しい記事",
      "Older post(blog post navigation)": "古い記事",
    },
  },
});
```

## Options [#options]

### BlogPluginOptions

| Prop          | Type                                                                                                                            | Description                                                                                            |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------ |
| `source`      | `string \| readonly (string \| undefined)[] \| undefined`                                                                       | The source keys that hold the posts.                                                                   |
| `authors?`    | `Record<string, BlogAuthor> \| undefined`                                                                                       | The authors, by the IDs that posts list in `authors`. An ID without an author shows as is.             |
| `paths?`      | `{ index?: string \| false; tags?: string \| false; } \| undefined`                                                             | The paths of the index and the tags page, or `false` for none. Each tag's page is under the tags page. |
| `getTags?`    | `((this: Context<AppShape>, page: Page<string \| undefined, SourcePageData>) => Awaitable<string[] \| undefined>) \| undefined` | A post's tags, or `undefined` to read its `tags`.                                                      |
| `getAuthors?` | `((this: Context<AppShape>, page: Page<string \| undefined, SourcePageData>) => Awaitable<string[] \| undefined>) \| undefined` | A post's author IDs, or `undefined` to read its `authors`.                                             |
| `getImage?`   | `((this: Context<AppShape>, page: Page<string \| undefined, SourcePageData>) => Awaitable<string \| undefined>) \| undefined`   | A post's cover image URL, or `undefined` to read its `image`.                                          |

Each entry in `authors`:

### BlogAuthor

| Prop     | Type                  | Description                         |
| -------- | --------------------- | ----------------------------------- |
| `name`   | `string`              | The author's display name.          |
| `title?` | `string \| undefined` | Their role or job title.            |
| `url?`   | `string \| undefined` | A link to their profile or website. |
| `image?` | `string \| undefined` | Their avatar's URL.                 |
