Skip to content

Commit

Permalink
FIX: Not invalidating citation blocks with 0 citations (#290)
Browse files Browse the repository at this point in the history
* test for links

* fix accidentally making cite blocks with no citations

* add integration test just in case
  • Loading branch information
shyamd authored Jan 21, 2025
1 parent fddf1c2 commit 26da333
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
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

0 comments on commit 26da333

Please sign in to comment.