|
| 1 | +--- |
| 2 | +title: Porting a Jekyll Site to Gatsby |
| 3 | +--- |
| 4 | + |
| 5 | +# Jekyll |
| 6 | + |
| 7 | +[Jekyll](https://jekyllrb.com/) is a static site generator for your websites. Think of it like a file-based CMS. It takes your content, renders Markdown, Liquid templates, and spits out a complete, static website ready to be served by [Apache](https://www.apache.org/), [Nginx](https://www.nginx.com/) or another web server. In Jekyll if you write a markdown file in the root folder under `_site` thay will considered `pages`, meanwhile if you write `.md` files in `_posts` folder they will considered `posts` that's how jekyll works. |
| 8 | + |
| 9 | +## What changes need to be made? |
| 10 | + |
| 11 | +In order to transition your codebase over to using Gatsby, a few things need to be taken care of to account for the differences between how the projects are set up. First of all Gatsby want to know where to read `markdown` files by using the plugin [`gatsby-source-filesystem`](https://www.gatsbyjs.org/packages/gatsby-source-filesystem/) `( source plugin for sourcing data into your Gatsby application)` but how does it understand markdown format for this we need to use the plugin [`gatsby-transformer-remark`](https://www.gatsbyjs.org/packages/gatsby-transformer-remark/) to transform markdown files into something like gatsby can understand. This plugin is the responsible to read `markdown` files a extract information like the `frontmatter`, the excerpt and convert the `text` into `HTML` ready to be rendered. |
| 12 | + |
| 13 | +<!-- The plugin gatsby-transformer-remark is really usefull and also has many plugins to use within it. --> |
| 14 | + |
| 15 | +## Development environment |
| 16 | + |
| 17 | +Gatsby generates websites and web applications for production through a compilation and build process, and it also has tools optimized for local development. To set up the Gatsby [CLI](/docs/glossary#cli) and development environment (if you haven't already) check out [Part Zero of the Gatsby tutorial](/tutorial/part-zero/). |
| 18 | + |
| 19 | +## Transition to Gatsby |
| 20 | + |
| 21 | +Gatsby can help you set up an application and removes much of the configuration headache. However, Gatsby offers some additional advantages like performance optimizations with static rendering and a thriving ecosystem of plugins, To get things set up and running we need to structure our jekyll-site files like this. |
| 22 | + |
| 23 | +``` |
| 24 | +jekyll-site |
| 25 | + ├── _includes |
| 26 | + │ ├── footer.html |
| 27 | + │ └── header.html |
| 28 | + ├── _layouts |
| 29 | + │ ├── default.html |
| 30 | + │ └── post.html |
| 31 | + ├── _posts |
| 32 | + │ ├── 2007-10-29-why-every-programmer-should-play-nethack.md |
| 33 | + │ └── 2009-04-26-barcamp-boston-4-roundup.md |
| 34 | + ├── _sass |
| 35 | + ├── _drafts |
| 36 | + │ ├── begin-with-the-crazy-ideas.md |
| 37 | + │ └── on-simplicity-in-technology.md |
| 38 | + ├── _site |
| 39 | + │ ├── about.md |
| 40 | + │ └── blah-blah.md |
| 41 | + ├── script |
| 42 | + ├── .jekyll-metadata |
| 43 | + ├── _config.yml |
| 44 | + └── index.md |
| 45 | +``` |
| 46 | + |
| 47 | +### Migrating the markdown / Liquid templates pages |
| 48 | + |
| 49 | +Create a new folder in Gatsby project under the `src/pages-markdown` for those pages written in markdown format. I left the `src/pages` as default so in future we need it for some javaScript files. Now its time to transfer all markdown files to `src/pages-markdown` like this. |
| 50 | + |
| 51 | +``` |
| 52 | + src |
| 53 | + └── pages-markdown |
| 54 | + ├── footer.html |
| 55 | + └── header.html |
| 56 | +``` |
| 57 | + |
| 58 | +In addition, these pages must be have next properties within the frontmatter section: |
| 59 | + |
| 60 | +```md |
| 61 | +--- |
| 62 | +layout: page |
| 63 | +title: About |
| 64 | +date: 2010-11-23 14:48 |
| 65 | +path: /about |
| 66 | +--- |
| 67 | +``` |
| 68 | + |
| 69 | +Now its time to make Gatsby engine take account into these files to inform where to read them: |
| 70 | + |
| 71 | +```json |
| 72 | +// gatsby-config.js |
| 73 | +{ |
| 74 | + "resolve": `gatsby-source-filesystem`, |
| 75 | + "options": { |
| 76 | + "name": `markdown-pages`, |
| 77 | + "path": `${__dirname}/src/pages-markdown/` |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +For each file found at `src/pages-markdown` Gatsby will create a `node`. Now, we can extend the `createPages` method and make Gatsby create a new page for each `node`. The steps can be summarized as: |
| 83 | +In addition, these pages must be have next properties within the `frontmatter` section: |
| 84 | + |
| 85 | +- Query for all the markdown nodes read by gatsby |
| 86 | +- Filter those nodes with `layout=page` |
| 87 | +- Create a new page invoking `action.createPage` method and using the template file src/templated/page.js |
| 88 | + |
| 89 | +```js |
| 90 | +// gatsby-node.js |
| 91 | +exports.createPages = ({ graphql, actions }) => { |
| 92 | + const { createPage } = actions |
| 93 | + const pageTemplate = path.resolve(`src/templates/page.js`) |
| 94 | + |
| 95 | + return graphql( |
| 96 | + ` |
| 97 | + { |
| 98 | + allMarkdownRemark { |
| 99 | + edges { |
| 100 | + node { |
| 101 | + fields { |
| 102 | + slug |
| 103 | + } |
| 104 | + frontmatter { |
| 105 | + path |
| 106 | + layout |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + ` |
| 113 | + ).then(result => { |
| 114 | + if (result.errors) { |
| 115 | + return Promise.reject(result.errors) |
| 116 | + } |
| 117 | + |
| 118 | + const markdownItems = result.data.allMarkdownRemark.edges |
| 119 | + |
| 120 | + // Create pages and blog post pages |
| 121 | + markdownItems.forEach(({ node }) => { |
| 122 | + if (node.frontmatter.layout === "page") { |
| 123 | + createPage({ |
| 124 | + path: node.frontmatter.path, |
| 125 | + component: pageTemplate, |
| 126 | + }) |
| 127 | + } |
| 128 | + }) |
| 129 | + }) |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +### Migrating blog posts |
| 134 | + |
| 135 | +Now its time to transfer all the blog posts in to `src/content` folder. |
| 136 | + |
| 137 | +``` |
| 138 | + src |
| 139 | + └── content |
| 140 | + ├── 2007-10-29-why-every-programmer-should-play-nethack.md |
| 141 | + └── 2009-04-26-barcamp-boston-4-roundup.md |
| 142 | +``` |
| 143 | + |
| 144 | +We need to tell gatsby where to find blog posts. |
| 145 | + |
| 146 | +```js |
| 147 | +// gatsby-config.js |
| 148 | +{ |
| 149 | + resolve: 'gatsby-source-filesystem', |
| 150 | + options: { |
| 151 | + name: 'blog', |
| 152 | + path: `${__dirname}/src/content/`, |
| 153 | + }, |
| 154 | +}, |
| 155 | +``` |
| 156 | + |
| 157 | +In Jekyll when we create a new blog post we create something like that `YYYY-MM-DD-some-title.md` so we need to create that `slug` from the file name. The way we can do is using the `onCreateNode` method |
| 158 | + |
| 159 | +```js |
| 160 | +// gatsby-node.js |
| 161 | +exports.onCreateNode = ({ node, getNode, actions }) => { |
| 162 | + const { createNodeField } = actions |
| 163 | + |
| 164 | + if (node.internal.type === `MarkdownRemark`) { |
| 165 | + const filename = createFilePath({ node, getNode, basePath: `blog` }) |
| 166 | + |
| 167 | + // Blog files must have format name YYYY-MM-DD-title.md |
| 168 | + if (node.frontmatter.layout === "post") { |
| 169 | + const match = filename.match(/^\/([\d]{4}-[\d]{2}-[\d]{2})-{1}(.+)\/$/) |
| 170 | + if (match) { |
| 171 | + const [, date, title] = match |
| 172 | + if (!date || !title) { |
| 173 | + console.error( |
| 174 | + `Invalid filename ${filename}. Change name to start with a valid date and title` |
| 175 | + ) |
| 176 | + } else { |
| 177 | + const slug = `/blog/${slugify(date, "/")}/${title}/` |
| 178 | + createNodeField({ |
| 179 | + node, |
| 180 | + name: `slug`, |
| 181 | + value: slug, |
| 182 | + }) |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | +Now we need to tell gatsby to create a page for each blog post `node`. We can do it extending a bit the previous `createPages` method |
| 191 | + |
| 192 | +```js |
| 193 | +exports.createPages = ({ graphql, actions }) => { |
| 194 | + const { createPage } = actions |
| 195 | + const blogPostTemplate = path.resolve(`src/templates/blog-post.js`) |
| 196 | + const pageTemplate = path.resolve(`src/templates/page.js`) |
| 197 | + |
| 198 | + return graphql(`...`).then(result => { |
| 199 | + if (result.errors) { |
| 200 | + return Promise.reject(result.errors) |
| 201 | + } |
| 202 | + |
| 203 | + const markdownItems = result.data.allMarkdownRemark.edges |
| 204 | + |
| 205 | + // Create pages and blog post pages |
| 206 | + markdownItems.forEach(({ node }) => { |
| 207 | + if (node.frontmatter.layout === "page") { |
| 208 | + createPage({ |
| 209 | + path: node.frontmatter.path, |
| 210 | + component: pageTemplate, |
| 211 | + }) |
| 212 | + } else if (node.frontmatter.layout === "post") { |
| 213 | + createPage({ |
| 214 | + path: node.fields.slug, |
| 215 | + component: blogPostTemplate, |
| 216 | + context: { |
| 217 | + slug: node.fields.slug, |
| 218 | + }, |
| 219 | + }) |
| 220 | + } |
| 221 | + }) |
| 222 | + }) |
| 223 | +} |
| 224 | +``` |
| 225 | + |
| 226 | +### Pagination with the blog posts |
0 commit comments