为深入了解hexo打造博客,跟着别家博客进行一次简单主题设计的记录。
针对hexo打造博客中,跟着别人的自建主题的一个记录,深入了解一下一个hexo主题的构成和编写过程,针对性会接触到模板引擎语法、CSS预处理,首先是hexo博客架构,在hexo init了一个博客项目以后,需要进入文件夹进行npm install下载第三方依赖,这里的npm就依托于nodejs而存在了,前端也很经常和它打招呼。此后的主体框架就算定了,进行配置的_config.yml配置文件,scaffolds模板文件夹,source资源文件夹是存放资源的地方,里面的_post文件夹会存在hexo new 文章创建的“文章”md文件,博客文章就是编写md文件,对markdown语法友好,而它们被解析后的文件就会被丢到public文件夹,这个文件夹就是挂载的主体,在github
page建站的时候,把这个文件夹丢上去就够了。
1 2 3 4 5 6 7 8 9 # 整体架构如下: . ├── _config.yml ├── package.json ├── scaffolds ├── source | ├── _drafts | └── _posts └── themes
除去上面的scaffolds模板文件夹,public发布建站文件夹,source资源文件夹以外,最重要的就是themes主题文件夹,在整体内容不变的情况下,themes提供对外的扩展接口,可以针对博客呈现形式有个自定义修改,这就是themes的来由。它的使用往往是在hexo官网主题页面 中挑选合适主题,在对应github主页clone下来,然后丢到themes中去,然后在根目录的_config.yml的[theme]数据项指定该项目文件夹,并且在同级目录下新建一个对应的config.yml文件,文件名当然需要用主题项目拓展一下,巴拉巴拉的。
下面的操作需要用到两个检验结果的命令:
1 2 3 4 # 清理当前配置并根据当前配置来进行博客html生成 hexo clean && hexo g # 执行服务器程序,把当前博客在本机跑起来 hexo s
预备两个概念,项目根目录是博客项目根目录,主题根目录是位于博客项目的themes目录下自定义的主题项目这个目录。
开始
下面的操作都是在themes文件夹做动作,准备做练习的simple项目,项目创建四个必要文件夹:languages、layout、scripts、source和一个配置文件_config.yml。然后先在layout做功夫,新建一个_partial目录,内建三个对应顶部导航栏、底部信息展示区域和html头部相关的三个文件:header.ejs、footer.ejs和head.ejs,至于重要的中间内容,则是后面出现的index.ejs,这样一来,项目结构如下:
1 2 3 4 5 6 7 8 9 10 11 12 simgple ├── languages ├── scripts ├── source ├── layout | ├── index.ejs | ├── layout.ejs | └── _partial | ├──head.ejs | ├──header.ejs | └──footer.ejs └── _config.yml
具体编写内容如下,重点关注head信息编写,使其自适应不同设备宽度,这点很重要:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <head > <meta http-equiv ="content-type" content ="text/html; charset=utf-8" > <meta content ="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name ="viewport" > <title > 标题</title > </head > <header > 头部导航栏</header > <footer > 底部信息框</footer >
然后在layout目录下编写layout.ejs来引入上面三个大头:
1 2 3 4 5 6 7 8 9 10 11 <!DOCTYPE html > <html > <%- partial('_partial/head') %> <body > <div class ="container" > <%- partial('_partial/header') %> <%- body %> <%- partial('_partial/footer') %> </div > </body > </html >
接触过flask中的jinja2模板,所以会对<%的标记眼熟,应当是模板渲染的标记,在jinja2中,这种标记就是{{或者{%了。上面的partial函数在上面的用法就是引入外部文件,把上面的head、header和footer都引入,如此一来,layout.ejs就已经给后面的ejs文件给定型了,算是最为基础的模板了。
在index.ejs直接编写body主体内容即可:<h1>hello, hexo</h1>,修改修改博客根目录的_config.yml文件,把新增主题给加上去:
然后hexo clean && hexo g配置一下,再hexo s跑起来看看,嗯,效果有了:
效果
丰富导航栏和底部信息
针对标题部分,调用全局设置中的title信息:<title><%= config.title %></title>,也就是站点根目录下的_config.yml的title信息。
编辑导航栏,根据导航栏-标题-导航栏信息(内含菜单)的样式在header.ejs中层层内叠起来:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <header class ="header" > <div class ="title" > <a href ="<% url_for() %>" class ="logo" > <%= config.title %></a > </div > <nav class ="navbar" > <ul class ="menu" > <li class ="menu-item" > <a href ="/" class ="menu-item-link" > Home</a > </li > <li class ="menu-item" > <a href ="/archive" class ="menu-item-link" > archive</a > </li > <li class ="menu-item" > <a href ="/tags" class ="menu-item-link" > tags</a > </li > <li class ="menu-item" > <a href ="/links" class ="menu-item-link" > links</a > </li > </ul > </nav > </header >
然后是底部footer,添加上作者信息:
1 2 3 4 <footer > <p > Jack</p > <p > Powered by <a href ="https://hexo.io/zh-cn/" > Hexo</a > </p > </footer >
跑起来的效果是这样的,因为没有设计样式,所以区域边界不是很明显:
simple主题效果
上面的header中设计导航栏中的菜单其实不够灵活,可以通过在header.ejs中设置一个for循环,循环获取主题设置中的menu项信息进行渲染:
1 2 3 4 5 menu: home: / archive: /archive tags: /tags links: /links
然后就在header.ejs放一个for循环:
1 2 3 4 5 6 7 8 9 10 11 ... <nav class ="navbar" > <ul class ="menu" > <% for (name in theme.name) { %> <li class ="menu-item" > <a href ="<%- url_for(theme.menu[name]) %>" class ="menu-item-link" > <%= name %></a > </li > <% } %> </ul > </nav > ...
因为是调用主题的设置,所以直接用theme这个变量,来调用它的子配置。
文章引入
上面的都是针对基础页面的设计,是时候引入多篇文章了。文章都在page变量的posts属性中获取:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <section class ="posts" > <% page.posts.each(function(post) { %> <article class ="post" > <div class ="post-title" > <a class ="post-title-link" href ="<%- url_for(post.path) %>" > <%= post.title %></a > </div > <div class ="post-content" > <%- post.content %> </div > <div class ="post-meta" > <span class ="post-time" > <%- date(post.date, "YYYY-MM-DD") %></span > </div > </article > <% }) %> </section >
编辑index.ejs如上,使用page变量的posts属性来获取信息,each就算一个迭代,逐个调用当中函数来处理posts的信息了,这里的处理是提取每一篇_posts文件夹的文章,post.title当然要的就是标题信息了,post.content获取的是文章的全部内容。因为文章很多都不是小篇幅,这一点很烦人,所以还是搞个摘要算了,把post.content换成post.excerpt,另外就是在文章中添加<!--more-->标记和导盲区共同标记出摘录了。跑起来检查一下效果看看。en......一直在读取<% page.posts.each(function (post) { %>的时候出错。看了一下其他能跑起来的样式,为啥会出错????
嗯。。。。。。试了一下,好像会报错,但是能跑?前后效果如下:
美化样式
在hexo主题中,样式都在source的css文件夹中,均为.styl文件,在这其中的样式文件的编写和css一致,不过它们在页面生成以后,hexo-renderer-stylus插件会编译styl文件为css文件。在css文件夹中新建一个style.styl作为主纲:
1 2 3 4 5 6 7 8 9 10 11 body { background-color : #F2F2F2 ; font-size : 1.25rem ; line-height : 1.5 ; } .container { max-width : 960px ; margin : 0 auto; } @import "_partial/header" @import "_partial/index"
css文件下新建_partial文件夹,编辑header.styl和index.styl两个文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 .header { margin-top : 2em display: flex align-items: baseline justify-content: space-between .blog-title .logo { color : #AAA ; font-size : 2em ; font-family : "Comic Sans MS" , cursive, LiSu, sans-serif; text-decoration : none; } .menu { margin : 0 ; padding : 0 ; .menu-item { display : inline-block; margin-right : 10px ; } .menu-item-link { color : #AAA ; text-decoration : none; &:hover { color : #368CCB ; } } } } .post { margin : 1em auto; padding : 30px 50px ; background-color : #fff ; border : 1px solid #ddd ; box-shadow : 0 0 2px #ddd ; } .posts { .post :first-child { margin-top :0 ; } .post-title { font-size : 1.5em ; .post-title-link { color : #368CCB ; text-decoration : none; } } .post-excerpt { a { color : #368CCB ; text-decoration : none; } } .post-meta { color : #BABABA ; } }
在head.ejs中引入,不过用了css函数:
1 2 3 ... <%- css('css/style') %> ...
原文是加上了styl后缀格式的,不过那样会出错,在调试的时候f12查看的结果出现引入style.styl.css文件,去掉后缀,就是生成的style.css,刚好对上。
效果
不过点击文章想查看具体内容的时候却没啥用,真烦人,后台查看,依然是hexo g生成public的时候的那个错误:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Unhandled rejection TypeError: D:\Desktop\gitee-blog\themes\simple\layout\index.ejs:2 1| <section class="posts"> > > 2| <% page.posts.each(function (post) { %> 3| <article class="post"> 4| <div class="post-title"> 5| <a class="post-title-link" href="<%- url_for(post.path) %>"><%= post.title %></a> Cannot read properties of undefined (reading 'each') at eval ("D:\\Desktop\\gitee-blog\\themes\\simple\\layout\\index.ejs":12:19) at index (D:\Desktop\gitee-blog\node_modules\ejs\lib\ejs.js:703:17) at _View._compiled (D:\Desktop\gitee-blog\node_modules\hexo\lib\theme\view.js:136:50) at _View.render (D:\Desktop\gitee-blog\node_modules\hexo\lib\theme\view.js:39:17) at D:\Desktop\gitee-blog\node_modules\hexo\lib\hexo\index.js:64:21 at tryCatcher (D:\Desktop\gitee-blog\node_modules\bluebird\js\release\util.js:16:23) at D:\Desktop\gitee-blog\node_modules\bluebird\js\release\method.js:15:34 at RouteStream._read (D:\Desktop\gitee-blog\node_modules\hexo\lib\hexo\router.js:47:5) at Readable.read (node:internal/streams/readable:496:12) at resume_ (node:internal/streams/readable:999:12) at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
样式方面的了解个人是不太够,主要还是想抓住那个脉络,润色这方面实在太繁杂了。随着文章数量增加,显示页面就会臃肿,所以需要分页:
在layout中_partial新建paginator.ejs,新增分页功能:
1 2 3 4 5 6 7 8 <% if (page.total > 1) { %> <nav class ="page-env" > <%- paginator({ prev_text: "<<; Prev", next_text: "Next >>;" }) %> </nav > <% } %>
然后在index.ejs中引入:
1 2 3 ... </section > <%- partial('_partial/paginator') %>
使用paginator辅助函数,帮助插入分页链接。
文章详情页实现
上面实现了导航页,但显示的文章点击以后,还是没有响应,如上面错误显示那样,添加post.ejs来处理一下文章详情页面:
1 2 3 4 5 6 7 8 9 10 11 <article class ="post" > <div class ="post-title" > <h2 class ="title" > <%= page.title %></h2 > </div > <div class ="post-meta" > <span class ="post-time" > <%- date(page.date, "YYYY-MM-DD") %></span > </div > <div class ="post-content" > <%- page.content %> </div > </article >
不过还是在这一步,前面的报错又炸出来了,初始化了几次同样的项目来查看格式,具体调用是没问题的,那就只能是在page.posts.each获取文章信息的时候读取出错了,啊,真烦。调试来回,格式上是没有问题的,只能是在迭代的时候,读取要素的时候出错了,麻烦的是我对这里的调试不够熟悉。两种主题,一种默认主题、一种自定义主题,在默认主题中进行debugger查看需要的post信息,但确实没错,时间不够了,先强行停一下吧。后面续更。
---------------------------------------------留待后续