obsidian_fetch 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +4 -0
- data/exe/obsidian_fetch +73 -0
- data/lib/obsidian_fetch/version.rb +5 -0
- data/lib/obsidian_fetch.rb +199 -0
- data/sig/obsidian_fetch.rbs +4 -0
- metadata +69 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c9de6573e2f1754f39f00139ab39a74f292cb43bd838e2524e68f0e1bd1d7283
|
4
|
+
data.tar.gz: 4f661ae683d1e7c140a8fba607c1f3258e6bc1677d0b9e274c7e67bd0753f03b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0b0f956739d3eba9ebb28bd28786169f164e9aced9bdff57f9dde7769dc34de7b30e3e60ce18689bcaa90e3fe907f76b94266069894fa24868c6dd0e5f1d917b
|
7
|
+
data.tar.gz: eb680c31c93783621717e69b28dac56ed1843a3838d73a74871c65c8b130bba544c3049bffcce2ad25b796f21a04734ed6e9e45376efde4b09ebbe9d9dbec77a
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 soukouki
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# ObsidianFetch
|
2
|
+
|
3
|
+
TODO: Delete this and the text below, and describe your gem
|
4
|
+
|
5
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/obsidian_fetch`. To experiment with that code, run `bin/console` for an interactive prompt.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
10
|
+
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
12
|
+
|
13
|
+
```bash
|
14
|
+
bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
15
|
+
```
|
16
|
+
|
17
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
18
|
+
|
19
|
+
```bash
|
20
|
+
gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
21
|
+
```
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake ` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/obsidian_fetch.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/exe/obsidian_fetch
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# !/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'obsidian_fetch'
|
5
|
+
|
6
|
+
|
7
|
+
name 'obsidian-fetch'
|
8
|
+
|
9
|
+
version '0.1.0'
|
10
|
+
|
11
|
+
vault_pathes = ARGV
|
12
|
+
|
13
|
+
vault = Vault.new(vault_pathes)
|
14
|
+
STDERR.puts "Found #{vault.notes.size} notes"
|
15
|
+
STDERR.puts "Found #{vault.links_by_file_name.size} links and #{vault.links_by_file_path.size} files linked by notes"
|
16
|
+
|
17
|
+
tool 'read' do
|
18
|
+
description <<~EOS
|
19
|
+
Read a note from Obsidian vault.
|
20
|
+
If I find multiple notes with the same name, I will show you all of them.
|
21
|
+
EOS
|
22
|
+
argument :name, String, required: true, description: "Note name to read"
|
23
|
+
call do |args|
|
24
|
+
name = args[:name]
|
25
|
+
# 名前が文字列でない場合
|
26
|
+
next 'Name must be a string' unless name.is_a?(String)
|
27
|
+
tool_read(vault, name)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
tool 'read_multiple' do
|
32
|
+
description <<~EOS
|
33
|
+
Read a notes from Obsidian vault.
|
34
|
+
If I find multiple notes with the same name, I will show you all of them.
|
35
|
+
EOS
|
36
|
+
argument :names, Array, items: String, required: true, description: "Note names to read"
|
37
|
+
call do |args|
|
38
|
+
names = args[:names]
|
39
|
+
# 名前が文字列の配列でない場合
|
40
|
+
next 'Name must be an array of strings' unless names.is_a?(Array) && names.all? { |name| name.is_a?(String) }
|
41
|
+
names.map do |name|
|
42
|
+
tool_read(vault, name)
|
43
|
+
end.join("\n\n---\n\n")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
tool 'list' do
|
48
|
+
description <<~EOS
|
49
|
+
Search for files with matching names partially.
|
50
|
+
EOS
|
51
|
+
argument :name, String, required: true, description: "Note name to search"
|
52
|
+
call do |args|
|
53
|
+
name = args[:name]
|
54
|
+
# 名前が文字列でない場合
|
55
|
+
next 'Name must be a string' unless name.is_a?(String)
|
56
|
+
tool_list(vault, name)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
tool 'list_multiple' do
|
61
|
+
description <<~EOS
|
62
|
+
Search for files with matching names partially.
|
63
|
+
EOS
|
64
|
+
argument :names, Array, items: String, required: true, description: "Note names to search"
|
65
|
+
call do |args|
|
66
|
+
names = args[:names]
|
67
|
+
# 名前が文字列の配列でない場合
|
68
|
+
next 'Name must be an array of strings' unless names.is_a?(Array) && names.all? { |name| name.is_a?(String) }
|
69
|
+
names.map do |name|
|
70
|
+
tool_list(vault, name)
|
71
|
+
end.join("\n\n---\n\n")
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,199 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'yaml'
|
4
|
+
require 'date'
|
5
|
+
require 'mcp'
|
6
|
+
|
7
|
+
require_relative "obsidian_fetch/version"
|
8
|
+
|
9
|
+
module ObsidianFetch
|
10
|
+
class Error < StandardError; end
|
11
|
+
|
12
|
+
class Vault
|
13
|
+
attr_reader :notes, :links_by_file_path, :links_by_file_name
|
14
|
+
|
15
|
+
def initialize vault_pathes
|
16
|
+
@vault_pathes = vault_pathes
|
17
|
+
|
18
|
+
# key: note_name, value: [file_pathes]
|
19
|
+
@notes = {}
|
20
|
+
|
21
|
+
# key: file_path, value: [file_pathes]
|
22
|
+
@links_by_file_path = {}
|
23
|
+
|
24
|
+
# ノートが見つからなかった場合に被リンクを表示するためのハッシュ
|
25
|
+
# key: note_name, value: [file_pathes]
|
26
|
+
@links_by_file_name = {}
|
27
|
+
|
28
|
+
collect_pathes()
|
29
|
+
collect_notes()
|
30
|
+
collect_links()
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def collect_pathes
|
36
|
+
@all_pathes = @vault_pathes.flat_map do |vault_path|
|
37
|
+
Dir.glob("#{vault_path}/**/*.md")
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# ノート名を正規化する
|
42
|
+
# ファイル名に使えない文字については除去しない。aliasで設定されている場合もあるため。
|
43
|
+
def self.normalize_note_name(note_name)
|
44
|
+
# ノート名を小文字に変換し、リンクに使えない文字を除去する
|
45
|
+
note_name.gsub(/[\[\]#\^\|]/, '')
|
46
|
+
end
|
47
|
+
|
48
|
+
def collect_notes
|
49
|
+
# ノート名を整理するための処理
|
50
|
+
@all_pathes.each do |file_path|
|
51
|
+
file_name = File.basename(file_path, '.md')
|
52
|
+
file_name = Vault.normalize_note_name(file_name)
|
53
|
+
@notes[file_name] ||= []
|
54
|
+
@notes[file_name] << file_path
|
55
|
+
content = open(file_path) { |f| f.read.force_encoding('UTF-8') }
|
56
|
+
# もしaliasが設定されていれば、それも追加する
|
57
|
+
frontmatter_str = content.match(/\A---(.*?\n)---/m)&.[](1)
|
58
|
+
if frontmatter_str
|
59
|
+
begin
|
60
|
+
frontmatter = YAML.safe_load(frontmatter_str, symbolize_names: true, aliases: true, permitted_classes: [Date])
|
61
|
+
rescue Psych::SyntaxError => e
|
62
|
+
puts "YAML syntax error in #{file_path}: #{e.message}"
|
63
|
+
next
|
64
|
+
end
|
65
|
+
(frontmatter&.[](:aliases) || []).each do |alias_name|
|
66
|
+
alias_name = Vault.normalize_note_name(alias_name)
|
67
|
+
@notes[alias_name] ||= []
|
68
|
+
@notes[alias_name] << file_path
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
@notes.each{|_name, file_pathes| file_pathes.uniq!}
|
73
|
+
end
|
74
|
+
|
75
|
+
def collect_links
|
76
|
+
# リンクを整理するための処理
|
77
|
+
# リンクを整理するためにノート名のリストを使用するため、ここで処理する
|
78
|
+
@all_pathes.each do |file_path|
|
79
|
+
file_name = File.basename(file_path, '.md')
|
80
|
+
file_name = Vault.normalize_note_name(file_name)
|
81
|
+
content = open(file_path) { |f| f.read.force_encoding('UTF-8') }
|
82
|
+
# もしリンクが設定されていれば、linksに追加する
|
83
|
+
# [[link]]と[[link|displayname]]の場合
|
84
|
+
# linkに.mdが付いている場合、付いていない場合両方を考慮する
|
85
|
+
content.scan(/\[\[(.*?)(?:\|.*)?\]\]/) do |match|
|
86
|
+
link_name = match[0]
|
87
|
+
link_name = link_name.sub(/\.md$/, '') # .mdを削除
|
88
|
+
# #、^以降の文字列を削除
|
89
|
+
link_name = link_name.sub(/#.*$/, '')
|
90
|
+
link_name = Vault.normalize_note_name(link_name)
|
91
|
+
next if link_name == file_name # 自分自身をリンクしている場合は無視する
|
92
|
+
@links_by_file_name[link_name] ||= []
|
93
|
+
@links_by_file_name[link_name] << file_path
|
94
|
+
# 同名のノートがある場合に、どこにリンクしているかを処理するのは結構難しい
|
95
|
+
# ここでは簡単のため、同名のノートがある場合はすべてのファイルをリンク先として追加する
|
96
|
+
# ただし、aliasは除外する
|
97
|
+
linked_candidates = (@notes[link_name] || []).filter{|path| path.include?(link_name + '.md') }
|
98
|
+
linked_candidates.each do |linked_candidate|
|
99
|
+
@links_by_file_path[linked_candidate] ||= []
|
100
|
+
@links_by_file_path[linked_candidate] << file_path
|
101
|
+
end
|
102
|
+
end
|
103
|
+
# [displayname](path)の場合
|
104
|
+
# pathの末尾に.mdが付いている場合、付いていない場合両方を考慮する
|
105
|
+
content.scan(/\[(.*?)\]\((?!\[\[)(.*?)(?<!\]\])\)/) do |match|
|
106
|
+
path = match[1]
|
107
|
+
# `(.+)://`から始まる場合は除外する
|
108
|
+
next if path =~ /^[a-z]+:\/\// # 外部リンク
|
109
|
+
# #、^以降の文字列を削除
|
110
|
+
path = path.sub(/#.*$/, '')
|
111
|
+
path = path.sub(/\.md$/, '') # .mdを削除
|
112
|
+
link_name = File.basename(path)
|
113
|
+
link_name = Vault.normalize_note_name(link_name)
|
114
|
+
@links_by_file_name[link_name] ||= []
|
115
|
+
@links_by_file_name[link_name] << file_path
|
116
|
+
# Obsidianはかなり賢くて、リンク先のファイルが無い場合には、その配下のファイルを探してくれる
|
117
|
+
# 今回の実装ではそこまで考慮せず、pathを信用する
|
118
|
+
path_from_vault = File.join(File.dirname(file_path), path) + '.md'
|
119
|
+
@links_by_file_path[path_from_vault] ||= []
|
120
|
+
@links_by_file_path[path_from_vault] << file_path
|
121
|
+
end
|
122
|
+
end
|
123
|
+
@links_by_file_name.each do |_file_name, file_pathes|
|
124
|
+
file_pathes.uniq!
|
125
|
+
end
|
126
|
+
@links_by_file_path.each do |_file_path, file_pathes|
|
127
|
+
file_pathes.uniq!
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
def tool_read vault, name
|
133
|
+
name = Vault.normalize_note_name(name)
|
134
|
+
file_pathes = vault.notes[name]
|
135
|
+
# 名前のノートが存在しない場合
|
136
|
+
if file_pathes.nil?
|
137
|
+
return "Note not found: #{name}" if vault.links_by_file_name[name].nil?
|
138
|
+
file_pathes = vault.links_by_file_name[name]
|
139
|
+
return <<~EOS
|
140
|
+
Note not found: #{name}
|
141
|
+
However, I found other notes linked to this note.
|
142
|
+
#{file_pathes.map { |file_path| "- #{file_path}" }.join("\n")}
|
143
|
+
EOS
|
144
|
+
end
|
145
|
+
|
146
|
+
# 複数のファイルがある場合は、---とファイル名で区切って返す
|
147
|
+
file_pathes.map do |file_path|
|
148
|
+
content = open(file_path) { |f| f.read.force_encoding('UTF-8') }
|
149
|
+
link_notes = if vault.links_by_file_path[file_path].nil?
|
150
|
+
""
|
151
|
+
else
|
152
|
+
<<~EOS
|
153
|
+
This note is linked by the following notes:
|
154
|
+
#{(vault.links_by_file_path[file_path] || []).shuffle.map { |file_path| "- #{File.basename(file_path, '.md')}" }.join("\n")}
|
155
|
+
EOS
|
156
|
+
end
|
157
|
+
preface = <<~EOS
|
158
|
+
The contents of the note '#{name}' is as follows.
|
159
|
+
#{link_notes}
|
160
|
+
---
|
161
|
+
|
162
|
+
EOS
|
163
|
+
preface + content
|
164
|
+
end.join("\n\n---\n\n")
|
165
|
+
end
|
166
|
+
|
167
|
+
MAX_LIST_SIZE = 20
|
168
|
+
def tool_list vault, name
|
169
|
+
name = Vault.normalize_note_name(name)
|
170
|
+
split_name = name.split(/[\s ]+/)
|
171
|
+
matched_notes = vault.notes.select do |note_name, _file_pathes|
|
172
|
+
split_name.map {|name_part| note_name.include?(name_part) }.all?
|
173
|
+
end
|
174
|
+
# 名前で検索したが見つからない場合
|
175
|
+
if matched_notes.empty?
|
176
|
+
has_found_link = vault.links_by_file_name[name] && !vault.links_by_file_name[name].empty?
|
177
|
+
return <<~EOS unless has_found_link
|
178
|
+
Note not found: #{name}
|
179
|
+
Search again with a substring or a string with a different notation.
|
180
|
+
EOS
|
181
|
+
return <<~EOS
|
182
|
+
Note not found: #{name}
|
183
|
+
However, I found other notes linked to this note.
|
184
|
+
#{vault.links_by_file_name[name].shuffle.map { |file_path| "- #{File.basename(file_path, '.md')}" }.join("\n")}
|
185
|
+
EOS
|
186
|
+
end
|
187
|
+
# マッチした名前の数が多すぎる場合は、ランダムにMAX_LIST_SIZE個選ぶ
|
188
|
+
preface = "Notes matching '#{name}' are as follows.\n"
|
189
|
+
if matched_notes.size > MAX_LIST_SIZE
|
190
|
+
matched_notes = matched_notes.to_a.sample(MAX_LIST_SIZE).to_h
|
191
|
+
preface = "Too many notes matched. I will show you only #{MAX_LIST_SIZE} of them.\n" + preface
|
192
|
+
end
|
193
|
+
# マッチした名前のリストで返す
|
194
|
+
list = matched_notes.keys.shuffle.map do |note_name|
|
195
|
+
"- #{note_name}"
|
196
|
+
end.join("\n")
|
197
|
+
preface + list
|
198
|
+
end
|
199
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: obsidian_fetch
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- sou7
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-04-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mcp-rb
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.3.3
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.3.3
|
27
|
+
description:
|
28
|
+
email:
|
29
|
+
- soukouki0@yahoo.co.jp
|
30
|
+
executables:
|
31
|
+
- obsidian_fetch
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- CHANGELOG.md
|
36
|
+
- LICENSE.txt
|
37
|
+
- README.md
|
38
|
+
- Rakefile
|
39
|
+
- exe/obsidian_fetch
|
40
|
+
- lib/obsidian_fetch.rb
|
41
|
+
- lib/obsidian_fetch/version.rb
|
42
|
+
- sig/obsidian_fetch.rbs
|
43
|
+
homepage: https://ob.sou7.io/2025-04/week17/obsidian_fetch
|
44
|
+
licenses:
|
45
|
+
- MIT
|
46
|
+
metadata:
|
47
|
+
homepage_uri: https://ob.sou7.io/2025-04/week17/obsidian_fetch
|
48
|
+
source_code_uri: https://github.com/soukouki/obsidian_fetch
|
49
|
+
changelog_uri: https://github.com/soukouki/obsidian_fetch/blob/main/CHANGELOG.md
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: 3.1.0
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
requirements: []
|
65
|
+
rubygems_version: 3.5.22
|
66
|
+
signing_key:
|
67
|
+
specification_version: 4
|
68
|
+
summary: MCP servers specialising in retrieving information from Obsidian vaults.
|
69
|
+
test_files: []
|