不知道啥时候开始,OneDrvie的空间已经不够用了,再加上天擎乱杀我的笔记。。。
而语雀有免费的10G流量,不多bb,直接把笔记迁移到语雀上
但是直接压缩导入的话,语雀会保留_v_attachments
、_vnote*.json
、_v_images
、_v_attachments
等,所以需要手动删除
ps: 我基本没什么附件,因此直接删
_v_attachments
,如果你有,那你就需要自行处理了。。
附上vnote转换到语雀的脚本
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2021-04-16 11:41:03
# @Author : 江南小虫虫 (fwh13612265462@gmail.com)
# @Link : https://fengwenhua.top
import os
import re
import shutil
"""
* 删除 _v_attachments
* 删除 _v_recycle_bin文件夹
* 将 _v_images 图片移动到上一级文件夹
* 遍历所有md,把_v_images/改成./
* 删除所有的 _vnote*.json
"""
##########################################################################################
# 文件夹(笔记本) 绝对路径
user_input_path = '/Users/fengwenhua/Documents/开发'
# 转换完成后,进入该文件夹,把里面的文件压缩,上传给语雀就行
##########################################################################################
def mv_img_file(t_dir):
"""根据给出的目录,删除json文件和几个文件夹
"""
img_dirs = []
for root, dirs, files in os.walk(t_dir):
for dir in dirs:
if dir == "_v_images":
img_dirs.append(root + "/" + dir)
for img_dir in img_dirs:
for root, dirs, files in os.walk(img_dir):
for file in files:
# print(file)
shutil.move(root + "/" + file, root + "/../" + file)
for img_dir in img_dirs:
shutil.rmtree(img_dir)
return True
def rm_file_dir(t_dir):
"""根据给出的目录,把_v_images目录下的文件移动到上一级目录
"""
# _v_attachments、_v_recycle_bin、.vswp和_vnote*.json
del_dirs = []
del_files = []
for root, dirs, files in os.walk(t_dir):
for dir in dirs:
if dir == "_v_attachments" or dir == "_v_recycle_bin":
# print("文件夹:" + root + "/" + dir)
del_dirs.append(root + "/" + dir)
for file in files:
if file.endswith(".json") or file.endswith(".vswp"):
# print("文件:" + root + "/" + file)
del_files.append(root + "/" + file)
for d in del_files:
os.remove(d)
for d in del_dirs:
shutil.rmtree(d)
return True
def find_md_file(dir):
"""根据给出的目录, 找到该目录下的所有的markdown文件
:param dir: 目录
:return: markdown的绝对路径列表
"""
md_files = []
for root, dirs, files in os.walk(dir):
# print('当前目录路径: ', root) # 当前目录路径
# print('当前路径下所有子目录: ', dirs) # 当前路径下所有子目录
# print('当前路径下所有非目录子文件: ', files) # 当前路径下所有非目录子文件
for file in files:
if file.endswith('.md'):
md_files.append(root + "/" + file)
# print('总共markdown文件数量是: ', len(md_files))
# for file in md_files:
# print(file)
return md_files
def find_replace_img(md_file):
"""根据给出的markdown文件, 找到里面的本地图片, 替换 _v_images 为 .
:param md_file: md文件
:return: 全部是本地图片的markdown文件
"""
# print("开始处理 ", md_file)
content = ""
with open(md_file, "r", encoding='utf8') as f:
content = f.read()
# print("markdown文档所在路径: ", os.path.dirname(md_file))
md_file_dir = os.path.dirname(md_file)
# print('文件内容: ', content)
pattern = re.compile(r'!\[.*?\]\((.*?)\)')
finded_img_urls = pattern.findall(content)
# print('该文档里的所有图片链接: ')
# print(finded_img_urls)
# print("\n")
have_local_img = False
if finded_img_urls: # 找到图片的链接
for url in finded_img_urls:
if "_v_images" in url: # 本地图片
have_local_img = True
img_url = url.replace("_v_images", ".")
content = content.replace(url, img_url)
with open(md_file, "w", encoding='utf8') as f:
f.write(content)
# print(url + " 已处理完成!")
else: # 在线图片
continue
# 最后将内容写进文件
if have_local_img:
print(md_file + " 已处理完成!")
else:
print(md_file + " 没有需要替换的图片")
return True
else:
print(md_file + " 没有图片")
return True
if __name__ == '__main__':
if os.path.exists(user_input_path): # 路径存在
print("=" * 50)
print("开始删除_v_attachments、_v_recycle_bin和_vnote*.json")
print("=" * 50)
rm_status = rm_file_dir(user_input_path)
if rm_status:
print()
else:
print("未知原因删除失败")
exit(-1)
print("=" * 50)
print("开始处理md文件")
print("=" * 50)
md_files = find_md_file(user_input_path) # 根据给出的目录寻找md文件
for file in md_files:
# 对于每个md文件, 分析其中的本地图片, 然后替换_v_images/ 为./
if not find_replace_img(file):
print("未知原因导致图片替换失败, 请检查具体原因, 然后重试")
exit(-1)
else:
print()
print("=" * 50)
print("开始移动图片")
print("=" * 50)
mv_status = mv_img_file(user_input_path)
if mv_status:
print()
else:
print("未知原因删除失败")
exit(-1)
else:
print('请给出正确的路径')
exit(-1)
print("=" * 50)
print("ALL DONE!!")
print("=" * 50)
转换完后,一个个导入即可。
版权属于:江南小虫虫
本文链接:https://fengwenhua.top/index.php/archives/68/
转载时须注明出处及本声明