You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guide/data-loading.md
+100-11
Original file line number
Diff line number
Diff line change
@@ -56,29 +56,118 @@ export default {
56
56
57
57
When you need to generate data based on local files, you should use the `watch` option in the data loader so that changes made to these files can trigger hot updates.
58
58
59
-
The `watch` option is also convenient in that you can use [glob patterns](https://github.com/mrmlnc/fast-glob#pattern-syntax) to match multiple files. The patterns can be relative to the loader file itself, and the `load()` function will receive the matched files as absolute paths:
59
+
The `watch` option is also convenient in that you can use [glob patterns](https://github.com/mrmlnc/fast-glob#pattern-syntax) to match multiple files. The patterns can be relative to the loader file itself, and the `load()` function will receive the matched files as absolute paths.
60
+
61
+
The following example shows loading CSV files and transforming them into JSON using [csv-parse](https://github.com/adaltas/node-csv/tree/master/packages/csv-parse/). Because this file only executes at build time, you will not be shipping the CSV parser to the client!
60
62
61
63
```js
62
64
importfsfrom'node:fs'
63
-
importparseFrontmatterfrom'gray-matter'
65
+
import{ parse } from'csv-parse/sync'
64
66
65
67
exportdefault {
66
-
// watch all blog posts
67
-
watch: ['./posts/*.md'],
68
+
watch: ['./data/*.csv'],
68
69
load(watchedFiles) {
69
70
// watchedFiles will be an array of absolute paths of the matched files.
70
71
// generate an array of blog post metadata that can be used to render
When building a content focused site, we often need to create an "archive" or "index" page: a page where we list all available entries in our content collection, for example blog posts or API pages. We **can** implement this directly with the data loader API, but since this is such a common use case, VitePress also provides a `createContentLoader` helper to simplify this:
The helper takes a glob pattern relative to [project root](./routing#project-root), and returns a `{ watch, load }` data loader object that can be used as the default export in a data loader file. It also implements caching based on file modified timestamps to improve dev performance.
95
+
96
+
Note the loader only works with Markdown files - matched non-Markdown files will be skipped.
97
+
98
+
The loaded data will be an array with the type of `ContentData[]`:
99
+
100
+
```ts
101
+
interfaceContentData {
102
+
// mapped absolute URL for the page. e.g. /posts/hello.html
103
+
url:string
104
+
// frontmatter data of the page
105
+
frontmatter:Record<string, any>
106
+
107
+
// the following are only present if relevant options are enabled
108
+
// we will discuss them below
109
+
src:string|undefined
110
+
html:string|undefined
111
+
excerpt:string|undefined
112
+
}
113
+
```
114
+
115
+
By default, only `url` and `frontmatter` are provided. This is because the loaded data will be inlined as JSON in the client bundle, so we need to be cautious about its size. Here's an example using the data to build a minimal blog index page:
0 commit comments