Skip to content

Commit c1029d5

Browse files
author
Laurent Franceschetti
committed
Document how to correct issue with macros / admonitions (#240)
1 parent 67ccb15 commit c1029d5

File tree

4 files changed

+138
-2
lines changed

4 files changed

+138
-2
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# Initialization
1313
# --------------------
1414

15-
VERSION_NUMBER = '1.1.2'
15+
VERSION_NUMBER = '1.1.3'
1616

1717
# required if you want to run document/test
1818
# pip install 'mkdocs-macros-plugin[test]'

webdoc/docs/admonition.png

27.7 KB
Loading

webdoc/docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ in the same directory as the config file:
112112
```Python
113113
def define_env(env):
114114
"""
115-
This is the hook for the functions (new form)
115+
This is the hook for the variables, macros and filters.
116116
"""
117117
118118
@env.macro

webdoc/docs/tips.md

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,142 @@ In your markdown page, add the call:
276276
Restart the mkdocs server (or rebuild the website) and _voilà_,
277277
you have the first five lines of your file!
278278

279+
Macros do not respect indentation (they break admonitions)!
280+
-----------------------------------------------------------
281+
282+
### Issue
283+
284+
When a macro producing text with newlines is rendered in Markdown,
285+
the additional lines will start from the first column.
286+
287+
It often doesn't matter, but there are cases where **indentation**
288+
is a part of the Markdown syntax.
289+
290+
This is particularly true with the [Admonition](https://python-markdown.github.io/extensions/admonition/) syntax extension, used for defining notes, warnings, etc.
291+
(see also the [description in the documentation for Material for MkDocs](https://squidfunk.github.io/mkdocs-material/reference/admonitions/)).
292+
293+
294+
In order to be considered part of an admonition,
295+
all the text needs to be indented with four columns:
296+
297+
```markdown
298+
!!! note
299+
300+
I just want to say:
301+
302+
hello
303+
there
304+
world
305+
```
306+
307+
It will be rendered as:
308+
!!! note
309+
310+
I just want to say:
311+
312+
hello
313+
there
314+
world
315+
316+
317+
Now consider the following macro, which defines the `say_hello()` macro:
318+
319+
```python
320+
# main.py
321+
def define_env(env):
322+
"""
323+
Hook for macros
324+
"""
325+
326+
@env.macro
327+
def say_hello():
328+
return "hello\nthere\nworld!"
329+
```
330+
331+
### Incorrect
332+
333+
If you have a page like this one, which defines a note:
334+
335+
```markdown
336+
# Homepage
337+
338+
{{ say_hello() }}
339+
340+
Now inside an admonition
341+
342+
!!! note
343+
344+
I just want to say:
345+
346+
{{ say_hello() }}
347+
```
348+
349+
The result of the macros' expansion will be:
350+
351+
```markdown
352+
# Homepage
353+
354+
hello
355+
there
356+
world
357+
358+
Now inside an admonition
359+
360+
!!! note
361+
362+
I just want to say:
363+
364+
hello
365+
there
366+
world
367+
```
368+
369+
The result of the rendering to HTML in the first case will work.
370+
However, the rendering of the call in the admonition will not be what you expect:
371+
372+
![](admonition.png)
373+
374+
The reason is that the Admonition syntax requires an **indentation**
375+
of 4 characters, for `there\nworld`n to be considered part of the note.
376+
377+
### Correct
378+
379+
This can be solved by using the standard [`indent()`](https://jinja.palletsprojects.com/en/3.1.x/templates/#jinja-filters.indent) filter provided by
380+
Jinja2, giving the number of columns as an argument:
381+
382+
```markdown
383+
!!! note
384+
385+
I just want to say:
386+
387+
{{ say_hello() | indent(4) }}
388+
```
389+
390+
The result of the macros' rendering into Markdown will be:
391+
392+
```markdown
393+
!!! note
394+
395+
I just want to say:
396+
397+
hello
398+
there
399+
world
400+
```
401+
402+
And the conversion of Markdown to HTML:
403+
!!! note
404+
405+
I just want to say:
406+
407+
hello
408+
there
409+
world
410+
411+
The admonition now appears correctly.
412+
413+
414+
279415

280416
How can I discover all attributes and methods of the `env` object?
281417
------------------------------------------------------------------

0 commit comments

Comments
 (0)