Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dealing with newlines in output of macros #240

Closed
timvink opened this issue Sep 13, 2024 · 3 comments
Closed

Dealing with newlines in output of macros #240

timvink opened this issue Sep 13, 2024 · 3 comments
Labels
documentation Documentation is required external Concerns another part of the ecosystem fixed A fix has been submitted

Comments

@timvink
Copy link
Contributor

timvink commented Sep 13, 2024

Consider the following example:

# main.py
def define_env(env):
    """
    This is the hook for the functions (new form)
    """

    @env.macro
    def say_hello():
        return "hello\nthere\nworld!"
# mkdocs.yml
site_name: Example
theme: mkdocs

nav:
    - Home: index.md

plugins:
  - search
  - macros

markdown_extensions:
  - admonition
  - pymdownx.details
  - pymdownx.superfences  

And

# Homepage

{{ say_hello() }}

Now inside an admonition

!!! note

    I just want to say:

    {{ say_hello() }}

This renders as:

image

Same in material theme:

image

The reason is that the newlines (\n) do not keep the indentation of the first line, so the admonition breaks (the same applies to things like content tabs). I explained that in the table-reader how to page here https://timvink.github.io/mkdocs-table-reader-plugin/howto/use_jinja2/. The solution I use, to add back the idendation to new lines, is hacky.

Ideally, mkdocs-macros would detect if a macros is placed on a page with any indentation, and ensure the same level of indentation is used across all text output. I do the same in table-reader when I use regex replacements.

I am not sure if it is possible though, if jinja is aware of the original indentation. I know you've gone deep into jinja, maybe we can make this work automatically.

Some interesting links:

@fralau
Copy link
Owner

fralau commented Sep 14, 2024

Indeed, as far as I know, a jinja2 block is not aware of its own position within the page.

There is not much we can do about it, except follow the doctrine for this case; and the best workaround so far (it worked for me) is to use the indent filter to manually correct the issue:

# Homepage

{{ say_hello() }}

Now inside an admonition

!!! note

    I just want to say:

    {{ say_hello() | indent(4) }}
Capture d’écran 2024-09-14 à 09 37 10

As mentioned in the jinja2 issue pallets/jinja#178, there would be a strong case for maintaining indentation, typical for languages like YAML. But until the community agrees on a solution, would that work?

@fralau fralau added the external Concerns another part of the ecosystem label Sep 14, 2024
@timvink
Copy link
Contributor Author

timvink commented Sep 14, 2024

It will have to do for now :)

Perhaps you would want to document this on the macros documentation?

@fralau fralau added the documentation Documentation is required label Sep 14, 2024
@fralau fralau added the fixed A fix has been submitted label Sep 15, 2024
@fralau
Copy link
Owner

fralau commented Sep 15, 2024

Documented under "Tips and Tricks".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Documentation is required external Concerns another part of the ecosystem fixed A fix has been submitted
Projects
None yet
Development

No branches or pull requests

2 participants