1 | # Nuxt-Content
|
2 |
|
3 | `nuxt-content` facilitates the usage of markdown files in content heavy sites.
|
4 | It does this first, by compiling all the data from markdown files based on configured rules and second, by providing helper methods for dynamically accessing this data inside Nuxt pages.
|
5 |
|
6 | Best of all, `nuxt-content` is a lightweight abstraction that does as little work as possible. Nuxt is already great, so we only need to add a little bit of sugar on top to handle the content. :)
|
7 |
|
8 | ## Installation
|
9 |
|
10 | ```
|
11 | npm install nuxt-content
|
12 |
|
13 | ```
|
14 |
|
15 | Then, under `nuxt.config.js` install the module:
|
16 |
|
17 | ```
|
18 | modules: [
|
19 | '@nuxtjs/content'
|
20 | ]
|
21 | ```
|
22 |
|
23 | ## Getting Started
|
24 |
|
25 | There are two main steps to using `nuxt-content`: 1) configuring how you want your content data compiled and 2) dynamically using the content data inside Nuxt pages.
|
26 |
|
27 | ### Content Configuration
|
28 |
|
29 | #### Directory Options
|
30 |
|
31 | All content options can be configured either inside a `nuxt.content.js` file or under the `content` property in `nuxt.config.js`.
|
32 |
|
33 | *Note: All paths are relative to Nuxt Source Directory.*
|
34 |
|
35 | - `srcDir`, String that specifies the directory where the content is located. By default, all content is placed in the `/content` directory.
|
36 | - `routeName`, String that specifies the name of the dynamic page route that serves as the content's page. This is necessary so that the route path can be changed to match the content's permalink configuration.
|
37 | - `permalink`, String that specifies dynamic url path configuration options. The possible options are `:slug`, `:section`, `:year`, `:month`, `:day`.
|
38 | - `isPost`, Boolean that specifies whether the content requires a date. The default is true.
|
39 | - `data`, Object that specifies that additional data that you would like injected into your content's component.
|
40 | - `dirs`, Array that specifies options for all content under a directory. A 2D array is also allowed to configure multiple content types. These nested configurations will override any global directory options mentioned above.
|
41 |
|
42 | Here's an example `nuxt.content.js` file:
|
43 |
|
44 | ```js
|
45 | module.exports = {
|
46 | // Global Options
|
47 | srcDir: "content" // default
|
48 | // Directory Options
|
49 | dirs: [
|
50 | // content/blog/2013-01-10-HelloWorld.md -> localhost:3000/2013/hello-world
|
51 | ["posts", {
|
52 | routeName: "post", // pages/_post.vue
|
53 | permalink: ':year/:slug',
|
54 | data: {
|
55 | author: "Alid Castano"
|
56 | }
|
57 | }],
|
58 | // content/projects/NuxtContent.md -> localhost:3000/projects/nuxt-content
|
59 | ["projects", {
|
60 | routeName: "projects-slug", // pages/projects/_slug.vue
|
61 | permalink: "projects/:slug",
|
62 | isPost: false
|
63 | }]
|
64 | ]
|
65 | }
|
66 |
|
67 | ```
|
68 |
|
69 | A couple considerations to keep in mind from the options above:
|
70 |
|
71 | - When specifying the `routeName`, you have to mentally serialize the route name based on the route's directory path. As a general rule, just ignore all initial underscores and file extensions, and separate the remaining words by a hypen.
|
72 | - Since all content route paths will be changed to comply to the content's `permalink`, be extra mindful of how you configure permalinks to avoid conflict. For example, since you can only have one dynamic page per level, both `:year/:slug` and `:section/:slug` conflict. To avoid this, hard code sections whenever possible. The permalinks `someSection/:slug` and `:year/slug` would be preferable.
|
73 |
|
74 |
|
75 | #### Page Options
|
76 |
|
77 | By default, page specific data is extracted from the file name, but some options can be overridden via the front-matter of the respective file.
|
78 |
|
79 | Front Matter Options:
|
80 | - `slug`, String that overrides the content's url identification name.
|
81 | - `permalink`, String that overrides the content's entire dynamic url path.
|
82 |
|
83 | For example, if you wanted to override the page's slug:
|
84 |
|
85 | ```js
|
86 | // `nuxt.config.js`
|
87 | content: {
|
88 | route: '/blog',
|
89 | permalink: 'blog/:year/:slug'
|
90 | }
|
91 |
|
92 | // content/2014-05-10-MyFirstPost.md -> localhost:3000/blog/1st
|
93 | ---
|
94 | permalink: "1st"
|
95 | ---
|
96 |
|
97 | # Hello World!
|
98 |
|
99 | ```
|
100 |
|
101 | ### Content Usage
|
102 |
|
103 | `Nuxt-content` merges and compiles all your content's data - the filename, front-matter, and markdown content. You can dynamically request this data inside Nuxt pages using the `$content` helper.
|
104 |
|
105 | The `$content` helper is injected into the `context.app` property passed to the asyncData method that is available inside each Nuxt page.
|
106 |
|
107 | `$content` takes as its first argument the name of the registered directory whose files you are requesting. The method fetches all the data from the markdown files inside that directory and returns two methods, `get` and `getAll`, for accessing the requested content.
|
108 |
|
109 | `get` takes in the route's path and returns the content of that specific route. `getAll` returns all the content data retrieved from the registered directory.
|
110 |
|
111 | Here's an example that uses both methods:
|
112 |
|
113 | ```js
|
114 | // pages/post.vue
|
115 |
|
116 | export default {
|
117 | asyncData ({ app, route }) {
|
118 | const posts = app.$content('/posts')
|
119 | return {
|
120 | currPost: posts.get(route.path)
|
121 | posts: posts.getAll()
|
122 | }
|
123 | },
|
124 | render (h) {
|
125 | h (
|
126 | <div>
|
127 | <h3> Article: </h3>
|
128 | <section class="post">
|
129 | <h1> {{ currPost.title }} </h1>
|
130 | <div v-html="currPost.content" />
|
131 | </section>
|
132 | <h3> See more: </h3>
|
133 | <ul>
|
134 | <li v-for="post in posts">
|
135 | <a href="post.permalink"> {{ post.title }}</a>
|
136 | </li>
|
137 | </ul>
|
138 | </div>
|
139 | )
|
140 | }
|
141 | }
|
142 |
|
143 | ```
|
144 | ### License
|
145 |
|
146 | MIT
|