Skip to content

Commit

Permalink
Change the default value of nav_file to SUMMARY.md, from index.md
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypin committed May 17, 2021
1 parent b8e3228 commit e6fd5b1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 54 deletions.
27 changes: 7 additions & 20 deletions mkdocs_literate_nav/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
import mkdocs.structure.nav
import mkdocs.structure.pages

try:
from mkdocs.exceptions import PluginError
except ImportError:
PluginError = SystemExit

from mkdocs_literate_nav import parser

log = logging.getLogger(f"mkdocs.plugins.{__name__}")
Expand All @@ -21,7 +26,7 @@

class LiterateNavPlugin(mkdocs.plugins.BasePlugin):
config_scheme = (
("nav_file", mkdocs.config.config_options.Type(str, default=None)),
("nav_file", mkdocs.config.config_options.Type(str, default="SUMMARY.md")),
("implicit_index", mkdocs.config.config_options.Type(bool, default=False)),
)

Expand Down Expand Up @@ -60,7 +65,7 @@ def resolve_directories_in_nav(
"""

def read_index_of_dir(path: str) -> Optional[str]:
file = find_index_of_dir(files, path, nav_file_name)
file = files.get_file_from_path(os.path.join(path, nav_file_name))
if not file:
return None
log.debug(f"Navigation for {path!r} based on {file.src_path!r}.")
Expand Down Expand Up @@ -93,24 +98,6 @@ def try_resolve_directory(path: str):
return convert_strings_in_nav(nav_data, try_resolve_directory)


def find_index_of_dir(
files: mkdocs.structure.files.Files, path: str, nav_file_name: Optional[str] = None
) -> mkdocs.structure.files.File:
"""Find the directory's index (or nav if specified) Markdown file.
If `nav_file` is configured, unconditionally get that file from this dir (could be None).
Else try README.md. Else try whatever maps to the index (effectively index.md or readme.md).
"""
if nav_file_name:
return files.get_file_from_path(os.path.join(path, nav_file_name))
f = files.get_file_from_path(os.path.join(path, "README.md"))
if f:
return f
for f in files.documentation_pages():
if os.path.split(f.src_path)[0] == path and f.name == "index":
return f


def convert_strings_in_nav(nav_data, converter: Callable[[str], Optional[Any]]):
"""Walk a nav dict and replace strings in it with the callback."""
if isinstance(nav_data, str):
Expand Down
38 changes: 4 additions & 34 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,14 @@ def test_resolve_directories_in_nav(tmp_path_factory, use_directory_urls, golden

with golden.capture_logs("mkdocs.plugins.mkdocs_literate_nav"):
output = plugin.resolve_directories_in_nav(
golden.get("nav"), files, golden.get("nav_file_name"), golden.get("implicit_index")
golden.get("nav"),
files,
nav_file_name=golden.get("nav_file_name") or "index.md",
implicit_index=golden.get("implicit_index"),
)
assert output == golden.out["output"]


def _file_sort_key(f: File):
parts = pathlib.PurePath(f.src_path).parts
return tuple(chr(i != len(parts) - 1) + chr(f.name != "index") + p for i, p in enumerate(parts))


@pytest.mark.parametrize("use_directory_urls", [True, False])
def test_find_index_of_dir(tmp_path_factory, use_directory_urls):
src_dir, dest_dir = map(tmp_path_factory.mktemp, ["src", "dest"])
file_names = [
" README.md",
"README.md ",
"index.md",
"bad/index.html",
"bad/foo.md",
"bad/foo.index.md",
"both1/index.md",
"both1/README.md",
"both2/readme.md",
"both2/index.md",
"a/a/a/a/a/README.md",
]
files = {f: File(f, src_dir, dest_dir, use_directory_urls) for f in file_names}
files_o = Files(list(files.values()))

assert plugin.find_index_of_dir(files_o, "") == files["index.md"]
assert plugin.find_index_of_dir(files_o, "nonexistent") is None
assert plugin.find_index_of_dir(files_o, "bad") is None
assert plugin.find_index_of_dir(files_o, "bad/foo") is None
assert plugin.find_index_of_dir(files_o, "bad/foo.md") is None
assert plugin.find_index_of_dir(files_o, "both1") == files["both1/README.md"]
assert plugin.find_index_of_dir(files_o, "both2") == files["both2/index.md"]
assert plugin.find_index_of_dir(files_o, "bad", "foo.md") == files["bad/foo.md"]
assert plugin.find_index_of_dir(files_o, "both2", "foo.md") is None
assert plugin.find_index_of_dir(files_o, "a/a/a/a", "a") is None
assert plugin.find_index_of_dir(files_o, "a/a/a/a/a") == files["a/a/a/a/a/README.md"]
assert plugin.find_index_of_dir(files_o, "a/a/a/a/a/a") is None

0 comments on commit e6fd5b1

Please sign in to comment.