相信各位github资深玩家们都有本身基于 github pages
搭建的我的站点。官方推荐的静态站点生成器是 Jekyll
,关于 Jekyll
的使用感兴趣的各位请自行 google,这里就不赘述了。本文主要介绍下基于 Create-React-App
搭建我的博客的相关实践,可能更适合作前端开发的伙伴。javascript
github pages
是 github
推出的静态站点服务,主要的用途在于使用你在 github
仓库中的代码构建你本身的静态站点,为用户提供 github.io
二级域名,您也能够经过添加DNS的 CNAME
记录来绑定本身的域名。css
github pages
最简单粗暴的方法就是直接往 github 上方静态页面了,建立一个名为 [您的github帐号名].github.io
的github仓库,将您的index.html页面代码扔进master分支,就能够直接经过 https://[您的github帐号名].github.io
访问到您的站点了。html
对于一个简单的我的博客站点来讲,存在如下基本功能特性:前端
下面介绍基于React如何实现一个简单的静态博客。java
使用 Create-React-App(如下简称CRA) 的generator建立一个React前端项目骨架。对此项目进行必定改造以方便咱们平常的开发和使用习惯:node
react-app-rewired
来调整CRA中webpack的配置
core-js
对浏览器版本进行向下兼容antd
设计语言(React组件)快速实现业务UIaxios
实现先后端的数据请求我的改造后的项目代码在这里,您能够直接fork或者down下来使用。react
通常的静态博客系统(如gatsby),会给用户提供一个用于建立新文章的交互式命令行,效果大体以下:webpack
相似功能可使用nodejs中readline模块的原生方法来实现。这里推荐一个第三方工具:inquirer,本质上是对readline模块进行了加强,提供了不少实用的方法用于交互式命令行开发,实现的用户界面(命令行)也比较友好。ios
对于上面GIF示例的功能,其代码实现以下:git
// newPost.js
const inquirer = require('inquirer');
const moment = require('moment');
const questions = [
{
type: 'input',
name: 'post_name',
message: '请输入您的文章别名(用于建立文章目录,仅限英文,单词间用短横杠‘-’链接):',
validate: value => {
if (/(\.|\*|\?|\\|\/)/gi.test(value)) {
return '文章别名不得包含特殊符号(.*?\\/),请从新输入↑↑';
}
if (/(([A-z]+-)+)?[A-z]+/gi.test(value)) {
return true;
}
return '文章别名不合法,请从新输入↑↑';
},
filter: value => value.replace(/\s+/gi, '-'),
},
{
type: 'input',
name: 'create_at',
message: '请输入文章的发布时间(或者按回车键使用默认值):',
default: () => {
return moment().format('YYYY-MM-DDThh:mm:ss');
},
validate: value => {
if (/\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d/gi.test(value)) {
return true;
}
return '时间格式不合法,请从新输入↑↑';
},
},
];
inquirer
.prompt(questions)
.then(answers => {
// 获取用户输入
const { post_name, create_at } = answers;
/* 此处作一些命令行反馈和过程性的工做 */
/* (如:提示用户输入是否合法、建立文章对应的目录和文件等等) */
})
.catch(err => {
/* 异常处理 */
});
复制代码
如是,将此node脚本添加到项目package.json
的scripts
中(如:new-post: "node newPost.js"
),便可经过npm run
命令执行。
为使用markdown文档来编辑、存储博客的文章内容,须要将md文档转换为react的JSX对象以渲染到网页中。在此推荐使用react-markdown,功能很6,做者维护得也比较勤。
使用方式以下:
import ReactMarkdown from 'react-markdown';
<ReactMarkdown source={'# 这是文章标题\n\n'} /> // <h1>这是文章标题</h1> 复制代码
react-markdown提供了一个renderers属性,用户能够传入一系列renderer组件来自定义文章中一些内容的渲染方式(有兴趣的童鞋能够看下包做者对默认renderer的实现)。
如:自定义md中图片的渲染方式(用法以下)。
// 传入renderer的方式
<ReactMarkdown
source={'[md文本内容]'}
renderers={{
image: ImageRenderer,
}}
/>
复制代码
// ImageRenderer的实现
import React, { Component } from 'react';
import PropTypes from 'prop-types';
class ImageRenderer extends Component {
static propTypes = {
src: PropTypes.string.isRequired,
};
render() {
return (
<img className="post-content-image" src={this.props.src} alt={this.props.src} /> ); } } export default ImageRenderer; 复制代码
与此相似,咱们能够经过传入一个自定义的renderer来实现文章中代码块的语法高亮。名为CodeBlock
的renderer实现以下:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { highlight, languages } from 'prismjs';
import ReactHtmlParser from 'react-html-parser';
import 'prismjs/themes/prism.css';
export class HtmlComponent extends Component {
static propTypes = {
html: PropTypes.string.isRequired,
};
render() {
return ReactHtmlParser(this.props.html);
}
}
export class CodeBlock extends Component {
static propTypes = {
literal: PropTypes.string.isRequired,
language: PropTypes.string.isRequired,
};
render() {
const html = highlight(this.props.literal, languages[this.props.language]);
const cls = `language-${this.props.language}`;
return (
<pre className={cls}> <code className={cls}> <HtmlComponent html={html} /> </code> </pre> ); } } export default CodeBlock; 复制代码
此处用到了prismjs和react-html-parser两个npm包,前者用于将代码文本转化为html文本,后者用于将html文本转化为React的JSX对象以传入React组件(这样作比直接使用dangerouslySetInnerHTML属性更安全些)。
一个友好的站点确定少不了导航菜单(或文章的分类菜单),本人的实现方式是直接使用文章的“标签”来进行分类统计,并生成站点的顶部导航,效果以下:
为此,须要撰写必定的脚本实现文章的分类统计和打包,我的的实现方式是将统计结果和文章内容各自打包为json文件,经过前端组件请求数据并加载。
导航栏组件的具体实现以下:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Link } from 'react-router-dom';
import { Dropdown, Menu, Icon } from 'antd';
import { randomId } from 'utils';
import './style.css';
export class Header extends Component {
static propTypes = {
data: PropTypes.array,
activeTag: PropTypes.string,
};
static defaultProps = {
data: [{ tag: '前端', count: 5 }],
activeTag: '',
};
constructor(props) {
super(props);
this.navTotal = 6;
}
renderMore() {
if (this.props.data.length <= this.navTotal) {
return false;
}
const subNavItems = this.props.data.slice(this.navTotal).map(t =>
<Menu.Item key={`sub_nav_${randomId()}`}>
<Link
to={t.linkTo || `/tag/${t.tag}`}
className={`ant-dropdown-link ${this.props.activeTag === t.tag
? 'active'
: ''}`}
key={`nav_top_${randomId()}`}>
{t.tag}({t.count})
</Link>
</Menu.Item>
);
const SubNav = (
<Menu>
{subNavItems}
</Menu>
);
const DropDownBtn = (
<Dropdown overlay={SubNav} key={`nav_top_${randomId()}`}>
<div className="header-nav-item">
更多分类 <Icon type="down" />
</div>
</Dropdown>
);
return DropDownBtn;
}
renderTop5() {
const items = this.props.data.slice(0, this.navTotal - 1).map(t =>
<Link
className={`header-nav-item ${this.props.activeTag === t.tag
? 'active'
: ''}`}
to={t.linkTo || `/tag/${t.tag}`}
key={`nav_top_${randomId()}`}>
{!t.linkTo ? `${t.tag}(${t.count})` : t.tag}
</Link>
);
return (
<div className="header-nav">
{items}
{this.renderMore()}
</div>
);
}
render = () => this.renderTop5();
}
export default Header;
复制代码
你们能够根据实际须要实现本身的文章打包方式(这里就不奉上个人脚本了😝)。
对于我的博客来讲,到这里为止还有不少功能没有实现,这里偷个懒,奉上一些相关的连接吧:
我最近应该会实现一个React用途的markdown树组件,你们不妨期待下☺️
CRA针对github pages用途专门推荐了一个包:gh-pages,使用方法以下:
(1)修改项目的package.json
文件,添加homepage属性:
"homepage": "https://parksben.github.io",
复制代码
(2)项目安装gh-pages
依赖后修改,在package.json
中添加以下配置:
"scripts": {
+ "predeploy": "npm run build",
+ "deploy": "gh-pages -d build",
"start": "react-scripts start",
"build": "react-scripts build",
复制代码
(3)将本地代码上传到github博客仓库的某个分支(只要不是master分支就行),而后执行:
yarn deploy
复制代码
gh-pages会将CRA项目build到仓库的master分支,而后,你就能够访问你的站点了(有关 CRA 项目部署到 github pages 的详细描述能够看这里)。
单页面应用通常须要设置服务端路由,将应用的全部页面路径都重定向到index.html,而github pages并无这样的默认设置。
于是,当你使用React的客户端路由(React的createBrowserHistory方法建立前端路由)时,除根路径之外的页面,github都会返回本身的404页面。
为此,CRA项目提供了一种比较hack的方法来支持React的客户端路由(经过操做window.history来强行匹配url)。也算是一种奇技淫巧吧☺️。
(1)在CRA项目的public目录下添加一个404.html
,其内容以下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>矮大紧的平常 | parksben's blog</title>
<script type="text/javascript"> var segmentCount = 0; var l = window.location; l.replace( l.protocol + '//' + l.hostname + (l.port ? ':' + l.port : '') + l.pathname.split('/').slice(0, 1 + segmentCount).join('/') + '/?p=/' + l.pathname.slice(1).split('/').slice(segmentCount).join('/').replace(/&/g, '~and~') + (l.search ? '&q=' + l.search.slice(1).replace(/&/g, '~and~') : '') + l.hash ); </script>
</head>
<body>
</body>
</html>
复制代码
(2)在index.html
的head中添加以下代码:
<script type="text/javascript"> (function(l) { if (l.search) { var q = {}; l.search.slice(1).split('&').forEach(function(v) { var a = v.split('='); q[a[0]] = a.slice(1).join('=').replace(/~and~/g, '&'); }); if (q.p !== undefined) { window.history.replaceState(null, null, l.pathname.slice(0, -1) + (q.p || '') + (q.q ? ('?' + q.q) : '') + l.hash ); } } }(window.location)) </script>
复制代码
大功告成,你的github站点支持React的客户端路由了。
除此以外,也能够改成使用createHashHistory
方法来建立客户端路由,这样前端路由就与服务端路由没多大关系了,不过url里面一串hash毕竟不够优雅。
有兴趣了解奇技淫巧的童鞋,能够点这里。
与CRA项目的生产环境部署方式同样:
这是个人github博客(基于上述过程实现的静态站点),感兴趣的伙伴能够点击这里查看项目源码,以为有用也能够fork或star一下下。