Skip to content

Commit

Permalink
Store the original path of all media files
Browse files Browse the repository at this point in the history
  • Loading branch information
MatMoore committed May 9, 2024
1 parent 9517bda commit 0d9fa24
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
9 changes: 8 additions & 1 deletion lib/obsidian/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:)
Expand All @@ -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
Expand Down
14 changes: 9 additions & 5 deletions lib/obsidian/parser/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?
Expand Down Expand Up @@ -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?
Expand All @@ -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(
Expand All @@ -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

Expand Down Expand Up @@ -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

0 comments on commit 0d9fa24

Please sign in to comment.