Python API
To use markdown-code-runner, simply import the update_markdown_file function from the package and call it with the path to your Markdown file:
from markdown_code_runner import update_markdown_file
update_markdown_file("path/to/your/markdown_file.md")
Function Reference
update_markdown_file
The main function for processing Markdown files.
def update_markdown_file(
input_filepath: Path | str,
output_filepath: Path | str | None = None,
*,
verbose: bool = False,
backtick_standardize: bool = True,
) -> None:
"""Rewrite a Markdown file by executing and updating code blocks.
Parameters
----------
input_filepath : Path | str
Path to the input Markdown file.
output_filepath : Path | str | None
Path to the output Markdown file. If None, overwrites input file.
verbose : bool
If True, print every line that is processed.
backtick_standardize : bool
If True, clean up markdown-code-runner string from backtick code blocks.
"""
process_markdown
Lower-level function for processing Markdown content as a list of strings.
def process_markdown(
content: list[str],
*,
verbose: bool = False,
backtick_standardize: bool = True,
) -> list[str]:
"""Execute code blocks in a list of Markdown-formatted strings.
Parameters
----------
content
A list of Markdown-formatted strings.
verbose
If True, print every line that is processed.
backtick_standardize
If True, clean up markdown-code-runner string from backtick code blocks.
Returns
-------
list[str]
A modified list of Markdown-formatted strings with code block output inserted.
"""
Examples
Basic Usage
from markdown_code_runner import update_markdown_file
# Update a Markdown file in-place
update_markdown_file("README.md")
# Write to a different file
update_markdown_file("README.md", "README_updated.md")
# Enable verbose output
update_markdown_file("README.md", verbose=True)
Processing Content Directly
from markdown_code_runner import process_markdown
content = [
"<!-- CODE:START -->",
"<!-- print('Hello, world!') -->",
"<!-- CODE:END -->",
"<!-- OUTPUT:START -->",
"This will be replaced.",
"<!-- OUTPUT:END -->",
]
result = process_markdown(content)
print("\n".join(result))