Skip to content

Commit

Permalink
Add method to collapse subdirectories
Browse files Browse the repository at this point in the history
  • Loading branch information
MatMoore committed Jul 18, 2024
1 parent 61d63fe commit 804350a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
### Added

- Added support for wikilinks that embed files. These are rendered as images or links in the HTML content.
- `Vault#mark_referenced` and `Vault#prune!` - ignore unwanted files
- `Vault#collapse!` - if a subdirectory contains only one file, treat it as if the file were added to the parent

### Removed

Expand Down
6 changes: 5 additions & 1 deletion lib/obsidian/parser/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def initialize(value, parent: nil, order_by: nil)
@order_by = order_by
end

attr_reader :value
attr_accessor :value
attr_reader :parent

def children
Expand All @@ -32,6 +32,10 @@ def inspect

def add_child(key, value)
node = Tree.new(value, parent: self)
add_child_node(key, node)
end

def add_child_node(key, node)
@children[key] = node
end

Expand Down
33 changes: 33 additions & 0 deletions lib/obsidian/parser/vault.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,40 @@ def prune!
end
end

# Replace any single-child nodes with their child
def collapse!
collapse_tree!(@tree)
end

attr_reader :root
attr_reader :media_root

private

def collapse_tree!(tree)
if tree.children.size == 1 && !tree.parent.nil?
slug = tree.value.slug
child = tree.children.first
child_slug = child.value.slug
parent = tree.parent

parts = child_slug.split("/")
last_part = parts.pop
parts.pop
parts << last_part

new_child_slug = parts.join("/")
child.value.slug = new_child_slug

parent.add_child_node(new_child_slug, child)
parent.remove_child(slug)

collapse_tree!(child)
else
tree.children.each do |child|
collapse_tree!(child)
end
end
end
end
end
27 changes: 27 additions & 0 deletions spec/obsidian/parser/vault_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,31 @@
expect(vault.find_in_tree("foo/baz")).to be_nil
end
end

describe("#collapse") do
it "preserves subtrees with more than one child" do
vault.add_page("foo/bar")
vault.add_page("foo/baz")
vault.add_page("foo/bar/a")
vault.add_page("foo/bar/b")

vault.collapse!

expect(vault.find_in_tree("foo/bar/a")).not_to be_nil
expect(vault.find_in_tree("foo/bar/b")).not_to be_nil
expect(vault.find_in_tree("foo/baz")).not_to be_nil
end

it "collapses subtrees with one child" do
vault.add_page("foo/bar")
vault.add_page("foo/baz")
vault.add_page("foo/bar/a")

vault.collapse!

expect(vault.find_in_tree("foo/baz")).not_to be_nil
expect(vault.find_in_tree("foo/bar/a")).to be_nil
expect(vault.find_in_tree("foo/a")).not_to be_nil
end
end
end

0 comments on commit 804350a

Please sign in to comment.