{"data":{"markdownRemark":{"html":"<p>This tutorial will take ~30 minutes or so. We'll take a Gatsby starter and add comment support via StaticMan.</p>\n<h2>Prerequisites</h2>\n<p>To follow this tutorial you will need:</p>\n<ul>\n<li>Git installed</li>\n<li>A GitHub account</li>\n<li>Node / npm (and preferably yarn) installed</li>\n<li>Gatsby installed (<code class=\"language-text\">npm -g install gatsby-cli</code>)</li>\n</ul>\n<h2>NPM or yarn</h2>\n<p>This tutorial assumes you have installed <a href=\"https://yarnpkg.com/en/docs/install\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">yarn</a>. You can use npm instead, but you will need to convert the <code class=\"language-text\">yarn</code> comments to their <code class=\"language-text\">npm</code> equivalents. We recommend <code class=\"language-text\">yarn</code>.</p>\n<h2>Fork the starter</h2>\n<p>To start the tutorial, fork <a href=\"https://github.com/GatsbyCentral/staticman-tutorial\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">this repo</a> on GitHub. Once you have your own fork, clone that.</p>\n<p>Detailed steps:</p>\n<ul>\n<li>Open the repository page</li>\n<li>Click the \"Fork\" button in the top right</li>\n<li>Click on the \"Refresh\" button</li>\n<li>Click on the green \"Clone or download\" button</li>\n<li>Copy the clone URL for this repository</li>\n<li>Run <code class=\"language-text\">git clone URL</code> in a terminal</li>\n</ul>\n<p>You should now have a local copy of your freshly cloned repository in a directory called <code class=\"language-text\">staticman-tutorial</code></p>\n<h2>Start Gatsby</h2>\n<p>Start your freshly cloned Gatsby site.</p>\n<ul>\n<li><code class=\"language-text\">cd staticman-tutorial</code></li>\n<li><code class=\"language-text\">yarn</code></li>\n<li><code class=\"language-text\">gatsby develop</code></li>\n</ul>\n<p>At this point  Gatsby should open chrome and load your Gatsby site. It should look like this:</p>\n<p><img src=\"images/staticman-tutorial-first-launch.png\" alt=\"Blog starter first launch screenshot\"></p>\n<p>Click on the first blog post. Comments are only visible on individual blog posts, not on the homepage.</p>\n<h2>Comments section</h2>\n<p>Open the <code class=\"language-text\">src/components/PostTemplateDetails/index.jsx</code> file. Online 35 you should see <code class=\"language-text\">const commentsBlock = &lt;div /&gt;</code>.</p>\n<p>Replace the <code class=\"language-text\">&lt;div /&gt;</code> with a \"Comments\" heading. It should look like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> commentsBlock <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span>\n  <span class=\"token operator\">&lt;</span>div<span class=\"token operator\">></span>\n    <span class=\"token operator\">&lt;</span>hr <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n    <span class=\"token operator\">&lt;</span>h2<span class=\"token operator\">></span>Comments<span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>h2<span class=\"token operator\">></span>\n    <span class=\"token operator\">&lt;</span>p<span class=\"token operator\">></span>No comments yet<span class=\"token punctuation\">.</span><span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>p<span class=\"token operator\">></span>\n  <span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>Now you should see this heading appear at the very bottom of the post.</p>\n<p>Let's move <code class=\"language-text\">{commentsBlock}</code> from line 72 up to line 61, below the <code class=\"language-text\">{tagsBlock}</code> line. Now the comments should appear directly underneath the tags.</p>\n<h2>Connect StaticMan</h2>\n<p>We need to give StaticMan access to our GitHub repository. You do this on your fork.</p>\n<ul>\n<li>Go to the Settings page for your GitHub fork</li>\n<li>Click on \"Collaborators &#x26; Teams\"</li>\n<li>\n<p>Under the \"Collaborators\" heading</p>\n<ul>\n<li>Type <code class=\"language-text\">staticmanapp</code> into the box</li>\n<li>Click \"Add collaborator\"</li>\n</ul>\n</li>\n</ul>\n<p>Now you've invited StaticMan to your project. Next, tell StaticMan to accept your invitation. You do that by visiting a special URL. You need to know your GitHub username (where your fork lives) and the repository name (probably <code class=\"language-text\">staticman-tutorial</code>).</p>\n<p>Then put those two values into the following URL:</p>\n<p><code class=\"language-text\">https://api.staticman.net/v2/connect/{your GitHub username}/{your repository name}</code></p>\n<p>The URL should look something like this:</p>\n<p><a href=\"https://api.staticman.net/v2/connect/GatsbyCentral/staticman-tutorial\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">https://api.staticman.net/v2/connect/GatsbyCentral/staticman-tutorial</a></p>\n<p>Open <strong>your</strong> URL in your browser, and you should see the message \"OK!\".</p>\n<h2>StaticMan config file</h2>\n<p>Now that you've connected StaticMan, the next step is to configure it.</p>\n<p>Create a new file at the root of your repository called <code class=\"language-text\">staticman.yml</code>. Copy the contents from <a href=\"https://raw.githubusercontent.com/eduardoboucas/staticman/5c37482e13d7eb33cafe413c6c5b3a1a853a7d12/staticman.sample.yml\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">the StaticMan sample config file</a>.</p>\n<blockquote>\n<p><strong>NOTE</strong> The filename must be exactly <code class=\"language-text\">staticman.yml</code>, not <code class=\"language-text\">.yaml</code> or otherwise.</p>\n</blockquote>\n<p>The sample configuration is fine for this tutorial. You can read the comments in the file to learn more about the available options.</p>\n<h3>Change <code class=\"language-text\">path</code></h3>\n<p>We need to make one change to the staticman config file. On line 68 remove <code class=\"language-text\">{options.slug}</code> from the <code class=\"language-text\">path</code>. It should look like:</p>\n<div class=\"gatsby-highlight\" data-language=\"yaml\"><pre class=\"language-yaml\"><code class=\"language-yaml\"><span class=\"token key atrule\">path</span><span class=\"token punctuation\">:</span> <span class=\"token string\">\"_data/comments\"</span></code></pre></div>\n<h2>Commit &#x26; push</h2>\n<p>At this point, you need to commit your changes. Hopefully you're familiar with git already. If not, try the following commands in a terminal.</p>\n<ul>\n<li><code class=\"language-text\">git add .</code></li>\n<li><code class=\"language-text\">git commit -m &#39;Configured staticman&#39;</code></li>\n<li><code class=\"language-text\">git push</code></li>\n</ul>\n<p>Now when you look at your fork on GitHub, you should see the <code class=\"language-text\">staticman.yml</code> file.</p>\n<h2>Comment form</h2>\n<p>Now we want to create a form to submit comments. This is just a standard HTML form.</p>\n<p>First, we need to construct the correct URL for the form. Just like above, you will need your GitHub username and fork name (<code class=\"language-text\">staticman-tutorial</code>). Then you create a URL like so:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">https://api.staticman.net/v2/entry/{your GitHub username}/{your fork name}/master/comments</code></pre></div>\n<p>Edit the <code class=\"language-text\">src/components/PostTemplateDetails/index.jsx</code> file again. After line 39, add a form like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token operator\">&lt;</span>h3<span class=\"token operator\">></span>Add a comment<span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>h3<span class=\"token operator\">></span>\n<span class=\"token operator\">&lt;</span>form\n  method<span class=\"token operator\">=</span><span class=\"token string\">\"POST\"</span>\n  action<span class=\"token operator\">=</span><span class=\"token string\">\"YOUR_FORM_URL\"</span>\n<span class=\"token operator\">></span>\n  <span class=\"token operator\">&lt;</span>input\n    name<span class=\"token operator\">=</span><span class=\"token string\">\"options[slug]\"</span>\n    type<span class=\"token operator\">=</span><span class=\"token string\">\"hidden\"</span>\n    value<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>props<span class=\"token punctuation\">.</span>pathContext<span class=\"token punctuation\">.</span>slug<span class=\"token punctuation\">}</span>\n  <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n  <span class=\"token operator\">&lt;</span>input name<span class=\"token operator\">=</span><span class=\"token string\">\"fields[name]\"</span> type<span class=\"token operator\">=</span><span class=\"token string\">\"text\"</span> placeholder<span class=\"token operator\">=</span><span class=\"token string\">\"Name\"</span> required <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n  <span class=\"token operator\">&lt;</span>input\n    name<span class=\"token operator\">=</span><span class=\"token string\">\"fields[email]\"</span>\n    type<span class=\"token operator\">=</span><span class=\"token string\">\"email\"</span>\n    placeholder<span class=\"token operator\">=</span><span class=\"token string\">\"Email\"</span>\n    required\n  <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n  <span class=\"token operator\">&lt;</span>textarea name<span class=\"token operator\">=</span><span class=\"token string\">\"fields[message]\"</span> placeholder<span class=\"token operator\">=</span><span class=\"token string\">\"Comment\"</span> required <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n  <span class=\"token operator\">&lt;</span>button type<span class=\"token operator\">=</span><span class=\"token string\">\"submit\"</span><span class=\"token operator\">></span>Submit Comment<span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>button<span class=\"token operator\">></span>\n<span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>form<span class=\"token operator\">></span></code></pre></div>\n<h3>Style the form</h3>\n<p>The form is unstyled at this point. Styling it is beyond the scope of this tutorial. It's just a standard HTML form, so you can style it using your preferred method.</p>\n<h2>Commit and push</h2>\n<p>As above, commit these changes, and push them to GitHub.</p>\n<ul>\n<li><code class=\"language-text\">git add .</code></li>\n<li><code class=\"language-text\">git commit -am &#39;Comment form.&#39;</code></li>\n<li><code class=\"language-text\">git push</code></li>\n</ul>\n<h2>Test comment</h2>\n<p>Type a name, email, and comment. Then click the \"Submit Comment\" button. You should see a JSON success message if your comment was submitted. Now look at your GitHub fork. You should see a new Pull Request with your test comment. Merge this PR.</p>\n<ul>\n<li>Go the Pull Requests tab on GitHub</li>\n<li>Click on the new Pull Request</li>\n<li>Scroll down and click the green \"Merge pull request\" button</li>\n<li>Click \"Confirm merge\"</li>\n</ul>\n<p>This commit is now in GitHub.</p>\n<h3>Pull</h3>\n<p>After merging the PR, you need to pull.</p>\n<p><code class=\"language-text\">git pull</code></p>\n<p>Check that you have a file in <code class=\"language-text\">_data/comments</code>. This means you have successfully pulled your test comment from GitHub.</p>\n<h2>Source comments</h2>\n<p>Now we have a comment, but it's not yet visible on the site. First, we need to add the <code class=\"language-text\">_data/comments</code> directory to our Gatsby config. This tells Gatsby to read this directory for comments.</p>\n<p>Install the <code class=\"language-text\">gatsby-tranformer-yaml</code> plugin like so:</p>\n<p><code class=\"language-text\">yarn add gatsby-transformer-yaml</code></p>\n<p>Open the <code class=\"language-text\">gatsby-config.js</code> file.</p>\n<p>After line 39, enable this plugin and create a new <code class=\"language-text\">gatsby-source-filesystem</code> by adding these lines:</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token string\">'gatsby-transformer-yaml'</span><span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">{</span>\n  resolve<span class=\"token punctuation\">:</span> <span class=\"token string\">'gatsby-source-filesystem'</span><span class=\"token punctuation\">,</span>\n  options<span class=\"token punctuation\">:</span> <span class=\"token punctuation\">{</span>\n    path<span class=\"token punctuation\">:</span> <span class=\"token template-string\"><span class=\"token string\">`</span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>__dirname<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\">/_data/comments`</span></span><span class=\"token punctuation\">,</span>\n    name<span class=\"token punctuation\">:</span> <span class=\"token string\">'comments'</span><span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></code></pre></div>\n<p>You need to restart Gatsby at this point. You do that by pressing <code class=\"language-text\">ctrl</code>+<code class=\"language-text\">c</code> in the terminal, then running <code class=\"language-text\">gatsby develop</code> again.</p>\n<p>Now your comment should be loaded into Gatsby's node system.</p>\n<h2>Load comments</h2>\n<p>Open the <code class=\"language-text\">src/templates/post-template.jsx</code> file. Edit the GraphQL query to add the comments. Add the following after line 52.</p>\n<div class=\"gatsby-highlight\" data-language=\"graphql\"><pre class=\"language-graphql\"><code class=\"language-graphql\">allCommentsYaml<span class=\"token punctuation\">(</span><span class=\"token attr-name\">filter</span><span class=\"token punctuation\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token attr-name\">slug</span><span class=\"token punctuation\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token attr-name\">eq</span><span class=\"token punctuation\">:</span> <span class=\"token variable\">$slug</span> <span class=\"token punctuation\">}</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  edges <span class=\"token punctuation\">{</span>\n    node <span class=\"token punctuation\">{</span>\n      id\n      name\n      email\n      message\n      date\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>This will load the comments into the post template.</p>\n<h2>Render comments</h2>\n<p>Open the <code class=\"language-text\">src/components/PostTemplateDetails/index.jsx</code> file again.</p>\n<p>Add the following after line 9.</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> comments <span class=\"token operator\">=</span>\n  <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>props<span class=\"token punctuation\">.</span>data<span class=\"token punctuation\">.</span>allCommentsYaml <span class=\"token operator\">&amp;&amp;</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>props<span class=\"token punctuation\">.</span>data<span class=\"token punctuation\">.</span>allCommentsYaml<span class=\"token punctuation\">.</span>edges</code></pre></div>\n<p>Above line 37 (<code class=\"language-text\">const commentsBlock =</code>) add the following:</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> commentsList <span class=\"token operator\">=</span>\n  comments <span class=\"token operator\">&amp;&amp;</span> comments<span class=\"token punctuation\">.</span>length <span class=\"token operator\">?</span> <span class=\"token punctuation\">(</span>\n    comments<span class=\"token punctuation\">.</span><span class=\"token function\">map</span><span class=\"token punctuation\">(</span>comment <span class=\"token operator\">=></span> <span class=\"token punctuation\">(</span>\n      <span class=\"token operator\">&lt;</span>div key<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>comment<span class=\"token punctuation\">.</span>node<span class=\"token punctuation\">.</span>id<span class=\"token punctuation\">}</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&lt;</span>p<span class=\"token operator\">></span>\n          Name<span class=\"token punctuation\">:</span> <span class=\"token punctuation\">{</span>comment<span class=\"token punctuation\">.</span>node<span class=\"token punctuation\">.</span>name<span class=\"token punctuation\">}</span>\n          <span class=\"token operator\">&lt;</span>br <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n          Comment<span class=\"token punctuation\">:</span> <span class=\"token punctuation\">{</span>comment<span class=\"token punctuation\">.</span>node<span class=\"token punctuation\">.</span>message<span class=\"token punctuation\">}</span>\n          <span class=\"token operator\">&lt;</span>br <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n          Date<span class=\"token punctuation\">:</span> <span class=\"token punctuation\">{</span>comment<span class=\"token punctuation\">.</span>node<span class=\"token punctuation\">.</span>date<span class=\"token punctuation\">}</span>\n        <span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>p<span class=\"token operator\">></span>\n      <span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span>\n    <span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">)</span> <span class=\"token punctuation\">:</span> <span class=\"token punctuation\">(</span>\n    <span class=\"token operator\">&lt;</span>p<span class=\"token operator\">></span>No comments yet<span class=\"token punctuation\">.</span><span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>p<span class=\"token operator\">></span>\n  <span class=\"token punctuation\">)</span></code></pre></div>\n<p>This is a bit complicated. It's doing several things.</p>\n<ul>\n<li>First, check if <code class=\"language-text\">comments</code> exists</li>\n<li>Second, check if <code class=\"language-text\">comments.length</code> is truthy (which means, there is at least 1 comment)</li>\n<li>If those two checks are true, then render a list of comments</li>\n<li>Otherwise, render the \"No comments yet\" message</li>\n</ul>\n<p>The next step is to replace the <code class=\"language-text\">&lt;p&gt;No comments yet.&lt;/p&gt;</code> block on line 58 with our new <code class=\"language-text\">commentsList</code> which looks like:</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token punctuation\">{</span>commentsList<span class=\"token punctuation\">}</span></code></pre></div>\n<p>When you save this file, you should see your test file appear above the comment form.</p>\n<h2>Success!</h2>\n<p>You can see all the changes we made in this tutorial <a href=\"https://github.com/GatsbyCentral/staticman-tutorial/compare/master...superlumen:master\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">on GitHub here</a>.</p>\n<h2>Next steps</h2>\n<p>This tutorial does not cover any styling. You can style the form and comment output using your preferred method.</p>\n<p>The comment output could also look much better. A few obvious upgrades include:</p>\n<ul>\n<li>Use the hashed email (<code class=\"language-text\">comment.node.email</code>) to show a Gravatar image for each comment</li>\n<li>Render an actual date instead of a timestamp</li>\n<li>Treat comment text as markdown</li>\n</ul>\n<p>The goal of this tutorial was to show you the basics of how to connect StaticMan and Gatsby. Hopefully we've done that. If you found the tutorial useful, please let us know in the comments or consider sharing it on twitter, facebook, etc.</p>\n<h2>Questions</h2>\n<p>If you have any issues with the tutorial, let us know in the comments below.</p>","excerpt":"This tutorial will take ~30 minutes or so. We'll take a Gatsby starter and add comment support via StaticMan. Prerequisites To follow this…","timeToRead":7,"frontmatter":{"date":"June 05, 2018","path":"/tutorial-comments-with-staticman-in-gatsby","title":"Tutorial: Comments with StaticMan in Gatsby"}},"relatedPosts":null,"allCommentsJson":{"edges":[{"node":{"id":"ea3a8d33-e5ac-5d74-b932-e2e44573c790","name":"Sukil","date":1529053435,"fields":{"messageHtml":"<p>Hi, may I suggest adding labels to the tutorial form, just as in here? They're good for accessibility purposes. (Also, this field doesn't have a label associated with it). Thanks!</p>"}}},{"node":{"id":"b914678c-c36c-5e0e-affe-5046c54848c3","name":"Callum Macdonald","date":1529079558,"fields":{"messageHtml":"<p>Labels sounds like a great idea. I'm not 100% sure what you mean exactly about putting them in the tutorial itself though. Can you share any  more info on that? Maybe we can do a tutorial on labels in Gatsby! :-)</p>"}}},{"node":{"id":"31efee8a-949e-5ae0-8501-684fb0e5c0cd","name":"Stephan Weinhold","date":1534700534,"fields":{"messageHtml":"<p>Great tutorial! This helped me a lot, thanks. But (and there's always a but): you say \"NOTE The filename must be exactly staticman.yml, not .yaml or otherwise.\" just to tell us \"you should see the staticman.yaml file\" a few lines below. Guess that's a typo, isn't it?</p>"}}}]},"allRatingsJson":{"totalCount":9,"edges":[{"node":{"id":"7e589d1d-4bdf-55c0-8c62-0012466b72cd","rating":"5"}},{"node":{"id":"58a781c2-6e4a-5431-b5bd-763ca94ae6ac","rating":"5"}},{"node":{"id":"c5807c06-e772-5202-90f9-a5158ed38f2e","rating":"4"}},{"node":{"id":"5034e08a-3154-59ca-b6be-02ab242ca5b6","rating":"3"}},{"node":{"id":"e97d8804-d993-5d71-8456-255ae05a2f7b","rating":"4"}},{"node":{"id":"ad0271a6-98dd-596e-a894-fd6290b465e2","rating":"5"}},{"node":{"id":"1ec4636f-3d0c-58bc-b5d1-be4fd2736520","rating":"5"}},{"node":{"id":"b7dd6ec4-f100-52ad-91f3-8ec3c35572a7","rating":"4"}},{"node":{"id":"50b3b725-d40d-5247-b7f9-15ff89460424","rating":"4"}}]}},"pageContext":{"tags":[]}}