1 | # gatsby-plugin-draft
|
2 |
|
3 | [![npm version](https://img.shields.io/npm/v/gatsby-plugin-draft.svg)](https://www.npmjs.com/package/gatsby-plugin-draft)
|
4 | [![install size](https://packagephobia.now.sh/badge?p=gatsby-plugin-draft)](https://packagephobia.now.sh/result?p=gatsby-plugin-draft)
|
5 | [![Build Status](https://travis-ci.com/shooontan/gatsby-plugin-draft.svg?branch=master)](https://travis-ci.com/shooontan/gatsby-plugin-draft)
|
6 |
|
7 |
|
8 | GatsbyJS Plugin for adding draft field to node.
|
9 |
|
10 | This plugin adds draft field to decide whether publish to Gatsby's data system node. For example, when we build blog (with `gatsby-transformer-remark`), GatsbyJS creates `MarkdownRemark` nodes. This node has `frontmatter` property. If `frontmatter` includes `date` metadata, `gatsby-plugin-draft` add automatically `draft` value to Gatsby's node field.
|
11 |
|
12 | ## Install
|
13 |
|
14 | ```bash
|
15 | # npm
|
16 | $ npm install gatsby-plugin-draft
|
17 |
|
18 | # or yarn
|
19 | $ yarn add gatsby-plugin-draft
|
20 | ```
|
21 |
|
22 | ## How to use
|
23 |
|
24 | ### gatsby-config.js
|
25 |
|
26 | #### with Markdown
|
27 |
|
28 | You need to add `gatsby-source-filesystem` and `gatsby-transformer-remark`.
|
29 |
|
30 | ```js
|
31 | module.exports = {
|
32 | plugins: [
|
33 | 'gatsby-source-filesystem',
|
34 | 'gatsby-transformer-remark',
|
35 | 'gatsby-plugin-draft'
|
36 | ],
|
37 | };
|
38 | ```
|
39 |
|
40 | #### with MDX
|
41 |
|
42 | You need to add `gatsby-source-filesystem` and `gatsby-plugin-mdx`. Set `Mdx` to `nodeType` option.
|
43 |
|
44 | ```js
|
45 | module.exports = {
|
46 | plugins: [
|
47 | 'gatsby-source-filesystem',
|
48 | 'gatsby-plugin-mdx',
|
49 | {
|
50 | resolve: 'gatsby-plugin-draft',
|
51 | options: {
|
52 | nodeType: 'Mdx',
|
53 | },
|
54 | },
|
55 | ],
|
56 | };
|
57 | ```
|
58 |
|
59 | #### other source
|
60 |
|
61 | You need to add `gatsby-source-anydata`. Set node internal type to `nodeType` option.
|
62 |
|
63 | ```js
|
64 | module.exports = {
|
65 | plugins: [
|
66 | 'gatsby-source-anydata',
|
67 | {
|
68 | resolve: 'gatsby-plugin-draft',
|
69 | options: {
|
70 | nodeType: 'Anydata',
|
71 | },
|
72 | },
|
73 | ],
|
74 | };
|
75 | ```
|
76 |
|
77 | ### gatsby-node.js
|
78 |
|
79 | You can query like the following. The important thing is to add `filter`. That query results is only the post whose `draft` is `false`.
|
80 |
|
81 | ```js
|
82 | const markdownTemplate = 'app/template/markdown';
|
83 | const mdxTemplate = 'app/template/mdx';
|
84 | const anycmsTemplate = 'app/template/anycms';
|
85 |
|
86 | exports.createPages = async ({ graphql, actions, reporter }) => {
|
87 | const { createPage } = actions;
|
88 | const result = await graphql(
|
89 | `
|
90 | {
|
91 | allMarkdownRemark(
|
92 | filter: { fields: { draft: { eq: false } } } # add
|
93 | ) {
|
94 | edges {
|
95 | node {
|
96 | fields {
|
97 | slug
|
98 | }
|
99 | }
|
100 | }
|
101 | }
|
102 | allMdx(
|
103 | filter: { fields: { draft: { eq: false } } } # add
|
104 | ) {
|
105 | edges {
|
106 | node {
|
107 | fields {
|
108 | slug
|
109 | }
|
110 | }
|
111 | }
|
112 | }
|
113 | allAnycms(
|
114 | filter: { fields: { draft: { eq: false } } } # add
|
115 | ) {
|
116 | edges {
|
117 | node {
|
118 | fields {
|
119 | slug
|
120 | }
|
121 | }
|
122 | }
|
123 | }
|
124 | }
|
125 | `
|
126 | );
|
127 |
|
128 | if (result.errors) {
|
129 | reporter.panic(result.errors);
|
130 | }
|
131 |
|
132 | result.data.allMarkdownRemark.edges.forEach(post => {
|
133 | createPage({
|
134 | path: post.node.fields.slug,
|
135 | component: markdownTemplate,
|
136 | context: {
|
137 | slug: post.node.fields.slug,
|
138 | },
|
139 | });
|
140 | });
|
141 |
|
142 | result.data.allMdx.edges.forEach(post => {
|
143 | createPage({
|
144 | path: post.node.fields.slug,
|
145 | component: mdxTemplate,
|
146 | context: {
|
147 | slug: post.node.fields.slug,
|
148 | },
|
149 | });
|
150 | });
|
151 |
|
152 | result.data.anyCms.edges.forEach(post => {
|
153 | createPage({
|
154 | path: post.node.fields.slug,
|
155 | component: anycmsTemplate,
|
156 | context: {
|
157 | slug: post.node.fields.slug,
|
158 | },
|
159 | });
|
160 | });
|
161 | };
|
162 | ```
|
163 |
|
164 | ### pages/index.js
|
165 |
|
166 | Add filter in each pages.
|
167 |
|
168 | ```js
|
169 | export const query = graphql`
|
170 | query IndexQuery {
|
171 | allMarkdownRemark(
|
172 | filter: { fields: { draft: { eq: false } } } # here
|
173 | ) {
|
174 | edges {
|
175 | node {
|
176 | excerpt
|
177 | }
|
178 | }
|
179 | }
|
180 | allMdx(
|
181 | filter: { fields: { draft: { eq: false } } } # here
|
182 | ) {
|
183 | edges {
|
184 | node {
|
185 | excerpt
|
186 | }
|
187 | }
|
188 | }
|
189 | allAnycms(
|
190 | filter: { fields: { draft: { eq: false } } } # here
|
191 | ) {
|
192 | edges {
|
193 | node {
|
194 | excerpt
|
195 | }
|
196 | }
|
197 | }
|
198 | }
|
199 | `;
|
200 | ```
|
201 |
|
202 | ### Draft Pattern
|
203 |
|
204 | Let's say you have the following content. If you run `gatsby build` on Feb 22. 2019, the First Post will be published, but Second-Post will not be published.
|
205 |
|
206 | If you build on Feb 26. 2019, both post will be published.
|
207 |
|
208 | ```md
|
209 | ---
|
210 | id: 1
|
211 | title: First Post
|
212 | date: 2019-02-20
|
213 | ---
|
214 |
|
215 | Published content.
|
216 | ```
|
217 |
|
218 | ```md
|
219 | ---
|
220 | id: 2
|
221 | title: Second Post
|
222 | date: 2019-02-25
|
223 | ---
|
224 |
|
225 | Draft content.
|
226 | ```
|
227 |
|
228 | Another Example. If a post has `draft: true` in frontmatter, the post is never published even if `date` is before build date time.
|
229 |
|
230 | ```md
|
231 | ---
|
232 | id: 3
|
233 | title: Second Post
|
234 | date: 2010-10-10
|
235 | draft: true
|
236 | ---
|
237 |
|
238 | Draft content, forever and ever!
|
239 | ```
|
240 |
|
241 | ### Options
|
242 |
|
243 | ```js
|
244 | module.exports = {
|
245 | plugins: [
|
246 | {
|
247 | resolve: 'gatsby-plugin-draft',
|
248 | options: {
|
249 | /**
|
250 | * Be added field name. [Optional]
|
251 | *
|
252 | * Type: string
|
253 | * Default: 'draft'
|
254 | **/
|
255 | fieldName: 'notReleased',
|
256 |
|
257 | /**
|
258 | * moment-timezone. [Optional]
|
259 | *
|
260 | * Type: string
|
261 | * Default: 'UTC'
|
262 | **/
|
263 | timezone: 'Asia/Tokyo',
|
264 |
|
265 | /**
|
266 | * Gatsby's node internal type. [Optional]
|
267 | *
|
268 | * Type: string
|
269 | * Default: 'MarkdownRemark'
|
270 | **/
|
271 | nodeType: 'GatsbyNodeInternalType',
|
272 |
|
273 | /**
|
274 | * Date information. [Optional]
|
275 | *
|
276 | * Type: function
|
277 | * - node: Gatsby's data node. https://www.gatsbyjs.com/docs/node-interface/
|
278 | * Default: node => node.frontmatter.date
|
279 | **/
|
280 | pickDate: node => node.metadata.publishedAt,
|
281 |
|
282 | /**
|
283 | * Draft information. [Optional]
|
284 | *
|
285 | * Type: function
|
286 | * - node: Gatsby's data node. https://www.gatsbyjs.com/docs/node-interface/
|
287 | * Default: node => node.frontmatter.draft
|
288 | **/
|
289 | pickDraft: node => node.metadata.isDraft,
|
290 |
|
291 | /**
|
292 | * publish draft posts [Optional]
|
293 | * Default is 'false'
|
294 | **/
|
295 | publishDraft: process.env.NODE_ENV !== 'production',
|
296 | },
|
297 | },
|
298 | ],
|
299 | };
|
300 | ```
|
301 |
|
302 | #### publishDraft
|
303 |
|
304 | If `publishDraft` is `false`, the posts which have draft field valued `true` does not published. So we can not edit watching that posts. This option is useful when we edit posts in development mode (`gatsby develop`).
|