介绍
Gatsby 是一个现代静态网站生成器,采用 React 和 GraphQL 技术栈,并且官方社区提供了很多插件、主题等扩展功能,帮助开发者快速构建响应式的静态网站。其中, gatsby-theme-notes
是一款适用于 Gatsby 的主题,专门用于生成以 Markdown 和 YAML 文件为基础的博客或笔记网站。
本文将详细介绍如何使用 gatsby-theme-notes
构建你的个人博客或笔记网站。
环境要求
确保你已经安装了以下环境:
- Node.js
- Gatsby CLI (安装方法:
npm install -g gatsby-cli
)
安装和配置
新建 Gatsby 网站
在命令行中输入以下代码,创建一个新的 Gatsby 网站:
gatsby new my-blog https://github.com/gatsbyjs/gatsby-starter-hello-world
安装 gatsby-theme-notes
将 gatsby-theme-notes
作为依赖项安装到你的 Gatsby 网站中:
npm i gatsby-theme-notes
配置 gatsby-theme-notes
在 gatsby-config.js
中配置 gatsby-theme-notes
主题:
-- -------------------- ---- ------- -------------- - - ------------- - ------ --- ------ ------- ----- ------ ------------ ------------ -- ---- ------ ------- - - ----- ---------- ---- ------------------------------------------ -- - ----- --------- ---- ------------------------------------------ -- -- -- -------- - - -------- --------------------- -------- - --------- -------- ------------ ------------- -- -- -- --展开代码
其中,basePath
是设置博客路径前缀,比如 /blog
,则博客列表页面路径为 http://localhost:8000/blog/
;contentPath
是设置博客内容的 Markdown 文件存放路径,比如 data/notes
。
创建博客(笔记)文件
在上文中的 contentPath
中,新建一个 index.md
文件,输入以下内容:
--- title: "My first note" date: "2021-03-01T01:57:19.284Z" description: "This is my first note using gatsby-theme-notes." --- This is my first note using gatsby-theme-notes.
index.md
中,使用 YAML 头信息来定义标题、日期、摘要等信息,Markdown 技术格式的正文写在 ---
之后。
运行和查看
在命令行中输入以下代码,启动 Gatsby 服务器:
gatsby develop
浏览器在地址栏中输入 http://localhost:8000/blog/
,即可查看你的博客列表和详情页面。
结语
通过本文的学习,我们学习了如何使用 gatsby-theme-notes
主题构建博客或笔记网站。在实际开发中,我们可以继续深入主题配置、博客文件编写、日志分页等方面,打造出自己的独特博客风格。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/80037