首页文章显示标签 在 index.pug 中 .post-meta 位置添加如下代码
1 2 3 4 5 6 .post-meta = post.date.format(config.date_format) if post.tags.length > 0 each tag in post.tags.toArray() i.fa.fa-tag(style='padding-right: 0.2em;padding-left: 0.5em;') a(href=url_for(tag.path))= tag.name
我的博客 不采用 Catagories 的结构,因为我认为博客文章应当是时间强相关的流水模式。结构化的知识可以单独开一个博文,指向其他博文。
我的博客更看重 tags, 将文章按照自己定义的 tag 零散的组织起来。
hexo new page "tags"
会生成 tags
页面
在 maupassant
的 _config.yml
的 menu:
中添加
1 2 3 - page: tags directory: tags/ icon: fa-tag
可以在 themes\maupassant\layout\tagcloud.pug 中修改标签云的样式。我将按列表列出的标签删除掉了。
在 style.css 中添加如下代码,当标题文本过长需要换行时,第二行会自动与日期的右边对齐。
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 .post-archive ul { padding-left : 0.8em } .post-archive .listing li { display : flex; gap : 0.1em ; margin-bottom : 0.2em ; list-style-type : disc; padding-left : 1em ; position : relative; } .post-archive .listing li ::before { content : "•" ; position : absolute; left : 0 ; color : #666 ; } .post-archive .listing li .date { flex : 0 0 100px ; text-align : left; } .post-archive .listing li a { flex : 1 ; text-align : left; padding-left : 0 ; word-break : break-word; overflow-wrap : break-word; }
修改主页标签的显示方式 原本的主题中,Tags 可能会换行,导致阅读不便,我可以模仿 编程随想 的博客 Tags 样式,写为列表形式。
我修改了 themes\maupassant\layout_widget\tag.pug 中 tagcloud 的形式,利用 hexo 提供的 site.tags
变量重新写了标签云的显示形式
在 themes\maupassant\layout\tagcloud.pug 中, 添加了最近的二十篇文章标题显示,以及剩余的文章数量。
为了 tags 页面不分页,从而方便按标签、标题来搜索文章,我发现该主题的标签分页逻辑是由 hexo 自身控制的。修改 _config.xml
文件的如下内容,即可不分页
1 2 3 4 5 # Pagination # 此处修改 tag 页面是否进行分页 ## Set per_page to 0 to disable pagination per_page: 0 pagination_dir: page
页面显示的配置 在2k显示屏上放大页面 在 style.scss
中添加如下代码
1 2 3 4 5 6 7 8 @media screen and (min-width : 2048px ) { body { transform : scale (1.25 ); transform-origin : top; overflow-x : hidden; } }
链接样式 原主题的链接很不显眼,故模仿端传媒 的链接,将样式修改如下
1 2 3 4 .post .post-content a { color : #2b2b2b ; background : linear-gradient (to top, rgba (40 , 179 , 195 , 0.48 ) 20% , transparent 30% ) 0 -1px ; }
字体配置 将字体堆栈改为
1 2 3 4 .pure-g [class *="pure-u" ] { font-family : "PingFangSC-Regular" , Helvetica, "Helvetica Neue" , "Segoe UI" , "Hiragino Sans GB" , "Source Han Sans CN" , "Microsoft YaHei" , "STHeiti" , "WenQuanYi Micro Hei" !important ; }
删去了 sans 字体的要求。
将字体颜色改为更深的黑色.
dark 模式 将 dark 模式的调整按钮固定到了菜单栏,并且修改 script 解决 DOM 未加载完全导致的找不到类的错误。
此时网站会根据时间和系统偏好自动设置暗\亮模式,并且会存储用户的上次选择一段时间。
图片配置 如何添加图片标签 修改 maupassant 主题,思路是模仿 fancybox 的调用方式。
在主题文件 after_footer.pug
中添加以下代码
1 2 if theme.customJS == true script(type='text/javascript', src=url_for(theme.js) + '/customJS.js')
这一步的目的是,当theme的配置文件中添加 customJS 的选项为 true 是,在页面加载完成后调用 js 脚本
在 source/js
中添加文件 customJS.js, 内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 $(document ).ready (function ( ) { function addFigcaption ( ) { $('img' ).each (function ( ) { var alt = $(this ).attr ('alt' ); if (alt) { $(this ).wrap ('<p></p>' ); $(this ).after ('<figcaption>' + alt + '</figcaption>' ); } }); } addFigcaption (); $(window ).on ('beforeunload' , function ( ) { addFigcaption (); }); });
这一步是为了将图片包含在 p 标签中,并且 alt 属性非空时将图片的alt作为标签显示出来。
插入图片时利用如下格式,其中 alt 即为alt文本
1 {% asset_img post.jpg "花馍 'alt'" %}
页面刷新后继续执行,是因为加密插件会重新加载DOM,并且加载完后不会调用 ready,所以注册一个事件,刷新后继续执行该命令。
vscode 实用配置 插入图片 复制或者拖入图片到编辑器,会产生如下的代码
1 
可以通过一个脚本将其转化为 hexo 的图片标签语法如下
1 {% asset_img 女孩的雕像.png "女孩的雕像 '女孩的雕像'" %}
脚本文件为
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import reimport urllib.parseimport sysimport osdef replace_encoded_string (match ): encoded_string = match .group(1 ) decoded_string = urllib.parse.unquote(encoded_string) base_name = os.path.splitext(decoded_string)[0 ] return '{% asset_img ' + decoded_string + ' "' + base_name + ' \'' + base_name + '\'" %}' with open (sys.argv[1 ], 'r+' ) as f: content = f.read() content = re.sub(r'!\[Alt text\]\([^)]*\/([^)]*)\)' , replace_encoded_string, content) f.seek(0 ) f.write(content) f.truncate()
配置自定义任务,tasks.json 的内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 { "version" : "2.0.0" , "tasks" : [ { "label" : "Run replace command" , "type" : "shell" , "command" : "python3 replace.py ${file}" , "problemMatcher" : [ ] , "presentation" : { "reveal" : "silent" , "panel" : "dedicated" } } ] }
在任务面板中输入 run task, 选择 Run replace command 即可实现迅速的替换。
列表配置 默认的tags中的日期是用可变宽度,导致日期后的文字根据日期数字宽度的不同缩进不同,比较难看。
解决方式:修改style.scss中 .post-archive
的 .date
属性为
1 2 3 4 5 6 7 8 .date { display : inline-block; width : 100px ; text-align : left; font-family : monospace; color : #202020 ; }
即可为固定宽度。