Skip to content

Commit

Permalink
Give tree children names
Browse files Browse the repository at this point in the history
This means the get_or_create can work based on name, rather than
value.

This fixed duplicate pages appearing.
  • Loading branch information
MatMoore committed Jun 28, 2024
1 parent 47a88b8 commit cb9d6b5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/obsidian/parser/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def get_or_create_child(title:, slug:, last_modified: nil, content: nil, content
source_path: source_path
)

child = @tree.add_child_unless_exists(value)
child = @tree.add_child_unless_exists(value.slug, value)
@content_store[slug] = content unless content.nil?
page = Page.new(child, @content_store, parent: self, root: root, media_root: media_root)
@child_pages[slug] ||= page
Expand Down
36 changes: 19 additions & 17 deletions lib/obsidian/parser/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,44 @@ module Obsidian
class Tree
def initialize(value)
@value = value
@children = []
@children = {}
end

attr_reader :children
attr_reader :value

def children
@children.values
end

def inspect
"Tree(#{value.inspect}) [#{children.length} children]"
end

def add_child(value)
def add_child(key, value)
node = Tree.new(value)
children << node
node
@children[key] = node
end

def get_child(value)
children.find { |child| child.value == value }
def get_child(key)
@children[key]
end

def child_exists(value)
children.any? { |node| node.value == value }
def child_exists(key)
@children.include?(key)
end

def add_child_unless_exists(value)
child = get_child(value)
def add_child_unless_exists(key, value)
child = @children[key]
return child unless child.nil?
add_child(value)
add_child(key, value)
end

def remove_child(value)
children.delete_if { |node| node.value == value }
def remove_child(key)
@children.delete(key)
end

def is_index?
children.length > 0
@children.length > 0
end

def find(&block)
Expand All @@ -59,11 +61,11 @@ def walk(&block)
end

def remove_all(&block)
@children = @children.delete_if do |node|
@children = @children.delete_if do |key, node|
block.call(node.value)
end

@children.each do |child|
children.each do |child|
child.remove_all(&block)
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/obsidian/parser/tree_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

describe("creating, retrieving, and removing nodes") do
before do
foo = root.add_child(:foo)
foo.add_child(:bar)
foo.add_child(:baz)
foo = root.add_child("a", :foo)
foo.add_child("a", :bar)
foo.add_child("b", :baz)
end

it "can find a node from the root" do
Expand All @@ -24,7 +24,7 @@
end

it "can remove nodes" do
root.remove_child(:foo)
root.remove_child("a")
expect(root.children).to be_empty
end

Expand Down

0 comments on commit cb9d6b5

Please sign in to comment.