@nuxtjs/content
Write your content inside your Nuxt app
📖 Release Notes
Setup
- Add
@nuxtjs/content
dependency to your project
yarn add @nuxtjs/content
- Add
@nuxtjs/content
to the modules
section of nuxt.config.js
{
modules: [
'@nuxtjs/content'
]
}
Usage
Create a content/
directory in your Nuxt project:
content/
articles/
article-1.md
article-2.md
home.md
Methods
$content(DirectoryPath: string).fetch()
⇒ [{}]
$content(FilePath: string).fetch()
⇒ {}
Example
content/home.md
----
title: Home page
---
# Home page
> Welcome to my *home page*!
$content('home').fetch()
{
"dir": "",
"slug": "home",
"path": "/home",
"updatedAt": "2017-11-07T12:21:34Z",
"metadata": {
"title": "Home page"
},
"body": {
"type": "root",
"children": [
{
"type": "heading",
"depth": 1,
"children": [
{
"type": "text",
"value": "Home page"
}
]
},
{
"type": "blockquote",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "Welcome to my ",
},
{
"type": "emphasis",
"children": [
{
"type": "text",
"value": "home page",
}
]
},
{
"type": "text",
"value": "!"
}
]
}
]
}
]
}
}
You can now use this.$content(path)
in your Vue components:
const articles = await this.$content('articles')
.only(['title', 'date', 'authors'])
.sortBy('date', 'asc')
.limit(5)
.skip(10)
.where({
tags: 'testing',
isArchived: false,
date: { $gt: new Date(2020) },
rating: { $gte: 3 },
})
.search('welcome')
.fetch()
const [ prev1, prev2, next1, next2, next3 ] = await this.$content('articles')
.only(['title', 'path'])
.sortBy('date')
.surround('article-2', { before: 2, after: 3 })
.where({ isArchived: false })
.fetch()
[
null,
{
title: 'Article 1',
path: 'article-1'
},
null,
null,
null
]
Code example
<template>
<article>
<h1>{{ page.title }}</h1>
<nuxt-content :body="page.body" />
</article>
</template>
<script>
export default {
data() {
page: null,
learn: null
},
fetch () {
this.page = await this.$content(`${this.i18n.locale}/guide/installation`).fetch()
this.learnSection = await this.$content(`${this.i18n.locale}/learn`)
.only(['title', 'metadata'])
.sortBy('updatedAt', 'desc')
}
}
</script>
Using global Vue component in Markdown is supported (see how nuxt press does).
Configuration
export default {
modules: ['@nuxtjs/content'],
content: {
apiPrefix: '_content',
dir: 'content',
fullTextSearchFields: ['title', 'description', 'slug'],
markdown: {
externalLinks: {},
prism: {
theme: 'prismjs/themes/prism.css'
}
},
yaml: {
},
csv: {
}
}
}
Development
- Clone this repository
- Install dependencies using
yarn install
or npm install
- Start development server using
yarn dev
or npm run dev
License
MIT License
Copyright (c) NuxtJS Company