-
Hey. Wanted to use this plugin for consecutive numbering of images across markdown pages. I defined a counter variable in my extra section in my mkdocs.yml and assigned 0 to it. I then created a function increment:
It works correctly on the first page I used this macro on, but on the second page it repeated the increments from the first page and then added the increments from the second page on top. Any ideas what could be the problem, or what i should do instead ? Something else i tried was assigning local variables in every markdown file and importing it in the following, so I could keep incrementing on top of it.
Hopefully my explanations make sense and leave a hint what I could do to improve it :) |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 2 replies
-
Welcome to this project and thank you!' first issue |
Beta Was this translation helpful? Give feedback.
-
@maxhenze This makes sense. The module provides global values for the pages, so if you have a counter managed centrally, every page is going to increase it. Off my hat, I would do it this way:
Then
Voilà (let me know if that works) For your next question: importing a page into another page can be dodgy, because you could precisely hit a recursive loop. I wouldn't try that, if I could avoid it. |
Beta Was this translation helpful? Give feedback.
-
The I have a solution that works, with a minimal example. The trick is to reset the counter for every page (or when you want). Since the building of the pages is sequential (not parallel), page by page, this is going to work: def define_env(env):
"""
This is the hook for defining variables, macros and filters
"""
counter = 0
@env.macro
def set_counter(value:int=0):
nonlocal counter
counter = value
return ''
@env.macro
def increment():
nonlocal counter
counter += 1
return counter Example of page:
# Example of counter
## First
Initialize {{ set_counter ()}}
First: {{ increment() }}
Second: {{ increment() }}
Third: {{ increment() }}
## Second
Initialize {{ set_counter ()}}
First: {{ increment() }}
Second: {{ increment() }}
Third: {{ increment() }} This is tested and works. |
Beta Was this translation helpful? Give feedback.
-
It is funny that what we would call global variable in the perspective of mkdocs-macros, we would need to define as nonlocal in the module. This is just a question of perspective: since we must define the main procedure as Unless you want to do like this, with a real global value: counter = 0
def define_env(env):
"""
This is the hook for defining variables, macros and filters
"""
@env.macro
def set_counter(value:int=0):
global counter
counter = value
return ''
@env.macro
def increment():
global counter
counter += 1
return counter |
Beta Was this translation helpful? Give feedback.
-
Something else I found, while working with your solution is that the counter gets incremented according to the order of the markdown files and not to the order specified in the For example: If I have the documents:
in my folder, but in my
and in each of them I increase the But intentionally I wanted to have: |
Beta Was this translation helpful? Give feedback.
The
counter
initial value is of course, read from the YAML file and is used to initializecounter
variable, but it is not "pushed" back to it.I have a solution that works, with a minimal example. The trick is to reset the counter for every page (or when you want). Since the building of the pages is sequential (not parallel), page by page, this is going to work: