2024-09-12 更新
-
推荐使用 python 脚本,对于其他导入的 md,会能够自动正则匹配第一个日期,作为创建时间。比如 front matter 中的 data: 2012-12-12 12:12:32
-
支持格式:
- 2012-12-12 12:12:32
- 2012-12-12,日期 + 导入时间
- 自己输入时间
-
优化一个问题:
- 当一个文档有下层文件夹时,还需要将文件夹的名称也进行替换。
前言
因为有的笔记是以前别的,但是导入思源后,笔记的创建时间是按照导入时间算的,所以如果需要 hugo 等需要时间情景的时候,就不准确了。
工具:quicker (工具箱,用来做可视化脚本很方便)
动作:https://getquicker.net/Sharedaction?code=d0c0c66d-e955-4bf2-de93-08dc5fed4e6b
原理:
1. 修改笔记文件名,因为文件名带有时间戳; 2. 修改文件文件内部的nodedocument名称,因为其也带有时间戳; 3. 重建索引,必须的,但是不必改一个就重建一下,可以多改几个之后再重建也行。
python 脚本
传参方式:
- 直接运行,输入路径的时候将笔记文件拖过来会自动输入,
- 输入日期,格式:20240101,然后重建索引即可。
该脚本用来修改之前的 hexo 博客的文档,因为 front-matter 中带有日期,所以不用输入,如果需要输入的,需要修改其中的代码片段。
import re import os import json file_list_str = input("请输入文件列表,用逗号隔开:") file_list = file_list_str.split(',') for file_path in file_list: file_path = file_path.strip() try: # 获取文件名 file_name = os.path.basename(file_path) oldtime = file_name[:14] with open(file_path, 'r', encoding='utf-8') as f: content = f.read() match = re.search(r'(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})', content) # 获取当前的笔记名称 content_dict = json.loads(content) print("当前文件:"+content_dict["Properties"]["title"]) if match: date_str = match.group(1) numbers = re.findall(r'\d', date_str) newtime = "".join(numbers) else: new_match = re.search(r'(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})', content) if new_match: date_str = new_match.group(1) numbers = re.findall(r'\d', date_str) newtime = "".join(numbers) else: new_match = re.search(r'(\d{4}-\d{2}-\d{2})', content) if new_match: date_str = new_match.group(1) newtime = date_str.replace('-', '') + oldtime[-6:] else: # 如果还找不到就只能自己输入时间了 print(f"找不到时间,文件定位:{file_name}") s = input("请输入时间:20121245或2012-12-12:") or "20211106" numbers = re.findall(r'\d', s) newtime = "".join(numbers) newtime=newtime+oldtime[-6:] print("你自己输入的new为:"+newtime) # continue oldtime_short = oldtime[:8] oldtime_escaped = re.escape(oldtime) newtime_escaped = re.escape(newtime) print("旧时间:"+oldtime_escaped+"\t新时间:"+newtime_escaped) new_content = re.sub(oldtime_escaped, newtime_escaped, content) # 替换文件名中的 oldtime 为 newtime new_file_name = file_name.replace(oldtime, newtime) with open(file_path, 'w', encoding='utf-8') as f_out: f_out.write(new_content) # 替换文件名称 os.rename(file_path, file_path.replace(oldtime, newtime)) # 同时查看是否有同名的文件夹,也进行替换 old_folder_dir=file_path.split('.')[0] if os.path.exists(old_folder_dir): parts=old_folder_dir.split('\\') parts[-1] = parts[-1].replace(oldtime_escaped, newtime_escaped) new_folder_dir='\\'.join(parts) print(f"存在文件夹,进行替换:{old_folder_dir}→{new_folder_dir}") os.rename(old_folder_dir,new_folder_dir) except FileNotFoundError: print(f"文件 {file_path} 不存在。")
手搓单个
如果只是改一个的话,可以用以下脚本即可。
-
思源笔记1打开文件位置
-
文件名就是时间戳,需要修改文件名
-
点击打开这个 sy 文件,里面去把所有的该时间文件名替换成修改后的就行了。
-
重建索引
思源笔记
样式设置
- logseq[^2]的引用样式:
- siyuan-logseq 双链引用 css[^3]
- sy_query 挂件[^5]
- 思源 api 配置[^6]
- 思源笔记更改笔记创建时间[^7]
- 思源自定义字体[^8] ↩
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于