ai_xiaopei
9 days ago 4cd5499d60a851a59723067e74a4a92f7254b8e9
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
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python3
"""
根据 migration-plan.json 更新所有文档的 wikilinks
"""
 
import json
import os
import re
import sys
from pathlib import Path
 
def load_migration_plan(vault_path):
    """加载迁移计划"""
    with open(os.path.join(vault_path, 'migration-plan.json'), 'r', encoding='utf-8') as f:
        return json.load(f)
 
def build_path_mapping(migration_plan):
    """构建路径映射表(旧路径 -> 新路径)"""
    mapping = {}
    for migration in migration_plan['migrations']:
        source = migration['source']
        target = migration['target']
        # 移除 .md 扩展名用于匹配
        source_base = os.path.splitext(source)[0]
        target_base = os.path.splitext(target)[0]
        mapping[source_base] = target_base
        
        # 也添加文件名到完整路径的映射
        source_name = os.path.basename(source_base)
        mapping[source_name] = target_base
    
    return mapping
 
def update_wikilinks(file_path, path_mapping):
    """更新文件中的 wikilinks"""
    with open(file_path, 'r', encoding='utf-8') as f:
        content = f.read()
    
    original_content = content
    
    # 查找所有 [[...]] 格式的 wikilinks
    # 支持 [[路径]] 和 [[路径|显示文本]] 格式
    pattern = r'\[\[([^\]|]+)(\|[^\]]+)?\]\]'
    
    def replace_wikilink(match):
        link_path = match.group(1).strip()
        display_text = match.group(2) or ''
        
        # 尝试在映射中查找
        if link_path in path_mapping:
            new_path = path_mapping[link_path]
            return f'[[{new_path}{display_text}]]'
        
        # 尝试匹配文件名(不含路径)
        link_name = os.path.basename(link_path)
        if link_name in path_mapping:
            new_path = path_mapping[link_name]
            return f'[[{new_path}{display_text}]]'
        
        # 未找到匹配,保持原样
        return match.group(0)
    
    content = re.sub(pattern, replace_wikilink, content)
    
    if content != original_content:
        with open(file_path, 'w', encoding='utf-8') as f:
            f.write(content)
        return True
    
    return False
 
def main():
    vault_path = sys.argv[1] if len(sys.argv) > 1 else '.'
    
    # 加载迁移计划
    migration_plan = load_migration_plan(vault_path)
    path_mapping = build_path_mapping(migration_plan)
    
    print(f'已加载 {len(path_mapping)} 个路径映射')
    
    # 更新所有迁移后的文件
    updated = 0
    total = 0
    
    # 遍历新目录结构
    for platform_dir in ['电子秤平台', '共有硬件', '通用', '第三方平台', '运营管理平台']:
        platform_path = os.path.join(vault_path, platform_dir)
        if not os.path.exists(platform_path):
            continue
        
        for md_file in Path(platform_path).rglob('*.md'):
            total += 1
            if update_wikilinks(str(md_file), path_mapping):
                print(f'已更新 wikilinks: {md_file.relative_to(vault_path)}')
                updated += 1
    
    print(f'\n更新完成: {updated}/{total} 个文件的 wikilinks 已更新')
 
if __name__ == '__main__':
    main()