hexo点滴

如果删除hexo-generator-index插件

如果没有hexo-generator-index插件(hexo自带),那么你就必须自己在source中创建一个index,这样编译后hexo才会生成index文件,读取localhost:4000时,才能读取到index,不然就没有localhost:4000这个路径了


hexo文章类型略说

  • hexo的所有内容管理其实说白了就两种类型 post和page,这两种类型的区别,逻辑上的就是post是可以列表的,page是单页,如about这样的,具体表现就是生成的post都是在_post文件夹中,page都是在外层根目录下自建文件夹的

  • 可以将config中archive_dir:news,permalink: news/:year/:month/:day/:title/ 这样就会把所有post编译到archive中,节省一个文件夹

  • 无论是index tag category archive,都不过是文章集合的再编辑形式而已,他们的形式基本为:

--- index
--- page(这里面是index的分页页面文件夹)

--- tag
------ tagName
--------- page(这里面是tag的分页页面文件夹)
------------ 02(从第二页开始)
--------------- index.html(分页页面)
--------- index.html(分页页面)
------ index.html(汇总页面)


image.png

--- categories
------ categorieName
--------- page(这里面是tag的分页页面文件夹)
------------ 02(从第二页开始)
--------------- index.html(分页页面)
--------- index.html(tag汇总页面)


image.png

--- archive
------ 2019(这里面是年份页面文件夹)
--------- 01
------ index.html(汇总页面)

生成结构基本就是这个样子的


生成器中name的用途

hexo.extend.generator.register(name, function(locals){
});
这个里面的 name 是做什么的呢?
其实这是注册了后面运行函数的代号
按照package.json里面加载插件的顺序
会依次运行代号里面的函数
如果重名
那么在最后面的插件就会把前面的同名函数取代掉
就是覆盖啦~
如:
hexo-generator-tag/index.js

hexo.extend.generator.register('tag', require('./lib/generator'));

注册了名为tag的生成器
我们自己定义一个插件
hexo-generator-tag-myself/index.js

hexo.extend.generator.register('tag', require('./lib/generator-tag'));

名字一样
那么就看在package.json中 哪一个后加载
哪个在后面 就用哪个生成器

通过上面的逻辑 再结合hexo源代码

hexo/lib/plugins/generator/index.js

'use strict';

module.exports = ctx => {
  const { generator } = ctx.extend;

  generator.register('asset', require('./asset'));
  generator.register('page', require('./page'));
  generator.register('post', require('./post'));
};

其实 asset page post 全部都是利用generator生成的,所以
我们可以通过重写asset page post的编译内容 更改其路径和内容
如:

hexo.extend.generator.register('post', function(locals){
    return locals.posts.map(function(post){
      return {
        path: "book/"+post.path,
        data: post,
        layout: 'post'
      };
    });
  });

这样就重写了post生成器


生成页面的path的规律

hexo.extend.generator.register('generator-name', function(locals){
    return {
      1) path: 'demo',  //根目录生成demo无格式文件
      2) path: 'demo.html', //根目录生成demo.html
      3) path: 'aaa/demo.html', //生成aaa文件夹下demo.html
      4) path: 'aaa/bbb/demo.html', //生成aaa/bbb文件夹下demo.html
      5) path: 'aaa/bbb/demo', //生成aaa/bbb文件夹下demo无格式文件
      6) path: 'aaa/bbb/demo/', //生成aaa/bbb/demo文件夹下index.html文件
      data: locals.posts,
      layout: ['duan']
    }
  });

注意:第六个规则最重要,只需要在后面加“/”,那么就会自动生成index.html

如果用hexo-pagination,第一个参数path是可以不带"/"的,插件里面自己写了会自动加上
也就是说分页只需要写路径就会自动生成index.html


插件的写法

node_modules 文件夹中建立文件夹,文件夹名称开头必须为 hexo-

.
├── index.js
└── package.json

package.json 中至少要包含 name, version, main 属性,例如:

package.json
{
  "name": "hexo-my-plugin",
  "version": "0.0.1",
  "main": "index"
}

注意:最重要的一步,需要将此插件添加都package.json的dependencies中即可

  "dependencies": {
    "hexo": "^3.7.0",
    "hexo-generator-archive": "^0.1.5",
    "hexo-generator-category": "^0.1.3",
    "hexo-generator-tag": "^0.2.0",
    "hexo-generator-basic-set": "^0.1.1",
    "hexo-renderer-ejs": "^0.3.1",
    "hexo-renderer-marked": "^0.3.2",
    "hexo-renderer-stylus": "^0.3.3",
    "hexo-server": "^0.3.1",
    "hexo-my-plugin": "0.0.1"  // 这是我们自己的插件
  }

var localizedPath = ['docs', 'api'];
~localizedPath.indexOf(title)
查找数组中的 "Apple" 元素:

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var a = fruits.indexOf("Apple");
a 结果输出:

2
以上输出结果意味着 "Apple" 元素位于数组中的第 3 个位置。

取反的用法是,是因为-1的取反操作等于0,而其他数的取反操作不等于0。所以用indexOf操作符可以用操作来判断某个字符串里是否有某个字符。

var str = '123456'
if(~str.indexOf(0)){
  console.log('因为取反之后不为零,说明indexOf的结果不等于-1,所以表示str字符串里包含了对应字符')
} else {
  console.log('说明取反之后为零,说明indexOf的结果等于-1,所以表示str字符串里不包含了对应字符')
}

凡是在_post文件夹下的文件,都是用的post模板
凡是不在_post文件夹下的,全部都用page,除非指定了布局文件


多语言文件是和url的多语言一一对应的 page.lang也会随之变化

/index.html => en
/archives/index.html => en
/zh-tw/index.html => zh-tw

如果想缺省状态下是en
需要先声明

language: en

如果一个国际化页面的url为localhost:4000/zh/ ,那么只有监测到在语言文件夹下有zh.yml文件时,page.lang才会变为“zh”


<%- partial('_partial/header', null, {cache: !config.relative_link}) %>

这是个大坑
缓存如果是true,那么helper函数就只运行一次 这样的话 我们生成的页面就用的第一次生成的页面 很糟糕

你可能感兴趣的:(hexo点滴)