33
loading...
This website collects cookies to deliver better user experience
// JSX syntax is coool!
let Site = (data) => {
return <div>Hello {data.name}!</div>
}
## This is a heading
This is a paragraph wil _emphasized_ and **strongly emphasized** text. And this is [a link to Google](https://google.com)
1. This is an ordered list
2. Another list item
3. - A nested unordered list
- Another list item.
npm
and we'll use it to install Explosiv.explosiv-blog
for mine. Open the folder in your favorite shell (or command prompt or command line interface). You'll first need to initialize the folder as a NodeJS project.npm init -y
package.json
that will be used to identify your app and manage your dependencies. The next step is to install Explosiv.npm install explosiv
pages
at the root of your project. This folder will hold all Explosiv pages.index.js
for our homepage. index
is a special name as it denotes that this file will be the first one that the user sees when they visit our site for the first time. Add some simple JSX to our index page to show a warm welcome message to visitors of the blog.// index.js
let Homepage = () => {
return <>
<Head>
<title>my blog</title>
<meta name="description" content="This is my own blog"/>
</Head>
<main>
<h1>Welcome to my blog</h1>
<p>This is my cool new blog built with Explosiv. <a href="/about">About Me</a></p>
</main>
</>
};
export default Homepage;
npx explosiv dev
about.js
. This will be our about page and it will be accessible at /about
on our website. Add some JSX for the about page as well.// about.js
let About = () => {
return <>
<Head>
<title>about my blog</title>
<meta name="description" content="About my blog"/>
</Head>
<main>
<h1>About my blog</h1>
<p>Hey there! Welcome to my new blog built with Explosiv. Here you can find all my blog posts. <a href="/">Go back to homepage</a></p>
</main>
</>
};
export default About;
ProTip: Creating a page at pages/about.js
is equivalent to creating one at pages/about/index.js
.
public/
and create a stylesheet at public/app.css
. Files in the public/
folder will be publicly accessible so you can visit http://localhost:3000/app.css to view the stylesheet./* public/app.css */
body {
max-width: 600px;
padding: 0 20px;
margin: 0 auto;
font-family: system-ui, -apple-system, "Segoe UI", "Roboto", "Ubuntu", "Cantarell", "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"
}
pages/_document.js
to customize the overall behavior of the blog.// pages/_document.js
let Document = () => {
return (<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width,initial-scale=1"/>
<link rel="stylesheet" href="/app.css"/>
</head>
<body>
<div class="root"></div>
</body>
</html>)
}
export default Document;
_document.js
file is a special one because it provides a wrapper to the whole site, hence it can be used to customize the site. Let's explain the components of this _document.js
:<html lang="en">
specify the language of our site.<meta charset="utf-8"/>
to specify the character set of our site to prevent incorrect renderings of our site's text.<meta name="viewport">
to correctly scale the site for mobile users.<link rel="stylesheet">
to allow web browsers to fetch our stylesheet.<div class="root"></div>
where the main page's content will be rendered.blog/first-post.md
:---
title: My First Blog Post
description: "The first blog post to be created on this site."
created: 1639915508100
---
Hello people, a warm welcome to you. This is the first blog post on this site.
Note: Notice the block at the beginning of the file that starts and ends with ---
. This is called Front Matter and is used to describe the blog posts like who wrote it, the title, when was it written etc. Also, be sure to update the created
field to show the real time you created it. View time in terms of milliseconds after the UNIX epoch.
npm install front-matter marked
front- matter
: Allows use to parse page's front matter.marked
: Allows use to parse Markdown files into HTML.src/posts.js
that loads all blog posts then give us info about them.// src/posts.js
// Import dependencies
let path = require("path");
let fs = require("fs");
let fm = require("front-matter");
// This function resolves where files or folders are relative to the `cwd` or current working directory.
let resolve = (...link) => path.resolve(process.cwd(), ...link);
// Where all our blog posts are stored
let blogsPath = resolve("blog");
let blogs = fs
// Get all blog posts in the `blogsPath` folder.
.readdirSync(blogsPath)
.map((blog) => {
// Get the slug. i.e `first-post` from `first-post.md`
let slug = blog.replace(/\.md$/, "");
// And return an array of posts and their front matter
// Example: [ "first-post", { title: "My First Blog Post", created: 1639915508100, description: "..." } ]
return [
slug,
{ slug, ...fm(fs.readFileSync(resolve(blogsPath, blog), "utf8")).attributes },
]
})
// Sort the blog posts by date created
.sort(([_, a], [$, b]) => b.created - a.created);
// Export the posts as an object
module.exports = Object.fromEntries(blogs);
components/posts.js
that uses the post data to display a list of info about posts.// components/posts.js
// Load the posts as an object.
import postsJSON from "../src/posts";
let PostsCard = ({ ...props }) => {
// Convert the posts object into an array.
let posts = Object.entries(postsJSON)
return (
<p>
<h2>Posts</h2>
<div className="posts">
{/* Display the posts one by one */}
{/* Display each post's title, date of creation and description with a link to read the post */}
{posts.map(([slug, { title, description, created }]) => (
<p>
<a href={"/post/" + slug}>{title} →</a><br/>
<small>{new Date(created).toDateString()}</small><br/>
<span>{description}</span>
</p>
))}
</div>
</p>
);
};
export default PostsCard;
pages/index.js
to show blog posts using the newly created component on the homepage.// index.js
import PostsCard from "../components/posts.js";
let Homepage = () => {
return <>
<Head>
<title>my blog</title>
<meta name="description" content="This is my own blog"/>
</Head>
<main>
<h1>Welcome to my blog</h1>
<p>This is my cool new blog built with Explosiv. <a href="/about">About Me</a></p>
<PostsCard/>
</main>
</>
};
export default Homepage;
/pages/post/first-blog.js
and /pages/post/second-blog.js
etc. However, there is a feature called Dynamic Pages that simplify the development of related pages. We will be creating one single dynamic page at /pages/post/[slug].js
that will render each post according to the [slug]
provided. For example, visiting /post/first-blog
will render /pages/post/[slug].js
with a slug
that is equal to first-blog
.Note: You can change [slug]
to any other name you like. For example [id]
or [post]
. However, it is important to enclose the slug in brackets ([]
).
// pages/post/[slug].js
// Import dependencies, will be used later
import { promises as fs } from 'fs'
import path from 'path'
import matter from 'front-matter'
import { marked } from 'marked'
// The Post component will be used to render each post
const Post = ({ post }) => (
<>
{/* Add a HEAD that shows the title of the page and expose the description of the post */}
<Head>
<title>{post.attributes.title} - vixalien</title>
<meta name="description" content={post.attributes.description} />
</Head>
<main>
{/* Show a link to the homepage */}
<div style={{marginTop:"20px"}}><a href="/">Homepage</a><br/><br/></div>
<small>{new Date(post.attributes.created).toDateString()}</small>
<h1>{post.attributes.title}</h1>
<p>{post.attributes.description}</p>
<div>===</div>
<br/>
{/* Render the post's content as HTML in an `article` tag */}
<article html={post.content}/>
</main>
</>
)
export default Post;
getPaths
and it is used to determine the number of all acceptable paths (or slugs). For example, it can be used to allow /post/first-blog
to be rendered and /post/unknown-post
to return a 404 page (Not Found). In our case, it's pretty straightforward to know the range of acceptable slugs. We just read the blog
folder and see which blog posts are there:// Append to the end of `pages/post/[slug].js`
export const getPaths = async () => {
// Read all files in the `blog` folder.
const files = await fs.readdir(path.resolve('blog'))
// Remove the training extensions like `.md` (remove the 3 last characters of filename)
return files.map((filename) => filename.slice(0, filename.length - 3))
}
getProps
to read info about the post themselves given the slug. The getProps
function is provided with a slug
and use it to get information that will be passed to default export of the function (as props.)// Append to the end of `pages/post/[slug].js`
export const getProps = async (slug) => {
// Read the file named `slug`+.md in the `blog` directory with the utf-8 format.
let post = await fs.readFile(path.join('blog', `${slug}.md`), 'utf-8')
// uses the `front-matter` package to get the post's attributes.
post = matter(post)
// parse the post's body to get the raw HTML content.
post.content = marked(post.body)
// Return an object that will be passed onto the default page export.
return { post }
}
first-blog
.