From 0d9fa240d86c4943c1b444ce7d23c03a2f38c6b2 Mon Sep 17 00:00:00 2001 From: Mat Moore Date: Thu, 9 May 2024 21:18:49 +0100 Subject: [PATCH] Store the original path of all media files --- lib/obsidian/parser.rb | 9 ++++++++- lib/obsidian/parser/page.rb | 14 +++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/obsidian/parser.rb b/lib/obsidian/parser.rb index 0933a2d..09233c9 100644 --- a/lib/obsidian/parser.rb +++ b/lib/obsidian/parser.rb @@ -49,6 +49,12 @@ def pages result end + def media + result = [] + media_index.walk_tree { |page| result << page } + result + end + private def add_markdown_file(basename:, parent_slug:, last_modified:, path:, markdown_parser:) @@ -71,7 +77,8 @@ 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: ContentType.new(path) + content_type: ContentType.new(path), + source_path: path ) end end diff --git a/lib/obsidian/parser/page.rb b/lib/obsidian/parser/page.rb index 40f6d88..7560554 100644 --- a/lib/obsidian/parser/page.rb +++ b/lib/obsidian/parser/page.rb @@ -20,7 +20,7 @@ def self.create_root Page.new(title: "", slug: "") end - def initialize(title:, slug:, last_modified: nil, content: nil, parent: nil, content_type: nil, media_root: nil) + def initialize(title:, slug:, last_modified: nil, content: nil, parent: nil, content_type: nil, media_root: nil, source_path: nil) # TODO: check frontmatter for titles as well @title = title @slug = slug @@ -31,6 +31,7 @@ def initialize(title:, slug:, last_modified: nil, content: nil, parent: nil, con @children = {} @content_type = content_type @media_root = media_root + @source_path = source_path end def is_index? @@ -66,7 +67,7 @@ def hash # Call this method on the root page. # When calling this method, you must ensure that anscestor pages # are added before their descendents. - def add_page(slug, last_modified: nil, content: nil, content_type: nil, media_root: nil) + def add_page(slug, last_modified: nil, content: nil, content_type: nil, media_root: nil, source_path: nil) path_components = slug.split("/") if path_components.empty? @@ -87,13 +88,14 @@ def add_page(slug, last_modified: nil, content: nil, content_type: nil, media_ro last_modified: last_modified, content: content, content_type: content_type, - media_root: media_root + media_root: media_root, + source_path: source_path ).tap do |page| page.update_content(content: content, last_modified: last_modified) end end - def get_or_create_child(title:, slug:, last_modified: nil, content: nil, content_type: nil, media_root: nil) + def get_or_create_child(title:, slug:, last_modified: nil, content: nil, content_type: nil, media_root: nil, source_path: nil) # TODO: validate slug matches the current page slug @children[title] ||= Page.new( @@ -103,7 +105,8 @@ def get_or_create_child(title:, slug:, last_modified: nil, content: nil, content content: content, content_type: content_type, parent: self, - media_root: media_root + media_root: media_root, + source_path: source_path ) end @@ -168,5 +171,6 @@ def generate_html(markdown_parser: MarkdownParser.new) attr_reader :parent attr_reader :root attr_reader :media_root + attr_reader :source_path end end