Skip to content

Commit 7ae82b2

Browse files
add support for mkdocs-material title mode (#33)
1 parent b85b8bf commit 7ae82b2

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,23 @@ Tabbed fences provide a 'title' for code blocks, and adjacent code blocks will a
3131
```
3232
3333
b. For version 7.x or lower of `pymdown-extensions`, use the following:
34+
3435
```yaml
3536
plugins:
3637
- codeinclude:
3738
title_mode: legacy_pymdownx.superfences
3839
```
3940

40-
c. If no tabbed fences should be used at all:
41+
c. If you are using [`mkdocs-material`](https://squidfunk.github.io/mkdocs-material/reference/code-blocks/#adding-a-title), use the following:
42+
43+
```yaml
44+
plugins:
45+
- codeinclude:
46+
title_mode: mkdocs-material
47+
```
48+
49+
d. If no tabbed fences should be used at all:
50+
4151
```yaml
4252
plugins:
4353
- codeinclude:

codeinclude/plugin.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class CodeIncludePlugin(BasePlugin):
5757
(
5858
"title_mode",
5959
mkdocs.config.config_options.Choice(
60-
choices=["none", "legacy_pymdownx.superfences", "pymdownx.tabbed"],
60+
choices=["none", "legacy_pymdownx.superfences", "pymdownx.tabbed", "mkdocs-material"],
6161
default="pymdownx.tabbed",
6262
),
6363
),
@@ -175,6 +175,15 @@ def get_substitute(self, page, title, filename, lines, block, inside_block):
175175
```
176176
177177
"""
178+
elif (
179+
self.config.get("title_mode") == "mkdocs-material"
180+
):
181+
return f"""
182+
```{header} title="{title}"
183+
{dedented}
184+
```
185+
186+
"""
178187
else:
179188
return f"""
180189
```{header}

tests/codeinclude/test_plugin.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,33 @@ def test_modern_tab_case(self):
297297
result.strip(),
298298
)
299299

300+
def test_mkdocs_material_no_selector(self):
301+
plugin = CodeIncludePlugin()
302+
plugin.load_config({"title_mode": "mkdocs-material"})
303+
result = plugin.on_page_markdown(
304+
MARKDOWN_EXAMPLE_NO_SELECTOR, PAGE_EXAMPLE, dict()
305+
)
306+
307+
print(result)
308+
self.assertEqual(
309+
textwrap.dedent(
310+
"""
311+
# hello world
312+
313+
some text before
314+
315+
```java title=\"foo\"
316+
public class Foo {
317+
318+
}
319+
```
320+
321+
and some text after
322+
"""
323+
).strip(),
324+
result.strip(),
325+
)
326+
300327
def test_empty_title_case(self):
301328
plugin = CodeIncludePlugin()
302329
plugin.load_config({"title_mode": "legacy_pymdownx.superfences"})

0 commit comments

Comments
 (0)