Skip to content

Commit

Permalink
Add parent attribute to Note objects
Browse files Browse the repository at this point in the history
  • Loading branch information
MatMoore committed Jul 27, 2023
1 parent ffe94f8 commit 9f0a869
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## [Unreleased]

## [0.3.0] - 2023-07-27

- Note objects have a `parent` attribute.

## [0.2.0] - 2023-07-24

- Note objects have a `content` attribute. Call `content.generate_html` to generate HTML on demand.
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
obsidian-parser (0.2.0)
obsidian-parser (0.3.0)
kramdown (~> 2.4)
kramdown-parser-gfm (~> 1.1)

Expand Down
8 changes: 6 additions & 2 deletions lib/obsidian/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ def generate_html
end

class Note
def initialize(title, slug, last_modified, content: nil)
def initialize(title, slug, last_modified, content: nil, parent: nil)
# TODO: check frontmatter for titles as well
@title = title
@slug = slug
@last_modified = last_modified
@content = content
@parent = parent
end

def inspect
Expand All @@ -39,6 +40,7 @@ def inspect
attr_reader :slug
attr_reader :last_modified
attr_reader :content
attr_reader :parent
end

class Index
Expand All @@ -58,9 +60,11 @@ def add_directory(title)
def add_note(title, parent_slug, last_modified, content: nil)
slug = Obsidian.build_slug(title, parent_slug)
directory = nested_directory(parent_slug.split("/"))
note = Note.new(title, slug, last_modified, content: content)
note = Note.new(title, slug, last_modified, content: content, parent: directory)

directory.notes << note

note
end

def inspect
Expand Down
2 changes: 1 addition & 1 deletion lib/obsidian/parser/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Obsidian
class Parser
VERSION = "0.2.0"
VERSION = "0.3.0"
end
end
16 changes: 16 additions & 0 deletions spec/obsidian/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,20 @@
it "converts markdown into HTML content" do
expect(parser.notes.find { |note| note.title == "cat" }.content.generate_html).to eq("<h2 id=\"cats-are-the-best\">Cats are the best</h2>\n\n<p>Meow meow meow</p>\n")
end

describe(Obsidian::Note) do
it "links to its parent" do
index = Obsidian::Index.new("", "")
note = index.add_note("slug", "grandparent/parent", Time.now, content: "")

expect(note.parent.slug).to eq("grandparent/parent")
end

it "links to the root if there is no parent" do
index = Obsidian::Index.new("", "")
note = index.add_note("slug", "", Time.now, content: "")

expect(note.parent.slug).to eq("")
end
end
end

0 comments on commit 9f0a869

Please sign in to comment.