笔记保存和同步方法

方案

编辑工具:obsdian
同步工具--上传:obsdian插件--sync cnblg
同步工具--下载:vscode插件--博客园 cnblogs 客户端
云端:博客园

流程:

  1. obsdian编辑文章时设置标签,格式为 0级父目录/1级父目录。。。
  2. 右键点击文件上传--sync cnblg
  3. 使用脚本收集obsdian配置并上传
  4. 在下载的机器上配置vscode插件--博客园 cnblogs ,将工作区设置为obsdian note目录
  5. 使用插件--博客园 cnblogs 客户端下载指定文件
  6. 使用命令行删除掉文件名后缀,对应脚本如下
  7. 使用obsdian打开工作区,继续编辑

3收集obsdian配置

import os  
import sys  
  
# 指定你想要修改文件名的根目录路径  
current_directory = ""  
if getattr(sys, 'frozen', False):  
    current_directory = os.path.dirname(os.path.abspath(sys.executable))  
elif __file__:  
    current_directory = os.path.dirname(os.path.abspath(__file__))  
  
print(f"{current_directory}")  
  
  
content = """  
---  
tags:  
  - 按目录结构/obsdian/插件  
---  
"""  
  
  
def find_obsidian_dir(start_path):  
    current_path = start_path  
    while current_path != os.path.abspath(os.sep):  
        if '.obsidian' in os.listdir(current_path):  
            return os.path.join(current_path, '.obsidian')  
        current_path = os.path.abspath(os.path.join(current_path, '..'))  
    return None  
  
  
def write_files_to_sync(obsidian_dir):  
    files_to_read = ['community-plugins.json', 'core-plugins.json', 'hotkeys.json']  
    sync_file_path = os.path.join(current_directory, 'sync.md')  
  
    # 如果 sync.md 存在,则删除它  
    if os.path.exists(sync_file_path):  
        os.remove(sync_file_path)  
  
    with open(sync_file_path, 'w', encoding='utf-8') as sync_file:  
        sync_file.write(content)  
        for file_name in files_to_read:  
            file_path = os.path.join(obsidian_dir, file_name)  
            if os.path.exists(file_path):  
                with open(file_path, 'r') as file:  
                    sync_file.write(f"### {file_name}\n")  
                    sync_file.write(file.read() + '\n\n')  
  
  
def list_plugins(plugins_folder):  
    # 定义plugins.md文件的路径  
    plugins_file = os.path.join(current_directory, "plugins.md")  
  
    # 如果文件存在,删除它  
    if os.path.exists(plugins_file):  
        os.remove(plugins_file)  
  
    # 创建并写入新的plugins.md文件  
    with open(plugins_file, 'w', encoding='utf-8') as file:  
        file.write(content)  
        # 遍历当前路径下的所有目录  
        for dir_name in os.listdir(plugins_folder):  
            # 检查是否为目录  
            if os.path.isdir(os.path.join(plugins_folder, dir_name)):  
                # 将目录名称按行写入文件  
                file.write(dir_name + '\n')  
  
    print(f"目录名称已写入 {plugins_file}")  
  
  
def main():  
    obsidian_dir = find_obsidian_dir(current_directory)  
    if obsidian_dir:  
        write_files_to_sync(obsidian_dir)  
        print(f"{obsidian_dir} 中的文件已写入 sync.md")  
        plugins_folder = os.path.join(obsidian_dir, "plugins")  
        list_plugins(plugins_folder)  
    else:  
        print("未找到 .obsidian 目录")  
  
  
if __name__ == "__main__":  
    main()

5将以下文件置于工作区目录下,执行

import os  
import re  
import sys  
from pathlib import Path  
  
def rename_md_files(directory):  
    # 使用Path库来递归遍历目录及其子目录  
    print(f"{Path(directory).rglob('*.md')}")  
    for filepath in Path(directory).rglob('*.md'):  
        filename = filepath.name  
        # 使用正则表达式查找倒数第二个.及其后的数字  
        match = re.search(r'(\.[^.]+)?\.(\d+)(?=\.\w+$)', filename)  
        if match:  
            # 构造新的文件名  
            new_filename = filename[:match.start()] + filename[match.end():]  
            new_filepath = filepath.with_name(new_filename)  
            if os.path.exists(new_filepath):  
                print(f' warning "{new_filepath}" exists,can not rename "{filepath}"')  
                continue  
            print(f'Renaming "{filepath}" to "{new_filepath}"')  
            # 重命名文件  
            try:  
                os.rename(filepath, new_filepath)  
            except Exception as e:  
                print(f"Failed to rename {filepath}: {e}")  
  
  
if __name__ == '__main__':  
    # 指定你想要修改文件名的根目录路径  
    current_directory = ""  
    if getattr(sys, 'frozen', False):  
        current_directory = os.path.dirname(os.path.abspath(sys.executable))  
    elif __file__:  
        current_directory = os.path.dirname(os.path.abspath(__file__))  
  
    print(f"{current_directory}")  
    rename_md_files(current_directory)

进度

posted @   40207123  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示