Skip to content

Commit

Permalink
feat: Provide a MkDocs plugin for easier setup
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed May 1, 2022
1 parent ac4fe06 commit 5fce814
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ Markdown(
)
```

...or in MkDocs configuration file:
...or in MkDocs configuration file, as a Markdown extension:

```yaml
# mkdocs.yml
markdown_extensions:
- pymdownx.superfences:
custom_fences:
Expand All @@ -85,6 +86,15 @@ markdown_extensions:
format: !!python/name:markdown_exec.formatter
```
...or in MkDocs configuration file, as a plugin:
```yaml
# mkdocs.yml
plugins:
- search
- markdown-exec
```
## Usage
You are now able to execute code blocks instead of displaying them:
Expand Down
8 changes: 2 additions & 6 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,7 @@ markdown_extensions:
check_paths: true
base_path: [docs/snippets, "."]
- pymdownx.highlight
- pymdownx.superfences:
custom_fences:
- name: python
class: python
validator: !!python/name:markdown_exec.validator
format: !!python/name:markdown_exec.formatter
- pymdownx.superfences
- pymdownx.tabbed:
alternate_style: true
- pymdownx.tasklist
Expand All @@ -78,6 +73,7 @@ plugins:
nav_file: SUMMARY.md
- coverage
- section-index
- markdown-exec
- mkdocstrings:
handlers:
python:
Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ Discussions = "https://github.com/pawamoy/markdown-exec/discussions"
Gitter = "https://gitter.im/markdown-exec/community"
Funding = "https://github.com/sponsors/pawamoy"

[project.optional-dependencies]
[project.entry-points."mkdocs.plugins"]
markdown-exec = "markdown_exec.mkdocs_plugin:MarkdownExecPlugin"

[tool.pdm]
version = {use_scm = true}
package-dir = "src"
Expand Down
29 changes: 29 additions & 0 deletions src/markdown_exec/mkdocs_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""This module contains an optional plugin for MkDocs."""

from mkdocs.config import Config, config_options
from mkdocs.plugins import BasePlugin

from markdown_exec import formatter, validator


class MarkdownExecPlugin(BasePlugin):
"""MkDocs plugin to easily enable custom fences for code blocks execution."""

config_scheme = (("languages", config_options.Type(list, default=["python"])),)

def on_config(self, config: Config, **kwargs) -> Config: # noqa: D102
self.languages = self.config["languages"]

mdx_configs = config.setdefault("mdx_configs", {})
superfences = mdx_configs.setdefault("pymdownx.superfences", {})
custom_fences = superfences.setdefault("custom_fences", [])
for language in self.languages:
custom_fences.append(
{
"name": language,
"class": language,
"validator": validator,
"format": formatter,
}
)
return config

0 comments on commit 5fce814

Please sign in to comment.