rename_tag
Rename tags across your Obsidian vault by specifying old and new tag names to update all occurrences consistently.
Instructions
Obsidianボルト内のタグを一括でリネームします
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| oldTag | Yes | 変更前のタグ名(#付きまたは#なし) | |
| newTag | Yes | 変更後のタグ名(#付きまたは#なし) |
Implementation Reference
- src/obsidian-handler.ts:141-213 (handler)Core handler function that recursively scans all .md files in the Obsidian vault, normalizes tags with # if missing, replaces oldTag with newTag in inline tag positions and YAML frontmatter tags arrays using regex, updates modified files, counts updates, and returns a success message.async renameTag(oldTag: string, newTag: string): Promise<string> { const vaultPath = this.config.vaultPath; let updatedFilesCount = 0; // #を自動的に追加 const oldTagNormalized = oldTag.startsWith('#') ? oldTag : `#${oldTag}`; const newTagNormalized = newTag.startsWith('#') ? newTag : `#${newTag}`; const processFile = async (filePath: string) => { try { const content = await fs.readFile(filePath, 'utf-8'); let updatedContent = content; let hasChanges = false; // インラインタグを置換 const tagPattern = new RegExp(`\\${oldTagNormalized}(?=\\s|$)`, 'g'); if (tagPattern.test(content)) { updatedContent = updatedContent.replace(tagPattern, newTagNormalized); hasChanges = true; } // YAML frontmatterのtagsフィールドを置換 const yamlMatch = content.match(/^---\s*\n([\s\S]*?)\n---/); if (yamlMatch) { const yamlContent = yamlMatch[1]; const tagsMatch = yamlContent.match(/tags:\s*\[(.*?)\]/); if (tagsMatch) { const tagsList = tagsMatch[1]; const oldTagInYaml = oldTagNormalized.substring(1); // #を除去 const newTagInYaml = newTagNormalized.substring(1); // #を除去 if (tagsList.includes(oldTagInYaml)) { const updatedTagsList = tagsList.replace( new RegExp(`(['"]?)${oldTagInYaml.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\1`, 'g'), `$1${newTagInYaml}$1` ); updatedContent = updatedContent.replace(tagsMatch[0], `tags: [${updatedTagsList}]`); hasChanges = true; } } } if (hasChanges) { await fs.writeFile(filePath, updatedContent, 'utf-8'); updatedFilesCount++; } } catch (error) { // ファイル読み込みエラーは無視 } }; const processDirectory = async (dirPath: string) => { try { const entries = await fs.readdir(dirPath, { withFileTypes: true }); for (const entry of entries) { const fullPath = path.join(dirPath, entry.name); if (entry.isDirectory()) { await processDirectory(fullPath); } else if (entry.isFile() && entry.name.endsWith('.md')) { await processFile(fullPath); } } } catch (error) { // ディレクトリ読み込みエラーは無視 } }; await processDirectory(vaultPath); return `タグ '${oldTagNormalized}' を '${newTagNormalized}' に変更しました。${updatedFilesCount}個のファイルを更新しました。`; }
- src/server.ts:129-145 (schema)Input schema definition for the rename_tag tool, specifying required string parameters oldTag and newTag with descriptions.name: 'rename_tag', description: 'Obsidianボルト内のタグを一括でリネームします', inputSchema: { type: 'object', properties: { oldTag: { type: 'string', description: '変更前のタグ名(#付きまたは#なし)', }, newTag: { type: 'string', description: '変更後のタグ名(#付きまたは#なし)', }, }, required: ['oldTag', 'newTag'], }, },
- src/server.ts:307-314 (registration)Tool dispatch/registration in the CallToolRequestSchema handler switch statement, extracting arguments and calling the ObsidianHandler.renameTag method.case 'rename_tag': const renameResult = await this.obsidianHandler.renameTag( args.oldTag as string, args.newTag as string ); return { content: [{ type: 'text', text: renameResult }], };