You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
defdefine_env(env):
322
+
"""
323
+
Hook for macros
324
+
"""
325
+
326
+
@env.macro
327
+
defsay_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
+

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
+
279
415
280
416
How can I discover all attributes and methods of the `env` object?
0 commit comments