简简单的说,静态网站生成器会获取你的内容,并将其应用于模板,而后生成基于 HTML 的静态网站。很是适合我的博客。css
好处:html
那么,都有哪些流行的静态网站生成器呢?前端
这些项目在 GitHub 上的知名度很是高。node
其官方网站号称 Hugo 是世界上最快的静态网站引擎。git
Hugo 是用 Go 语言编写的,它还有很是丰富的主题系统。程序员
Mac:github
brew install hugo
Linux:面试
sudo apt-get install hugo 或者 sudo pacman -Syu hugo
而后执行下面的命令检查是否安装成功:json
hugo version
建立一个新项目:segmentfault
hugo new site my-project
下载一个主题。能够在 https://themes.gohugo.io/ 找到更多你喜欢的主题。
cd my-project git init git submodule add https://github.com/budparr/gohugo-theme-ananke.git themes/ananke
将主题添加到配置文件。
echo 'theme = "ananke"' >> config.toml
添加一篇文章。
hugo new posts/my-first-post.md
它看上去应该像这样:
--- title: "My First Post" date: 2021-03-10T18:37:11+08:00 draft: true --- Hello World!
能够在这里给文章添加添加更多属性配置(标签,描述,类别,做者)。
能够在 https://gohugo.io/content-man... 了解更多的配置项。
看看效果:
hugo server -D
在浏览器中打开 http://localhost:1313
就能看到你的网站了。
. ├── archetypes ├── assets (not created by default) ├── config.toml ├── content ├── data ├── layouts ├── static └── themes
config.toml
、config.yaml
或 config.json
(能够在网站根目录中找到)做为默认网站配置文件。除了单独的配置文件以外,你还能够使用 config directory 来分隔不一样的环境。devops
和 nodejs
部分,那么你须要有 content/devops/first-post.md
和 content/nodejs/second-post.md
目录。.html
文件的形式存储模板。有关更多信息,请参见 Styling
部分。咱们在以前应用了一个主题。如今,若是咱们检查 themes
文件夹,能够看到样式文件。
可是要小心!
千万不要直接编辑这些文件!。
应该将主题目录结构复制到 layouts
文件夹。
假设我要将自定义 CSS 应用于主题。
主题有一个 themes/theme-name/layouts/partials
文件夹,能够在其中找到一些HTML模板(header.html
、footer.html
)。如今咱们将编辑 header.html
模板,将内容从这个文件复制到 layouts/partials/header.html
中,并注意在主题 layouts
根目录中建立与主题相同的目录结构。
layouts/partials/header.htmlss themes/theme-name/layouts/partials/header.html
建立一个自定义CSS文件: static/css/custom-style.css
,而后把自定义 CSS 文件添加到 config.toml
中:
[params] custom_css = ["css/custom-style.css"]
打开 layouts/partials/header.html
:
将这段代码添加到 <head>
标签内:
{{ range .Site.Params.custom_css -}} <link rel="stylesheet" href="{{ . | absURL }}"> {{- end }}
如今,就能够覆盖主题中所应用的 CSS 类。
在项目的根目录下执行 hugo
命令:
>>> hugo | EN -------------------+----- Pages | 14 Paginator pages | 0 Non-page files | 0 Static files | 1 Processed images | 0 Aliases | 6 Sitemaps | 1 Cleaned | 0 Total in 74 ms
执行成功后,会生成一个public 目录,这个目录中的内容就是咱们静态网站的全部内容。
而后就能够托管到 GitHub 或 OSS 中了。
Hugo 还提供了更多的内容,能够到官方文档查看:https://gohugo.io/documentation/。