Article Component Setup
Do initial installation first
You need to import and install this project to your VitePress site, if you haven't done so already.
You are free to create your author file and data loader wherever you want. However, you will need to update the paths accordingly.
Author Information
- Create the
authors.jsfile (docs/.vitepress/theme/data/authors.js)
js
const authors = {};
export default authors;- You can add authors by updating the file as follows:
js
const authors = {
sherlock: {
name: 'Sherlock Holmes',
avatar: '/authors/sherlock.jpg', // Optional
description: 'Detective', // Optional
url: 'https://example.com/external-URL-of-author' // Optional
}
};
export default authors;- Import this data in your site's
index.tsfile:
ts
import authors from './data/authors.js'
// ...
export default {
// ...
enhanceApp({ app, router, siteData }) {
// ...
app.provide('authors', authors)
}
} satisfies ThemeData Loader Setup
- Create a new directory for your articles (
docs/articles). - Create the data loader file
articles.data.js(docs/.vitepress/theme/data/articles.data.js). If you didn't usedocs/articlesas the directory for your articles, you will need to adjust theblog/**/*.mdpattern accordingly:
js
import { createContentLoader } from 'vitepress';
const data = createContentLoader('articles/**/*.md', {
transform(rawData) {
return rawData
.sort((a, b) => {
const dateA = new Date(a.frontmatter.date);
const dateB = new Date(b.frontmatter.date);
return dateB.getTime() - dateA.getTime();
});
}
});
export default data;- Import the data loader in your site's
index.tsfile:
ts
import { data as postsData } from './data/posts.data.js';
// ...
export default {
// ...
enhanceApp({ app, router, siteData }) {
// ...
app.provide('postsData', postsData)
}
} satisfies ThemeCustomizations
Optional: Override the default export keys
Warning: This section will not be necessary for most users.
While this package has extensive support for filtering posts, you may choose to have completely separate authors and/or post data sections.
- If you have multiple blog/announcement sections, you can specify the data for each section separately instead of remembering what to filter out.
- This can also be helpful if your site is already using the
authorsorpostsDatakeys for other purposes.
Steps:
- Create any additional data and authors files as needed. For example:
docs/.vitepress/theme/data/demo-blog.data.jsdocs/.vitepress/theme/data/demo-authors.js
- When creating a new authors data file, you will need to update object's name. For example:
js
const demoAuthors = {
sherlock: {
name: 'Sherlock Holmes',
avatar: '/authors/sherlock.jpg',
description: 'Detective', // Optional
url: 'https://example.com/external-URL-of-author' // Optional
}
};
export default demoAuthors; - In your
index.tsfile, import the additional data and/or authors files:
ts
import authors from './data/authors.js'
import { data as postsData } from './data/posts.data.js';
import { data as demoBlogData } from './data/demo-blog.data.js';
import demoAuthors from './data/demo-authors.js';
// ...
export default {
// ...
enhanceApp({ app, router, siteData }) {
// ...
app.provide('authors', authors)
app.provide('postsData', postsData)
app.provide('demoAuthors', demoAuthors)
app.provide('demoBlogData', demoBlogData)
}
}- You will need to use the
postsDataKeyand/orauthorsDataKeyprops in the components that use the data. See the documentation for each component for more information.