Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,13 @@ markdown_extensions:

</td></tr></table>

#### Supporting links produced by Obsidina.md

[Obsidian.md](https://obsidian.md) creates links to other markdown files by using URL quotation so instead `[Link to a file](link to a file.md)` we will see `[Link to a file](link%20to%20a%20file.md)`. If we remove URL quotation manually from a link, then Obsidian.md is not able to support this link and basic functionalities like link jumping and graph display are not working. Since mkdocs can be used as a presentation layer, then a valid place to remove URL quotations is to do it just before parsing markdown files and producing static documentation. To remove URL quotation from links, use this for **mkdocs.yaml**:

```yaml
unquote_links: true
```


[mkdocs-nav]: https://www.mkdocs.org/user-guide/writing-your-docs/#configure-pages-and-navigation
Expand Down
5 changes: 5 additions & 0 deletions mkdocs_literate_nav/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ def __init__(
get_nav_for_dir: Callable[[str], Optional[Tuple[str, str]]],
globber,
implicit_index: bool = False,
unquote_links: bool = False,
):
self.get_nav_for_dir = get_nav_for_dir
self.globber = globber
self.implicit_index = implicit_index
self.unquote_links = unquote_links
self.seen_items = set()
self._warn = functools.lru_cache()(log.warning)

Expand Down Expand Up @@ -99,6 +101,9 @@ def _list_element_to_nav(
if error:
raise LiterateNavParseError(error, item)

if self.unquote_links:
out_item = urllib.parse.unquote(out_item)

if type(out_item) in (str, list, DirectoryWildcard) and out_title is not None:
out_item = {out_title: out_item}
result.append(out_item)
Expand Down
10 changes: 8 additions & 2 deletions mkdocs_literate_nav/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class LiterateNavPlugin(mkdocs.plugins.BasePlugin):
config_scheme = (
("nav_file", mkdocs.config.config_options.Type(str, default="SUMMARY.md")),
("implicit_index", mkdocs.config.config_options.Type(bool, default=False)),
("unquote_links", mkdocs.config.config_options.Type(bool, default=False)),
)

def on_files(self, files: mkdocs.structure.files.Files, config: mkdocs.config.Config):
Expand All @@ -36,6 +37,7 @@ def on_files(self, files: mkdocs.structure.files.Files, config: mkdocs.config.Co
files,
nav_file_name=self.config["nav_file"],
implicit_index=self.config["implicit_index"],
unquote_links=self.config["unquote_links"]
)
self._files = files

Expand All @@ -53,7 +55,10 @@ def on_nav(


def resolve_directories_in_nav(
nav_data, files: mkdocs.structure.files.Files, nav_file_name: str, implicit_index: bool
nav_data, files: mkdocs.structure.files.Files,
nav_file_name: str,
implicit_index: bool,
unquote_links: bool
):
"""Walk through a standard MkDocs nav config and replace `directory/` references.

Expand All @@ -77,7 +82,8 @@ def get_nav_for_dir(path: str) -> Optional[Tuple[str, str]]:
return nav_file_name, f.read()

globber = MkDocsGlobber(files)
nav_parser = parser.NavParser(get_nav_for_dir, globber, implicit_index=implicit_index)
nav_parser = parser.NavParser(get_nav_for_dir, globber, implicit_index=implicit_index,
unquote_links=unquote_links)

result = None
if not nav_data or get_nav_for_dir("."):
Expand Down
14 changes: 14 additions & 0 deletions tests/nav/test_unquote.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
files:
index.md:
SUMMARY.md: |
* [Bar with quote](bar%20with%20quote.md)
* [Baz with space](baz with space.md)
* [Foo-without-space](foo-without-space.md)
bar with quote.md:
baz with space.md:
foo-without-space.md:
unquote_links: true
output:
- Bar with quote: bar with quote.md
- Baz with space: baz with space.md
- Foo-without-space: foo-without-space.md
1 change: 1 addition & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def test_nav(tmp_path_factory, golden):
files,
nav_file_name=golden.get("nav_file_name") or "SUMMARY.md",
implicit_index=golden.get("implicit_index"),
unquote_links=golden.get("unquote_links"),
)
assert output == golden.out.get("output")

Expand Down