mirror of
https://github.com/kremalicious/blog.git
synced 2024-12-22 17:23:50 +01:00
refactor
This commit is contained in:
parent
c271ab750e
commit
ae9030ef6d
@ -9,59 +9,68 @@ const Fraction = require('fraction.js')
|
||||
const meta = yaml.load(fs.readFileSync('./content/meta.yml', 'utf8'))
|
||||
const { itemsPerPage } = meta
|
||||
|
||||
// Create slug & date for posts from file path values
|
||||
exports.onCreateNode = ({ node, actions, getNode }) => {
|
||||
const { createNodeField } = actions
|
||||
|
||||
// Markdown files
|
||||
if (node.internal.type === 'MarkdownRemark') {
|
||||
const fileNode = getNode(node.parent)
|
||||
const parsedFilePath = path.parse(fileNode.relativePath)
|
||||
const slugOriginal = createFilePath({ node, getNode })
|
||||
|
||||
// slug
|
||||
let slug
|
||||
|
||||
if (parsedFilePath.name === 'index') {
|
||||
slug = `/${parsedFilePath.dir.substring(11)}` // remove date from file dir
|
||||
} else {
|
||||
slug = `/${slugOriginal.substring(12)}` // remove first slash & date from file path
|
||||
}
|
||||
|
||||
createNodeField({
|
||||
node,
|
||||
name: 'slug',
|
||||
value: slug
|
||||
})
|
||||
|
||||
// date
|
||||
let date
|
||||
|
||||
if (node.frontmatter.date) {
|
||||
date = `${node.frontmatter.date}`
|
||||
} else {
|
||||
date = `${slugOriginal.substring(1, 10)}` // grab date from file path
|
||||
}
|
||||
|
||||
createNodeField({
|
||||
node,
|
||||
name: 'date',
|
||||
value: date
|
||||
})
|
||||
createMarkdownNodeFields(node, createNodeField, getNode)
|
||||
}
|
||||
|
||||
// exif
|
||||
// Image files
|
||||
if (node.internal.mediaType === 'image/jpeg') {
|
||||
fastExif
|
||||
.read(node.absolutePath, true)
|
||||
.then(exifData => {
|
||||
if (!exifData) return
|
||||
generateExif(exifData, createNodeField, node)
|
||||
})
|
||||
.catch(() => null) // just silently fail when exif can't be extracted
|
||||
readAndCreateExifFields(node, createNodeField)
|
||||
}
|
||||
}
|
||||
|
||||
const generateExif = (exifData, createNodeField, node) => {
|
||||
// Create slug & date for posts from file path values
|
||||
const createMarkdownNodeFields = (node, createNodeField, getNode) => {
|
||||
const fileNode = getNode(node.parent)
|
||||
const parsedFilePath = path.parse(fileNode.relativePath)
|
||||
const slugOriginal = createFilePath({ node, getNode })
|
||||
|
||||
// slug
|
||||
let slug
|
||||
|
||||
if (parsedFilePath.name === 'index') {
|
||||
slug = `/${parsedFilePath.dir.substring(11)}` // remove date from file dir
|
||||
} else {
|
||||
slug = `/${slugOriginal.substring(12)}` // remove first slash & date from file path
|
||||
}
|
||||
|
||||
createNodeField({
|
||||
node,
|
||||
name: 'slug',
|
||||
value: slug
|
||||
})
|
||||
|
||||
// date
|
||||
let date
|
||||
|
||||
if (node.frontmatter.date) {
|
||||
date = `${node.frontmatter.date}`
|
||||
} else {
|
||||
date = `${slugOriginal.substring(1, 10)}` // grab date from file path
|
||||
}
|
||||
|
||||
createNodeField({
|
||||
node,
|
||||
name: 'date',
|
||||
value: date
|
||||
})
|
||||
}
|
||||
|
||||
const readAndCreateExifFields = (node, createNodeField) => {
|
||||
fastExif
|
||||
.read(node.absolutePath, true)
|
||||
.then(exifData => {
|
||||
if (!exifData) return
|
||||
createExifFields(exifData, createNodeField, node)
|
||||
})
|
||||
.catch(() => null) // just silently fail when exif can't be extracted
|
||||
}
|
||||
|
||||
const createExifFields = (exifData, createNodeField, node) => {
|
||||
const { Model } = exifData.image
|
||||
const {
|
||||
ISO,
|
||||
|
@ -9,17 +9,21 @@ import Rss from '../svg/Rss'
|
||||
import Jsonfeed from '../svg/Jsonfeed'
|
||||
|
||||
const NetworkIcon = ({ link }) => {
|
||||
let Icon
|
||||
|
||||
if (link.includes('twitter')) {
|
||||
return <Twitter className={styles.twitter} />
|
||||
Icon = <Twitter className={styles.twitter} />
|
||||
} else if (link.includes('github')) {
|
||||
return <Github className={styles.github} />
|
||||
Icon = <Github className={styles.github} />
|
||||
} else if (link.includes('facebook')) {
|
||||
return <Facebook className={styles.facebook} />
|
||||
Icon = <Facebook className={styles.facebook} />
|
||||
} else if (link.includes('feed.xml')) {
|
||||
return <Rss className={styles.rss} />
|
||||
Icon = <Rss className={styles.rss} />
|
||||
} else if (link.includes('feed.json')) {
|
||||
return <Jsonfeed className={styles.json} />
|
||||
Icon = <Jsonfeed className={styles.json} />
|
||||
}
|
||||
|
||||
return Icon
|
||||
}
|
||||
|
||||
const IconLinks = ({ links }) => (
|
||||
|
Loading…
Reference in New Issue
Block a user