Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Not invalidating citation blocks with 0 citations #290

Merged
merged 3 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/mkdocs_bibtex/citation.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ def from_markdown(cls, markdown: str) -> List["CitationBlock"]:
citation_blocks = []
for match in CITATION_BLOCK_REGEX.finditer(markdown):
try:
citation_blocks.append(
CitationBlock(raw=match.group(1), citations=Citation.from_markdown(match.group(1)))
)
citations = Citation.from_markdown(match.group(1))
if len(citations) > 0:
citation_blocks.append(CitationBlock(raw=match.group(1), citations=citations))
except Exception as e:
print(f"Error extracting citations from block: {e}")
return citation_blocks
7 changes: 7 additions & 0 deletions test_files/test_citation.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,10 @@ def test_citation_string():
citations = [citation, citation]
block = CitationBlock(citations)
assert str(block) == "[Author @test 2020; Author @test 2020]"


def test_links_not_citations():
"""Test that non-citations are not parsed as citations"""
markdown = "This is [google](www.google.com)."
blocks = CitationBlock.from_markdown(markdown)
assert len(blocks) == 0
9 changes: 9 additions & 0 deletions test_files/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,12 @@ def test_full_bib_command_with_pandoc(pandoc_plugin):
assert "[^test2]: Author F, Author S (2019b)" in result
assert "[^Bivort2016]: De Bivort BL, Van Swinderen B (2016)" in result
assert "[^test_citavi]: Author F, Author S (2019c)" in result


def test_leaving_non_citations(plugin):
"""Test that non-citations are not parsed as citations"""
markdown = "This is not a citation [google](www.google.com). But this is a citation [@test]."
result = plugin.on_page_markdown(markdown, None, None, None)

assert "[^test]" in result
assert "[google](www.google.com)" in result
Loading