Hexo实战(二)Hexo基本设置和使用
Hexo 站点配置文件
官方关于配置的文档:https://hexo.io/zh-cn/docs/configuration
网站的配置信息在文件 _config.yml 中,完整的初始配置信息(除了修改 Deployment)如下: # Hexo Configuration ## Docs: https://hexo.io/docs/configuration.html ## Source: https://github.com/hexojs/hexo/ # Site title: Hexo subtitle: "" description: "" keywords: author: John Doe language: en timezone: "" # URL ## If your site is put in a subdirectory, set url as "http://yoursite.com/child" and root as "/child/" url: http://yoursite.com root: / permalink: :year/:month/:day/:title/ permalink_defaults: pretty_urls: trailing_index: true # Set to false to remove trailing "index.html" from permalinks trailing_html: true # Set to false to remove trailing ".html" from permalinks # Directory source_dir: source public_dir: public tag_dir: tags archive_dir: archives category_dir: categories code_dir: downloads/code i18n_dir: :lang skip_render: # Writing new_post_name: :title.md # File name of new posts default_layout: post titlecase: false # Transform title into titlecase external_link: enable: true # Open external links in new tab field: site # Apply to the whole site exclude: "" filename_case: 0 render_drafts: false post_asset_folder: false relative_link: false future: true highlight: enable: true line_number: true auto_detect: false tab_replace: "" wrap: true hljs: false # Home page setting # path: Root path for your blogs index page. (default = "") # per_page: Posts displayed per page. (0 = disable pagination) # order_by: Posts order. (Order by date descending by default) index_generator: path: "" per_page: 10 order_by: -date # Category & Tag default_category: uncategorized category_map: tag_map: # Metadata elements ## https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta meta_generator: true # Date / Time format ## Hexo uses Moment.js to parse and display date ## You can customize the date format as defined in ## http://momentjs.com/docs/#/displaying/format/ date_format: YYYY-MM-DD time_format: HH:mm:ss ## Use post"s date for updated date unless set in front-matter use_date_for_updated: false # Pagination ## Set per_page to 0 to disable pagination per_page: 10 pagination_dir: page # Include / Exclude file(s) ## include:/exclude: options only apply to the "source/" folder include: exclude: ignore: # Extensions ## Plugins: https://hexo.io/plugins/ ## Themes: https://hexo.io/themes/ theme: landscape # Deployment ## Docs: https://hexo.io/docs/deployment.html deploy: type: git repo: https://github.com/kungge/kungge.github.io.git branch: master
主要分成如下: Site:网站 URL:网址 Directory:目录 Writing:文章写作 Category & Tag:分类和标签 Date / Time format:日期 / 时间格式 Pagination:分页 Include / Exclude file(s):包括或不包括目录和文件 Extensions:扩展 网站信息配置# Site title: 坤哥博客小站 subtitle: 热爱生活,热爱技术,听听故事 description: 生活是一门艺术,更是一门技术 keywords: 坤哥博客小站,坤哥博客网,坤哥博客技术网 author: wankun language: zh-CN timezone: Asia/Shanghai
渲染效果:
写作
上一篇文章讲过通过命令 hexo new [layout] 可以新建一篇文章,layout 表示布局,Hexo 有三种默认布局:post、page 和 draft,不指定布局则会使用默认布局,默认布局配置使用 default_layout: post 设置,不同的布局会存储在不同的目录中,前面使用命令 hexo new "Hello by hexo newcommand" 新增文件【Hello-by-hexo-new-command.md】存储在目录【source/_posts】中,如果是 draft 则是【source/_drafts】,page 则是【source】。
【Hello-by-hexo-new-command.md】文件内容为空,但是有"头"信息: --- title: Hello by hexo new command date: 2020-03-29 01:09:05 tags: ---
渲染效果:
这里的"头"信息叫 Front-matter,是文件最上方以 --- 分隔的区域,用于指定个别文件的变量,以下是预先定义的参数
参数
描述
默认值
layout
布局
不指定则使用默认_config.yml中配置的
title
标题
文章的文件名
date
建立日期
文件建立日期
updated
更新日期
文件更新日期
comments
开启文章的评论功能
true
tags
标签(不适用于分页)
categories
分类(不适用于分页)
permalink
覆盖文章网址
keywords
仅用于 meta 标签和 Open Graph 的关键词(不推荐使用)
修改该文件内容如下: --- title: Hello by hexo new command date: 2020-03-29 01:09:05 updated: 2020-03-31 23:10:05 tags: - 标签1 - 标签2 - 标签3 categories: - 类别1 - [类别2, 类别2-1] - [类别2, 类别2-2] - [类别3] --- 这里是文章内容…
注:在hexo中类别有父子之分,[类别2, 类别2-1] 表示 类别2-1 是 类别2 的子类别。
标签和类别也可以使用 [],如: tags: [hexo,Hexo 基本设置] categories: 开源博客
生成静态文件时,发现不光生成了文件本身,还生成了类别和标签页面:
渲染结果:
进入类别页面:
文件名称使用 new_post_name: :title.md # File name of new posts 来配置,默认使用 :title.md 表示使用文章标题名来命名,可使用加入时间信息来命名方便文章管理:
变量
描述
:title
标题(小写,空格将会被替换为短杠)
:year
建立的年份,比如, 2015
:month
建立的月份(有前导零),比如, 04
:i_month
建立的月份(无前导零),比如, 4
:day
建立的日期(有前导零),比如, 07
:i_day
建立的日期(无前导零),比如, 7 文章地址
站点配置文件,默认: # URL ## If your site is put in a subdirectory, set url as "http://yoursite.com/child" and root as "/child/" url: http://yoursite.com root: / permalink: :year/:month/:day/:title/ permalink_defaults: pretty_urls: trailing_index: true # Set to false to remove trailing "index.html" from permalinks trailing_html: true # Set to false to remove trailing ".html" from permalinks
渲染效果:
这样的地址对seo不太友好。
修改如下: # URL ## If your site is put in a subdirectory, set url as "http://yoursite.com/child" and root as "/child/" url: http://yoursite.com root: / permalink: p/:id/ permalink_defaults: pretty_urls: trailing_index: true # Set to false to remove trailing "index.html" from permalinks trailing_html: true # Set to false to remove trailing ".html" from permalinks
每篇文章 ---xx--- 中指定一个 Id 唯一值,如 id: nas-network,渲染效果:
常见问题网站中文乱码
为什么会出现乱码?
先查看网站用的主题,在 # Extensions 一栏,使用命令 npm install hexo-generator-searchdb --save 安装: # Extensions ## Plugins: https://hexo.io/plugins/ ## Themes: https://hexo.io/themes/ theme: landscape
用的主题是 landscape,然后再主题的目录下找到语言配置文件:
那么在 # Site 一栏中语言配置 language: 也要在使用的主题语言配置中。
如上中文应使用 zh-CN,设置 language: zh-CN。
如果还不行,那可能是配置文件【_config.yml】本身的编码格式,另存为 UTF-8 格式:
.yml 可读性差
在华为云的 windows 服务器上用记事本打开成这样:
可读性太差。
而在本机的 windows 打开却是正常的:
在华为云的 windows 服务器上记事本也设置了自动换行,这是怎么回事?
不做深究,强烈建议使用第三方编辑器编写即可,如 vs code、sublime等。 网站全空白
查看页面元素,发现都在:
说明是样式的问题,查看日志:
查看使用主题的 font-asesome.min.css 文件,发现确实不存在,刚才误删了:
恢复就好了。