diff --git a/Gemfile.lock b/Gemfile.lock index bd32562..e273be7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,6 +2,7 @@ PATH remote: . specs: obsidian-parser (0.7.0) + marcel (~> 0.3.1) markly (~> 0.7.0) GEM @@ -13,8 +14,17 @@ GEM json (2.6.3) language_server-protocol (3.17.0.3) lint_roller (1.1.0) + marcel (0.3.3) + mimemagic (~> 0.3.2) markly (0.7.0) method_source (1.0.0) + mimemagic (0.3.10) + nokogiri (~> 1) + rake + nokogiri (1.15.4-x86_64-darwin) + racc (~> 1.4) + nokogiri (1.15.4-x86_64-linux) + racc (~> 1.4) parallel (1.23.0) parser (3.2.2.3) ast (~> 2.4.1) diff --git a/lib/obsidian/parser.rb b/lib/obsidian/parser.rb index d99ec9e..0933a2d 100644 --- a/lib/obsidian/parser.rb +++ b/lib/obsidian/parser.rb @@ -26,9 +26,9 @@ def initialize(vault_directory) markdown_parser = MarkdownParser.new vault_directory.glob("**/*").each do |path| - dirname, basename = path.relative_path_from(vault_directory).split + next if path.directory? - next if basename == "." + dirname, basename = path.relative_path_from(vault_directory).split # Remove the path component "./" from the start of the dirname parent_slug = dirname.to_s.gsub(/\A\.\/?/, "") @@ -36,7 +36,7 @@ def initialize(vault_directory) if basename.to_s.end_with?(".md") add_markdown_file(basename: basename, parent_slug: parent_slug, path: path, last_modified: path.mtime, markdown_parser: markdown_parser) else - add_media_file(basename: basename, parent_slug: parent_slug, last_modified: path.mtime) + add_media_file(basename: basename, parent_slug: parent_slug, last_modified: path.mtime, path: path) end end @@ -67,11 +67,11 @@ def add_markdown_file(basename:, parent_slug:, last_modified:, path:, markdown_p ) end - def add_media_file(basename:, parent_slug:, last_modified:) + def add_media_file(basename:, parent_slug:, last_modified:, path:) @media_index.add_page( Obsidian.build_slug(basename.to_s, parent_slug), last_modified: last_modified, - content_type: PretendEverythingIsAnImage.new + content_type: ContentType.new(path) ) end end diff --git a/lib/obsidian/parser/content_type.rb b/lib/obsidian/parser/content_type.rb index 8f2c777..5ef0e7f 100644 --- a/lib/obsidian/parser/content_type.rb +++ b/lib/obsidian/parser/content_type.rb @@ -1,5 +1,13 @@ -class PretendEverythingIsAnImage +require "marcel" + +class ContentType + def initialize(path) + @content_type = Marcel::MimeType.for(path) + end + def image? - true + content_type.start_with?("image/") end + + attr_reader :content_type end diff --git a/obsidian-parser.gemspec b/obsidian-parser.gemspec index 06872f8..5c3687d 100644 --- a/obsidian-parser.gemspec +++ b/obsidian-parser.gemspec @@ -31,6 +31,7 @@ Gem::Specification.new do |spec| spec.require_paths = ["lib"] spec.add_dependency "markly", "~> 0.7.0" + spec.add_dependency "marcel", "~> 0.3.1" # For more information and examples about making a new gem, check out our # guide at: https://bundler.io/guides/creating_gem.html diff --git a/spec/example_vault/foo/bar.jpg b/spec/example_vault/foo/bar.jpg new file mode 100644 index 0000000..5454715 Binary files /dev/null and b/spec/example_vault/foo/bar.jpg differ diff --git a/spec/obsidian/parser/markdown_parser_spec.rb b/spec/obsidian/parser/markdown_parser_spec.rb index 40f16cf..fde6aba 100644 --- a/spec/obsidian/parser/markdown_parser_spec.rb +++ b/spec/obsidian/parser/markdown_parser_spec.rb @@ -66,7 +66,11 @@ let(:media_root) { Obsidian::Page.create_root } before do - media_root.add_page("foo/bar.jpg", content_type: PretendEverythingIsAnImage.new) + path = Pathname.new(__dir__).join("../../example_vault/foo/bar.jpg") + media_root.add_page("foo/bar.jpg", content_type: ContentType.new(path)) + + path = Pathname.new(__dir__).join("../../example_vault/hello_world.txt") + media_root.add_page("hello_world.txt", content_type: ContentType.new(path)) end it "expands image wikilinks" do @@ -95,6 +99,11 @@ expect(result).to eq("[bar](/foo/bar)") end + it "falls back to a link if the target is not an image" do + result = parser.expand_attachments("![[hello_world.txt]]", root: index, media_root: media_root) + expect(result).to eq("[hello_world.txt](/hello_world.txt)") + end + it "falls back to plain text if there is no such page" do result = parser.expand_attachments("![[banana|a yellow banana]]", root: index, media_root: media_root) expect(result).to eq("a yellow banana")