惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Jina AI
Jina AI
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI
Help Net Security
Help Net Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
GbyAI
GbyAI
博客园_首页
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
LINUX DO - 最新话题
PCI Perspectives
PCI Perspectives
博客园 - 三生石上(FineUI控件)
V2EX - 技术
V2EX - 技术
Spread Privacy
Spread Privacy
T
Tor Project blog
量子位
阮一峰的网络日志
阮一峰的网络日志
S
SegmentFault 最新的问题
小众软件
小众软件
博客园 - 叶小钗
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Blog — PlanetScale
Blog — PlanetScale
H
Help Net Security
Y
Y Combinator Blog
N
News | PayPal Newsroom
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Tenable Blog
Scott Helme
Scott Helme
G
GRAHAM CLULEY
大猫的无限游戏
大猫的无限游戏
aimingoo的专栏
aimingoo的专栏
IT之家
IT之家
Schneier on Security
Schneier on Security
F
Fortinet All Blogs
Martin Fowler
Martin Fowler
T
Threat Research - Cisco Blogs
博客园 - 司徒正美
Application and Cybersecurity Blog
Application and Cybersecurity Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Attack and Defense Labs
Attack and Defense Labs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The Last Watchdog
The Last Watchdog
L
LangChain Blog
C
Check Point Blog
Google Online Security Blog
Google Online Security Blog
V
Visual Studio Blog
Latest news
Latest news

Hunsh's Blog

生成个人的 Github PR List 如何选择合适的加密手段 Nginx SNI Proxy 如何优雅导入 k8s.io/kubernetes Make Go mod and Git to use specify .netrc 多架构镜像的体积优化 使用 buildkit 进行多架构构建并提取产物 如何修改镜像 layer(以 sourcemap-less grafana 为例) acme.sh 自动化 Google CA 申请证书 微信小程序嵌入任意公众号文章 k8s && bazel 项目从 go1.20 升级 go1.21 go serve http and https on same port 实现 hexo 文章和资源在同一目录下 golang mitm 遇到的问题 流式数据专用的 mine-type Golang 正向代理对于 Host 的处理 (RFC 7230) 分享 MacOS 的一些系统fix脚本(dns、sleep) prometheus rate/increase/delta/sum等函数不符合预期或出现小数的原理 [油猴脚本] USTB Everywhere 为校外访问添加访问任意网站的能力
typecho 迁移到 hexo
2023-08-06 · via Hunsh's Blog

前言

typecho 本身的格式就是 markdown,所以迁移难度不高,就是适配一下格式就可以了。

主要参考了 https://cloud.tencent.com/developer/article/1158747

迁移

链接里的代码在 python3.10 已经不可使用了,所以进行了一小点改造,同时适配了我自己的文章链接格式。

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# -*- coding: utf-8 -*-

import os
import MySQLdb
import arrow
from box import Box

def create_data(db):
cursor = db.cursor(MySQLdb.cursors.DictCursor)
cursor.execute("select type, slug, name from typecho_metas")
# 创建分类和标签
categories = cursor.fetchall()
for cate in tags:
cate = Box(cate)
path = 'data/%s' % cate.slug
if not os.path.exists(path):
os.makedirs(path)
f = open('%s/index.md' % path, 'w', encoding="utf-8")
f.write("title: %s\n" % cate.slug)
f.write("date: %s\n" % arrow.now().format('YYYY-MM-DD HH:mm:ss'))
# 区分分类和标签
if cate.type == 'category':
f.write('type: "categories"\n')
elif cate.type == 'tags':
f.write('type: "tags"\n')
# 禁止评论
f.write("comments: true\n")
f.write("---\n")
f.close()

# 创建文章
cursor = db.cursor(MySQLdb.cursors.DictCursor)
cursor.execute("select cid, title, slug, text, created from typecho_contents where type='post'")
# 创建分类和标签
entries = cursor.fetchall()
for e in entries:
e = Box(e)
title = e.title
title = title.strip(' ')
urlname = f'/archives/{e.slug}/'
print(title)
content = str(e.text).replace('<!--markdown-->', '')
tags = []
category = ""
# 找出文章的tag及category

cursor = db.cursor(MySQLdb.cursors.DictCursor)
cursor.execute("select type, name, slug from `typecho_relationships` ts, typecho_metas tm where tm.mid = ts.mid and ts.cid = %d" % e.cid)
# 创建分类和标签
metas = cursor.fetchall()

for m in metas:
m = Box(m)
if m.type == 'tag':
tags.append(m.name)
if m.type == 'category':
category = m.slug
# 这里主要是适配我的文章链接格式:new_post_name: :year/:month:day :title/index.md
path = 'data/_posts/'
if not os.path.exists(path):
os.makedirs(path)
path += f"{arrow.get(e.created).format('YYYY')}/"
if not os.path.exists(path):
os.makedirs(path)
path += arrow.get(e.created).format('MMDD') + " " + title.replace('/', '-')
if not os.path.exists(path):
os.makedirs(path)
f = open(f"{path}/index.md", 'w', encoding="utf-8")
f.write("---\n")
f.write("title: %s\n" % title)
f.write("date: %s\n" % arrow.get(e.created).format('YYYY-MM-DD HH:mm:ss'))
f.write("tags:\n- %s\n" % category)
f.write("tags: [%s]\n" % ','.join(tags))
f.write("permalink: %s\n" % urlname)
f.write("---\n")
f.write(content)
f.close()

def main():
# 把数据库连接信息

db = MySQLdb.connect("localhost", "username", "password", "typecho", charset='utf8')

create_data(db)

if __name__ == "__main__":
main()

然后由于格式比较混乱,用 lint-md 把所有 markdown 文件格式化一下。

然后由于我博客的评论质量不是特别高也不多,就不迁移了…

这里面的 permalink 主要是为了保留文章原来的链接,保证 SEO 和以前发布到外部的外链不受影响,算是一点历史包袱吧。
原本还想统一新格式,在老链接里面做 301 跳转,但是觉得这样与 hexo 的纯静态有点不相符,得把配置放 nginx,包袱更重了,万一下次还想换程序就痛苦面具了。