修改hexo+next, 使得url路径带.html后缀 部署到阿里云oss

355 阅读2分钟

阿里云oss部署hexo不支持自动跳转到index.html页面,所以如果需要部署到阿里云oss上,那么需要做以下修改。

创建“标签”页面 生成 tags.html

  • 新建“标签”页面

    hexo new page tags
    
  • 给标签页面添加类型 我们在source文件夹中的tags文件夹下找到index.md文件,在它的头部加上type属性。

    ---
    title: tags
    date: 2018-08-06 22:48:29
    type: "tags" #新添加的内容
    ---
    

    将index.md文件重命名为 tags.md并挪到 tags的父文件夹下,删除tags文件夹

创建“分类”页面 生成categories.html

这里和上一步骤基本一致

  • 新建分类页面

    hexo new page categories
    
  • 给分类页面添加类型 我们在source文件夹中的categories文件夹下找到index.md文件,在它的头部加上type属性。

    ---
    title: 文章分类
    date: 2017-05-27 13:47:40
    type: "categories"   #这部分是新添加的
    ---
    

    将index.md文件重命名为 categories.md并挪到 categories的父文件夹下,删除categories文件夹

创建about.html

参考以上两个步骤

文章页面

在_config.yml中,修改

permalink: :year/:month/:day/:title/

permalink: post/:urlname.html

其中 urlname为在文章中指定当前文件的永久链接,如

---
title: 修改hexo+next, 使得url路径带.html后缀
date: 2018-04-21 23:55:07
categories: [hexo设置]
tags: [hexo]
urlname: hexo-next-url-html-suffix
---

修改文章归档首页的访问地址

修改 主题文件夹下的 _config.yml 配置文件

menu:
  home: / || home
  tags: /tags.html || tags
  categories: /categories.html || th
  archives: /archives/index.html || archive
  about: /about.html || user

这里修改之后会出现 点击归档页菜单栏的时候 菜单没有处于选中状态

修改 主题文件夹下 scripts/helpers/engine.js

var current = this.url_for(canonical).replace('index.html', '', 'g');

var current = this.url_for(canonical);

问题解决

修改hexo的分页

%d/index.html 变为 %d.html

hexo生成的分页 静态文件路径 如 archives/page/2/index.html 修改成 archives/page/2.html

修改 node_modules/hexo-pagination/lib/pagination.js

  //var format = options.format || 'page/%d/';
  var format = 'page/%d.html';

修改分页插件生成的分页链接

修改分页插件生成的分页链接 修改 node_modules\hexo\lib\plugins\helper\paginator.js

  function link(i) {
    return self.url_for(i === 1 ? base + '/index.html' : base + format.replace('%d/', i) + '.html');
  }

修改分类页的链接 形如 categories/Spring-Cloud-%E5%85%A5%E9%97%A8%E6%95%99%E7%A8%8B/index.html

修改 node_modules\hexo\lib\plugins\helper\list_categories.js

const suffix = options.suffix || 'index.html';

修改标签页的链接 形如 tags/Spring-Cloud/index.html

修改 node_modules\hexo\lib\plugins\helper\tagcloud.js

    result.push(
      `<a href="${self.url_for(tag.path)}index.html" style="${style}">${transform ? transform(tag.name) : tag.name}</a>`
    );

修改文章内容页里的 分类和标签 链接

修改 主题文件夹下 layout/_macro/post.swig

<a href="{{ url_for(cat.path) }}index.html" itemprop="url" rel="index">
  <a href="{{ url_for(tag.path) }}index.html" rel="tag"># {{ tag.name }}</a>

到这里,所有的链接都是以 html后缀 打开