Skip to content

Commit

Permalink
feat: Allow automatic execution of code blocks thanks to the `MARKDOW…
Browse files Browse the repository at this point in the history
…N_EXEC_AUTO` environment variable

Issue-24: #24
  • Loading branch information
pawamoy committed Jun 13, 2024
1 parent ee3fae9 commit 0db27b2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
8 changes: 8 additions & 0 deletions docs/usage/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ print("Hello Markdown!")
The `exec` option will be true for every possible value
except `0`, `no`, `off` and `false` (case insensitive).

To enable automatic execution of code blocks for specific languages
(without having to add the `exec="on" option to your code blocks),
set the `MARKDOWN_EXEC_AUTO` environment variable:

```bash
MARKDOWN_EXEC_AUTO=python,bash
```

## Options summary

As the number of options grew over time,
Expand Down
4 changes: 3 additions & 1 deletion src/markdown_exec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from __future__ import annotations

import os
import re
from typing import TYPE_CHECKING, Any

Expand All @@ -26,6 +27,7 @@

__all__: list[str] = ["formatter", "validator"]

MARKDOWN_EXEC_AUTO = [lang.strip() for lang in os.getenv("MARKDOWN_EXEC_AUTO", "").split(",")]

formatters = {
"bash": _format_bash,
Expand Down Expand Up @@ -63,7 +65,7 @@ def validator(
Returns:
Success or not.
"""
exec_value = _to_bool(inputs.pop("exec", "no"))
exec_value = language in MARKDOWN_EXEC_AUTO or _to_bool(inputs.pop("exec", "no"))
if language not in {"tree", "pyodide"} and not exec_value:
return False
id_value = inputs.pop("id", "")
Expand Down

0 comments on commit 0db27b2

Please sign in to comment.