前言
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
   | 
  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 = ""         
          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                  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,包袱更重了,万一下次还想换程序就痛苦面具了。