Knowing last heading (tree) from anywhere in the text

No, you are mistaken. Each link is between two headings and the list of links is not generated at run time, but when like all other pages, when the site is built.

I could do it in better languages but that one just makes your head hurt… Here’s an exemple:

## title 1
### subtitle 1
[link1](www.yahoo.fr "name of website 1")
sdfsdfds
[link2](www.google.fr "name of website 2")
### subtitle 2
sdfsdfsd
### subtitle 3
[link3](aol.fr "name of website 3")
## title 2
### subtitle 1
dsfsdf
### subtitle 2
[link4](www.doi.org "name of website 4")

That would translate into:

I. Title 1
	1. subtitle 1
	name of website 1: www.yahoo.fr
	name of website 2: www.google.fr
	3. subtitle 3
	name of website 3: aol.fr
II. Title 2
	2. subtitle 2
	name of website 4: www.doi.org