An MkDocs plugin that enables managing citations with BibTex
—
Core MkDocs plugin functionality that integrates BibTeX citation management into the MkDocs build process. The plugin implements the MkDocs plugin lifecycle hooks to process citations during site generation.
Main plugin class that inherits from MkDocs BasePlugin and implements the BibTeX citation processing functionality.
class BibTexPlugin(BasePlugin[BibTexConfig]):
"""
Allows the use of bibtex in markdown content for MKDocs.
"""
def __init__(self):
"""Initialize the plugin with empty state."""
def on_startup(self, *, command, dirty):
"""
Having on_startup() tells mkdocs to keep the plugin object upon rebuilds.
Args:
command: The MkDocs command being run
dirty: Whether this is a dirty rebuild
"""
def on_config(self, config):
"""
Loads bibliography on load of config.
Args:
config: MkDocs configuration object
Returns:
Updated config object
Raises:
ConfigurationError: If bibliography files are not configured properly
"""
def on_page_markdown(self, markdown, page, config, files):
"""
Parses the markdown for each page, extracting the bibtex references.
Process:
1. Finds all cite keys (may include multiple citation references)
2. Convert all cite keys to citation objects
3. Insert formatted cite keys into text
4. Insert the bibliography into the markdown
5. Insert the full bibliography into the markdown
6. Process inline references if enabled
Args:
markdown (str): The raw markdown content
page: MkDocs page object
config: MkDocs configuration object
files: MkDocs files collection
Returns:
str: Processed markdown content with citations replaced
"""The plugin maintains state across the build process through several key attributes.
# Plugin instance attributes
bib_data: Optional[BibliographyData] # Parsed bibliography data
all_references: OrderedDict # All references collected during build
last_configured: Optional[float] # Timestamp of last configuration
registry: Optional[Union[SimpleRegistry, PandocRegistry]] # Reference formatting registry
csl_file: Optional[str] # Path to CSL style filefrom mkdocs_bibtex.plugin import BibTexPlugin
# Create plugin instance
plugin = BibTexPlugin()
# Load configuration
plugin.load_config(options={
"bib_file": "references.bib",
"footnote_format": "{key}"
})
# Initialize with MkDocs config
plugin.on_config(mkdocs_config)# Input markdown with citations
markdown_input = '''
# My Document
This references [@smith2020; @jones2019].
\bibliography
'''
# Process through plugin
processed_markdown = plugin.on_page_markdown(
markdown_input,
page_object,
config_object,
files_object
)
# Result includes formatted citations and bibliography
print(processed_markdown)plugin = BibTexPlugin()
plugin.load_config(options={
"bib_file": "https://api.zotero.org/groups/12345/items?format=bibtex",
"csl_file": "https://raw.githubusercontent.com/citation-style-language/styles/master/apa.csl",
"footnote_format": "{key}"
})The plugin handles various error conditions during processing:
ConfigurationError if no bib_file or bib_dir is configuredConfigurationError if footnote_format doesn't contain {key} placeholderThe plugin integrates with MkDocs at three key points:
on_startup): Ensures plugin persistence across rebuildson_config): Loads and validates bibliography dataon_page_markdown): Processes citations in each markdown fileThe plugin handles various file sources:
mkdocs.yml).bib file discovery)Install with Tessl CLI
npx tessl i tessl/pypi-mkdocs-bibtex