| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
A static site generator based in Next.js
nextein is a wrapper around next.js that allows you to write static sites using markdown and react.
NodeJS v10.x+ is required to run nextein commands.
If you want to jump into a starter project check nextein-starter
There are a few steps you have to follow to get your site up and running with nextein
Create a project:
Install Dependencies
Add a next.config.js config file
const { withNextein } = require('nextein/config')
module.exports = withNextein({
})Create pages/index.js
import React from 'react'
import { getPosts } from 'nextein/fetcher'
import Content from 'nextein/content'
export async function getStaticProps () {
return {
props: {
posts: await getPosts()
}
}
}
export default function Index ({ posts }) {
return (
<section>
{
posts.map(post => <Content {...post} />)
}
</section>
)
})Create a markdown post entry under posts folder (posts/my-first-post.md)
---
title: First Post
category: post
---
This is the first paragraph and it will be used as an excerpt when loaded in a `<Content excerpt />` tag.
This paragraph should *not* appear in that list.
Add npm scripts to run dev mode to your package.json
"scripts": {
"dev": "next"
}Run the development server
Add another npm script to your package.json to export the site
"scripts": {
"dev": "next",
"export": "next build && next export"
}Use fetcher to retrieve the posts and data from your markdown files.
The getPostsFilterBy and getDataFilterBy methods in fetcher allows to pass filter functions. For instance, we can use inCategory filter to retrieve posts in a given category:
import { getPostsFilterBy } from 'nextein/fetcher'
import { inCategory } from 'nextein/filters'
//...
const blog = await getPostsFilterBy(InCategory('blog'))The getData and getDataFilterBy will retrieve only the metadata generated for entries instead of the entire post.
The fetcher method is a convenient way to define a filter and then use the getPosts and getData with a filter applied.
import fetcher from 'nextein/fetcher'
import { inCategory } from 'nextein/filters'
//...
const { getPosts } = fetcher(InCategory('blog'))
//...
const blog = await getPosts()You can use Dynamic Routes and static generator functions (getStaticProps and getStaticPaths) with fetcher methods.
Example for a [name].js dynamic route
import fetcher from 'nextein/fetcher'
const { getData, getPost } = fetcher(/* filter */)
export async function getStaticPaths () {
const data = await getData()
return {
paths: data.map(({ name }) => ({ params: { name } })),
fallback: false
}
}
export async function getStaticProps ({ params }) {
const post = await getPost(params)
return { props: { post } }
}
export default function Post ({ post }) {
//...
}Example for a [[...name]].js dynamic route:
import fetcher from 'nextein/fetcher'
import { inCategory } from 'nextein/filters'
const { getData, getPosts, getPost } = fetcher(inCategory('guides'))
export async function getStaticPaths () {
const data = await getData()
return {
paths: [{ params: { name: [] } },
...data.map(({ name }) => ({ params: { name: [name] } }))
],
fallback: false
}
}
export async function getStaticProps ({ params }) {
const posts = await getPosts()
const post = await getPost(params) // This can be null if not matching `...name`
return { props: { posts, post } }
}
export default function Guides ({ posts, post }) {
//...
}Filter function to be applied to posts to retrieve posts in a given category.
Categories are resolved by the folder structure by default. This means that a post located at posts/categoryA/subOne will have a category categoryA/subOne unless you specify the category name in frontmatter.
import { getPosts } from 'nextein/fetcher'
import { inCategory } from 'nextein/filters'
//...
const posts = await getPosts()
const homePosts = posts.filter(inCategory('home'))
If you want to retrieve all posts under a certain category, let's say categoryA which will include all those under subOne, use the options includeSubCategories: true.
import { inCategory } from 'nextein/filters'
const categoryAPosts = posts
.filter(inCategory('categoryA', { includeSubCategories: true }))Component to render a post object. This component receives the content from the post as a property. Use the excerpt property to only render the first paragraph (this is useful when rendering a list of posts).
import Content from 'nextein/content'
//...
export default function PostPage ({ post }) {
return <Content {...post} />
} Using renderers to change/style the <p> tag
const Paragraph = ({ children }) => (<p style={{padding:10, background: 'silver'}}> { children } </p> )
// Then in your render method ...
<Content
{...post}
renderers={{
p: Paragraph
}}
/>{ data, content } = postThere are only a few defined properties in the frontmatter metadata that is used by nextein
---
category: categoryOne
date: 2017-06-23
---
Post Content...
A wrapper configuration function to be applied into the next.config.js. It provides a way to add your own next.js config along with nextein internal next.js config.
next.config.js
const { withNextein } = require('nextein/config')
module.exports = withNextein({
// Your own next.js config here
})You can also define nextein plugins using the withNextein configuration:
const { withNextein } = require('nextein/config')
module.exports = withNextein({
nextein: {
plugins: [
//your nextein plugins here
]
}
// Your own next.js config here
})The nextein.plugins configuration accepts an array of plugins with the following formats:
The plugin name should be a pre-installed plugin (nextein-plugin-markdown) , or a local file (./myplugins/my-awesome-plugin)
The default configuration includes:
plugins: [
['nextein-plugin-source-fs', { path: 'posts', data: { page: 'post' } }],
'nextein-plugin-markdown',
'nextein-plugin-filter-unpublished'
]Read files from file system.
Options:
'**/.DS_Store',
'**/.gitignore',
'**/.npmignore',
'**/.babelrc',
'**/node_modules',
'**/yarn.lock',
'**/package-lock.json'Render markdown files.
Options:
Filter posts by using a property to prevent draft / unpublished entries to be displayed.
Options:
You can write your own plugins. There are basically 2 different types (source and transforms). Source plugins will be called to generate the posts entries and then the transform plugins will receive those entries and can modify, filter, append, or transform in anyway the posts list.
| Back | FazBrowse Home | New Git URL |