Include file relative to processed markdown file
neurobin opened this issue · 4 comments
This is a nice idea mentioned at cmacmackin/markdown-include#7
by @kamar535
It would be great if there was an option to include a file relative to the markdown file being processed.
For example if we have the following directory structure.
├── a.md
└── sub
├── b.md
└── c.md
To include c.md from b.md someting simlar to {!./c.md!} could be used. To include a.md from b.md something similar to {!../a.md!} could be used.
recursive_relative_path
can be set to True
to achieve this in latest version.
I have used this with mkdocs and I have included the extension as below in mkdocs.yaml file
- mdx_include:
recursive_relative_path: true
But it does not give the expected behaviour explained in this issue.
I have following structure
├── test
└── sample
├── b.md
└── c.md
I have included the b.md file in the c.md file as below
{!./b.md!}
It gives the following error
ERROR:mdx_include-1.3.1:E: Could not find file: b.md
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/mdx_include/mdx_include.py", line 75, in get_local_content_list
with open(filename, 'r', encoding=encoding) as f:
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/codecs.py", line 905, in open
file = builtins.open(filename, mode, buffering)
FileNotFoundError: [Errno 2] No such file or directory: b.md
Did u try using it without ./
i.e simply {!b.md!}
I ran into the same issue. It seems recursive_relative_path
only works for an indirectly included file. Including a file directly from any of the .md
files in the documentation always uses the base path, no way to use a relative path from that .md
file. Simple example:
├── mkdocs.yml
└── docs
├── index.md
└── b.txt
└── c.txt
mkdocs.yml:
site_name: MkLorum
nav:
- Home: index.md
markdown_extensions:
- mdx_include:
recursive_relative_path: true
index.md:
# This is a test header
{!docs/b.txt!}
{!b.txt!}
b.txt:
B is included
{!c.txt!}
c.txt:
This is from C!
With this example, {!docs/b.txt!}
works from index.md
, but {!b.txt!}
results in a FileNotFoundError "b.txt". Including {!c.txt!}
from b.txt
works as it is an indirect include (included from a file that was itself included).