Skip to content

Instantly share code, notes, and snippets.

@canertuzunar
Created April 14, 2021 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save canertuzunar/7e5164a9e2400241f5c79b505a6e4bae to your computer and use it in GitHub Desktop.
Save canertuzunar/7e5164a9e2400241f5c79b505a6e4bae to your computer and use it in GitHub Desktop.
const path = require('path')
const { createFilePath } = require(`gatsby-source-filesystem`)
/* adi ilk gonderi olan bir markdown dosyasi oldugunu varsayalim. bu dosyayi linklerken
kullanilacak olan patterndir dosyanin linkini /ilk-gonderi olarak olusturacaktir*/
exports.onCreateNode =({ node, getNode, actions}) => {
const { createNodeField } = actions
if(node.internal.type === `MarkdownRemark`) {
const slug = createFilePath({ node, getNode, basePath: `pages`})
createNodeField({
node,
name: `slug`,
value: slug
})
}
}
exports.createPages = async ({graphql, actions, reporter}) => {
const { createPage } = actions
//olusturdugumuz blog sayfasinda kullanmak uzere sorgu hazirliyoruz
const result = await graphql(`
{
allMarkdownRemark(limit:1000) {
edges{
node{
fields{
slug
}
frontmatter{
title
date
description
}
html
rawMarkdownBody
}
}
}
}
`)
//Olusabilecek hatalari gormek icin reporter kullaniyoruz
if(result.errors){
reporter.panicOnBuild(`Graphql Sorgusunda bir hata olustu. ${result.errors}`)
}
//Olusturulan her post dosyasi icin blog sayfasi olusturuyoruz
const blogPostTemplate = path.resolve(`./src/templates/blog.jsx`)
result.data.allMarkdownRemark.edges.forEach( ({node}) => {
const path = node.fields.slug
createPage({
path, //md dosyasi gatsby sayfasi haline geldikten sonra olusacak linki
component: blogPostTemplate, /* md dosyasi goruntulenirken kullanilacak
olan react componenti*/
context: {
pagePath: path,
html: node.html,
frontmatter: node.frontmatter
} /* olusan gatsby sayfalarinin alacagi propslari belirliyoruz */
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment