Skip to content

Commit 8cf133e

Browse files
committed
Add mechanism to mark and prune subtrees
This enables you to trim down the vault after parsing the content, e.g. you can filter out anything without a specific header, or that isn't linked to by another page
1 parent c6dbae8 commit 8cf133e

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,46 @@
11
## [Unreleased]
2+
23
- Added support for wikilinks that embed files. These are rendered as images or links in the HTML content.
34
- Added method `#page.generate_html` to replace `#page.content.generate_html`, and removed the `MarkdownDocument` class. `#page.content` is now a callable that returns the markdown content.
5+
- Added `#mark_referenced` and `#referenced?` to page objects
6+
- Added `#prune!` method to page objects, to remove non-referenced pages
47

58
## [0.7.0] - 2023-08-03
9+
610
- Fix wikilinks pointing to slugs with spaces not rendering properly.
711
- Links created from wikilinks now include a leading slash
812

913
## [0.6.1] - 2023-08-03
14+
1015
- Prevent `HtmlRenderer` state being shared across documents
1116

1217
## [0.6.0] - 2023-08-03
18+
1319
- Replace Kramdown with Markly
1420
- Enabled support for Github Flavored Markdown tables and tasklists
1521
- Rename `MarkdownContent` -> `MarkdownDocument`, `ObsidianFlavoredMarkdown` -> `MarkdownParser`
1622

1723
## [0.5.4] - 2023-08-02
24+
1825
- Fix page getting clobbered when wikilinks point to non-existent pages.
1926
- Expand `[[foo/index]]` wiklinks to `[foo](foo)`.
2027

2128
## [0.5.3] - 2023-08-01
29+
2230
- Support non-fully qualified titles when parsing wikilink syntax.
2331
- Autolink raw URLs.
2432

2533
## [0.5.2] - 2023-07-30
34+
2635
- Fix handling of `index.md` at the root level.
2736

2837
## [0.5.0] - 2023-07-30
38+
2939
- Fix ordering of `Page#children` so that index pages come first.
3040
- Fix handling of `index.md` documents so that the slug reflects the directory path.
3141

3242
## [0.4.0] - 2023-07-30
43+
3344
- Unify `Note` and `Index` classes into `Page`. This is a breaking API change. `Parser#notes is replaced by Parse#pages`. Call `Page#is_index?`to distinguish between directory derived pages and documents.
3445
- Remove `Parser#table_of_contents` and `Parser#walk_tree`.
3546
- Add `Page#find_in_tree` to recursively search for a page with a matching slug.

lib/obsidian/parser/page.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def initialize(title:, slug:, last_modified: nil, content: nil, parent: nil, con
3232
@content_type = content_type
3333
@media_root = media_root
3434
@source_path = source_path
35+
@referenced = false
3536
end
3637

3738
def is_index?
@@ -163,6 +164,29 @@ def generate_html(markdown_parser: MarkdownParser.new)
163164
markdown_parser.parse(content.call, root: root, media_root: media_root).to_html
164165
end
165166

167+
def referenced?
168+
@referenced
169+
end
170+
171+
# Mark the tree containing this page as being "referenced"
172+
# i.e. reachable through links
173+
def mark_referenced
174+
@referenced = true
175+
parent&.mark_referenced
176+
end
177+
178+
# Remove any child paths that are unreferenced,
179+
# i.e. not reachable through links
180+
def prune!
181+
@children = @children.delete_if do |k, v|
182+
!v.referenced?
183+
end
184+
185+
@children.values.each do |page|
186+
page.prune!
187+
end
188+
end
189+
166190
attr_reader :title
167191
attr_reader :slug
168192
attr_reader :last_modified

spec/obsidian/parser/page_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,41 @@
9999
expect(root.find_in_tree("foo/index")).to eq(page.parent)
100100
end
101101
end
102+
103+
describe "#referenced?" do
104+
it "is false by default" do
105+
expect(root.referenced?).to eq(false)
106+
end
107+
108+
it "is true after #mark_referenced is called" do
109+
root.mark_referenced
110+
expect(root.referenced?).to eq(true)
111+
end
112+
113+
it "is true after #mark_referenced is called on a child node" do
114+
page = root.add_page("foo/bar")
115+
page.mark_referenced
116+
expect(page.referenced?).to eq(true)
117+
expect(page.parent.referenced?).to eq(true)
118+
expect(root.referenced?).to eq(true)
119+
end
120+
end
121+
122+
describe "#prune!" do
123+
it "does not delete referenced pages" do
124+
root.add_page("foo/bar").mark_referenced
125+
root.prune!
126+
expect(root.find_in_tree("foo/bar")).not_to be_nil
127+
end
128+
129+
it "deletes unreferenced pages" do
130+
root.add_page("foo/bar").mark_referenced
131+
page = root.add_page("foo/baz")
132+
expect(page.referenced?).to eq(false)
133+
root.prune!
134+
expect(root.find_in_tree("foo")).not_to be_nil
135+
expect(root.find_in_tree("foo/bar")).not_to be_nil
136+
expect(root.find_in_tree("foo/baz")).to be_nil
137+
end
138+
end
102139
end

0 commit comments

Comments
 (0)