|
| 1 | +"""Tests for inline tag extraction and rendering.""" |
| 2 | + |
| 3 | +from rockgarden.config import Config, SiteConfig |
| 4 | +from rockgarden.obsidian.inline_tags import ( |
| 5 | + expand_hierarchical_tags, |
| 6 | + extract_inline_tags, |
| 7 | +) |
| 8 | +from rockgarden.output.builder import build_site |
| 9 | + |
| 10 | + |
| 11 | +class TestExtractInlineTags: |
| 12 | + def test_basic_tag(self): |
| 13 | + content, tags = extract_inline_tags("Hello #python world") |
| 14 | + assert tags == ["python"] |
| 15 | + assert "[#python](" in content |
| 16 | + |
| 17 | + def test_multiple_tags(self): |
| 18 | + content, tags = extract_inline_tags("Use #python and #rust") |
| 19 | + assert tags == ["python", "rust"] |
| 20 | + |
| 21 | + def test_nested_tag(self): |
| 22 | + content, tags = extract_inline_tags("Status: #project/active") |
| 23 | + assert tags == ["project/active"] |
| 24 | + assert "[#project/active](" in content |
| 25 | + |
| 26 | + def test_not_in_code_span(self): |
| 27 | + content, tags = extract_inline_tags("Use `#python` for scripting") |
| 28 | + assert tags == [] |
| 29 | + assert "`#python`" in content |
| 30 | + |
| 31 | + def test_not_in_fenced_block(self): |
| 32 | + content, tags = extract_inline_tags("```\n#python\n```") |
| 33 | + assert tags == [] |
| 34 | + |
| 35 | + def test_not_a_heading(self): |
| 36 | + content, tags = extract_inline_tags("# Heading\n\nSome text") |
| 37 | + assert tags == [] |
| 38 | + |
| 39 | + def test_not_a_number(self): |
| 40 | + content, tags = extract_inline_tags("Issue #123 is fixed") |
| 41 | + assert tags == [] |
| 42 | + |
| 43 | + def test_tag_at_start_of_line(self): |
| 44 | + content, tags = extract_inline_tags("#python is great") |
| 45 | + assert tags == ["python"] |
| 46 | + |
| 47 | + def test_tag_with_hyphens(self): |
| 48 | + content, tags = extract_inline_tags("Use #my-tag here") |
| 49 | + assert tags == ["my-tag"] |
| 50 | + |
| 51 | + def test_tag_link_url(self): |
| 52 | + content, tags = extract_inline_tags("Hello #python", base_path="/docs") |
| 53 | + assert "[#python](/docs/tags/python/)" in content |
| 54 | + |
| 55 | + def test_tag_link_no_clean_urls(self): |
| 56 | + content, tags = extract_inline_tags("Hello #python", clean_urls=False) |
| 57 | + assert "[#python](/tags/python.html)" in content |
| 58 | + |
| 59 | + def test_no_tags(self): |
| 60 | + content, tags = extract_inline_tags("No tags here") |
| 61 | + assert tags == [] |
| 62 | + assert content == "No tags here" |
| 63 | + |
| 64 | + def test_tag_after_punctuation(self): |
| 65 | + content, tags = extract_inline_tags("see: #python") |
| 66 | + assert tags == ["python"] |
| 67 | + |
| 68 | + def test_tag_not_inside_word(self): |
| 69 | + content, tags = extract_inline_tags("foo#bar is not a tag") |
| 70 | + assert tags == [] |
| 71 | + |
| 72 | + def test_not_in_anchor_link(self): |
| 73 | + content, tags = extract_inline_tags("[Jump](#introduction)") |
| 74 | + assert tags == [] |
| 75 | + assert content == "[Jump](#introduction)" |
| 76 | + |
| 77 | + def test_not_in_markdown_link_text(self): |
| 78 | + content, tags = extract_inline_tags("[text with #python](http://example.com)") |
| 79 | + assert tags == [] |
| 80 | + |
| 81 | + def test_tag_outside_link_still_works(self): |
| 82 | + content, tags = extract_inline_tags("[link](#anchor) and #python") |
| 83 | + assert tags == ["python"] |
| 84 | + assert "[link](#anchor)" in content |
| 85 | + |
| 86 | + |
| 87 | +class TestExpandHierarchicalTags: |
| 88 | + def test_flat_tags_unchanged(self): |
| 89 | + assert expand_hierarchical_tags(["python", "rust"]) == ["python", "rust"] |
| 90 | + |
| 91 | + def test_nested_expands(self): |
| 92 | + result = expand_hierarchical_tags(["project/active"]) |
| 93 | + assert "project/active" in result |
| 94 | + assert "project" in result |
| 95 | + |
| 96 | + def test_deep_nesting(self): |
| 97 | + result = expand_hierarchical_tags(["a/b/c"]) |
| 98 | + assert set(result) == {"a", "a/b", "a/b/c"} |
| 99 | + |
| 100 | + def test_dedup(self): |
| 101 | + result = expand_hierarchical_tags(["project/a", "project/b"]) |
| 102 | + assert result.count("project") == 1 |
| 103 | + |
| 104 | + def test_empty(self): |
| 105 | + assert expand_hierarchical_tags([]) == [] |
| 106 | + |
| 107 | + |
| 108 | +class TestInlineTagsIntegration: |
| 109 | + def test_inline_tags_merged_with_frontmatter(self, tmp_path): |
| 110 | + source = tmp_path / "content" |
| 111 | + source.mkdir() |
| 112 | + (source / "page.md").write_text( |
| 113 | + "---\ntags: [existing]\n---\n# Hello\n\nSee #python for details.\n" |
| 114 | + ) |
| 115 | + output = tmp_path / "output" |
| 116 | + config = Config(site=SiteConfig(source=source, output=output)) |
| 117 | + build_site(config, source, output) |
| 118 | + # Both tag pages should exist |
| 119 | + assert (output / "tags" / "existing" / "index.html").exists() |
| 120 | + assert (output / "tags" / "python" / "index.html").exists() |
| 121 | + |
| 122 | + def test_hierarchical_tag_pages_generated(self, tmp_path): |
| 123 | + source = tmp_path / "content" |
| 124 | + source.mkdir() |
| 125 | + (source / "page.md").write_text("---\ntags: [project/active]\n---\n# Hello\n") |
| 126 | + output = tmp_path / "output" |
| 127 | + config = Config(site=SiteConfig(source=source, output=output)) |
| 128 | + build_site(config, source, output) |
| 129 | + assert (output / "tags" / "project-active" / "index.html").exists() |
| 130 | + assert (output / "tags" / "project" / "index.html").exists() |
| 131 | + |
| 132 | + def test_inline_tag_renders_as_link(self, tmp_path): |
| 133 | + source = tmp_path / "content" |
| 134 | + source.mkdir() |
| 135 | + (source / "page.md").write_text("# Hello\n\nSee #python for details.\n") |
| 136 | + output = tmp_path / "output" |
| 137 | + config = Config(site=SiteConfig(source=source, output=output)) |
| 138 | + build_site(config, source, output) |
| 139 | + html = (output / "page" / "index.html").read_text() |
| 140 | + assert "/tags/python/" in html |
0 commit comments