Skip to content

Commit

Permalink
Use marcel to detect content types
Browse files Browse the repository at this point in the history
When pages embed content via wikilinks, the behaviour depends on the
content type. For images, we can generate regular image markdown. For
anything else, we generate a link.
  • Loading branch information
MatMoore committed Aug 27, 2023
1 parent cb6ce2f commit 9517bda
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 8 deletions.
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ PATH
remote: .
specs:
obsidian-parser (0.7.0)
marcel (~> 0.3.1)
markly (~> 0.7.0)

GEM
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions lib/obsidian/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ 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\.\/?/, "")

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

Expand Down Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions lib/obsidian/parser/content_type.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions obsidian-parser.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file added spec/example_vault/foo/bar.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 10 additions & 1 deletion spec/obsidian/parser/markdown_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 9517bda

Please sign in to comment.