基础部分:三分钟快速搭建个性化博客。
第一部分:hexo-Next8博客搭建、美化(darkmode、waline等)
第二部分:hexoNext美化(二)
第三部分:hexo美化(三)进阶:异步加载脚本,加快网站访问
全文整理:csdn
在上一篇中我们谈到了利用hexo-renderer-markdown-it-plus插件渲染md支持的目录,并利用hexo支持的md注释显示阅读更多按钮,这样,我们在各个网站迁移自己写的md文档时也不会遇到格式匹配问题了。
但是这样就会产生一个问题,如果我们在首页同时用显示摘要(或部分文字)和插入阅读更多按钮,而md文档自动帮我们生成的TOC目录只会产生一个#锚点,并不会跳转到博文相对应的链接,然后读者就和一动不动的网页干瞪眼啊😅😂官方的helper我个人感觉弄起来非常麻烦,而且我们的md文档转到另一个平台又要重新忧虑插入目录的问题了~
因此博主的思路是,在静态文件生成后修改首页(包括index.html和page文件夹下的所有页面),利用lxml库解析home页的对应markdownIt-TOC类下的所有a标签的herf链接,同时找到markdownIt-TOC父节点的兄弟节点(就是那个post的header a的herf),连接到一起,然后重新匹配,替换掉该部分(因为直接写入解析替换后的html会乱码,不敢动),hexo d生成后,拉取静态文件到本地,再重新push上去,这里还可以再对逻辑修改下减少下载上传量。
4.11 修改:
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
| from lxml import etree import re import os import sys
def update_links(html_file): with open(html_file, 'r', encoding='utf-8') as f: html_content = f.read() parser = etree.HTMLParser(encoding="utf-8") htmlelement = etree.parse(html_file, parser=parser)
index_elements = htmlelement.findall('.//div[@class="post-block"]')
for index_elem in index_elements: toc_links = index_elem.findall(".//ul[@class='markdownIt-TOC']//a") if toc_links is not None: header_link = index_elem.find(".//header//a") if header_link is not None: for toc_link in toc_links: old_href = toc_link.get('href') text = toc_link.text new_href = header_link.get('href') + old_href html_content = re.sub(fr'<a href="{old_href}">', f'<a href="{new_href}">',html_content)
with open(html_file, 'w', encoding='utf-8') as f: f.write(html_content) if __name__ == '__main__': dir_path = sys.argv[1] if len(sys.argv)>1 else os.path.dirname(os.path.abspath(__file__)) for root, dirs, files in os.walk(dir_path): for subdir_count in dirs: file_path = os.path.join("page", subdir_count, "index.html") if os.path.exists(file_path): update_links(file_path) update_links(os.path.join(dir_path,"index.html"))
|
upaload.bat
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
| CALL hexo cl
CALL hexo d
timeout /t 10
git clone ?yourrepo
CALL conda activate yourenvs
python updateIndexLink.py yourrepo
cd yourrepo
git add .
git commit -m "uploadTOC"
git push
rd /s /q unnamedtat-blogs
|
这样直接在命令行运行这个bat就可以了,不过还是比较麻烦的,而且在本地启动服务器也看不到子链修改的效果了,希望我有动力push自己学着写个插件吧~不过还是很麻烦啊😂
对了,今天遇到了hexo的公式渲染问题,之前我一直用的mathjax,hexo-renderer-markdown-it-plus插件貌似会自动启用katex,去看了一下其貌似需要引入一个css文件。
而在hexo next官网发现官方已经是帮我们集成好了,因此直接设置使用katex渲染就可以了,貌似会少一些函数,不过又不是写论文。😄Math Equations
在md中使用emoji:
emoji表情列表