Skip to content

Commit ec7cfa6

Browse files
authored
Allow specifying Markdown config: extensions, tab_length (#15)
1 parent 2ee23c4 commit ec7cfa6

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,10 @@ Let's say you need the ability to infer nav for a sub-directory, but are unhappy
396396

397397
[See an example that generates both the files and the navigation covering them](https://github.com/mkdocstrings/mkdocstrings/blob/5802b1ef5ad9bf6077974f777bd55f32ce2bc219/docs/gen_doc_stubs.py#L25).
398398

399+
#### Indent lists by 2 spaces, not 4
400+
401+
Configure it through [tab_length](https://oprypin.github.io/mkdocs-literate-nav/reference.html#wildcards) or [markdown_extensions](https://oprypin.github.io/mkdocs-literate-nav/reference.html#wildcards)
402+
399403
#### Migrating from GitBook?
400404

401405
It might be very easy! Just beware of the stricter Markdown parser; it will *not* accept 2-space indentation for sub-lists.

docs/reference.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ Wildcards (items without a title that have an asterisk in them) get replaced by
9090
- literate-nav:
9191
nav_file: SUMMARY.md
9292
implicit_index: false
93+
tab_length: 4
9394
```
9495

9596
### Config
@@ -112,6 +113,28 @@ If a directory has a file named [`index.md` or `README.md`](https://www.mkdocs.o
112113

113114
This is important when using directory cross-linking, which otherwise makes it impossible to specify a *[section-index][]* page for a subdirectory.
114115

116+
#### `tab_length`
117+
118+
*integer, default `4`*
119+
120+
By default (like in MkDocs), lists need to be indented by 4 spaces. The more modern style is 2 spaces, though.
121+
122+
You can change the indentation just for the extension, but that will not affect MkDocs' rendering. If you want to change both at once, install [mdx_truly_sane_lists](https://github.com/radude/mdx_truly_sane_lists) and use it through `markdown_extensions`, instead of this option. See example below.
123+
124+
#### `markdown_extensions`
125+
126+
*list of mappings, [same as MkDocs](https://www.mkdocs.org/user-guide/configuration/#markdown_extensions)*
127+
128+
!!! example "mkdocs.yml"
129+
```yaml
130+
plugins:
131+
- literate-nav:
132+
markdown_extensions:
133+
- mdx_truly_sane_lists
134+
135+
markdown_extensions:
136+
- mdx_truly_sane_lists
137+
```
115138

116139
[mkdocs-nav]: https://www.mkdocs.org/user-guide/writing-your-docs/#configure-pages-and-navigation
117140
[docs_dir]: https://www.mkdocs.org/user-guide/configuration/#docs_dir

mkdocs_literate_nav/parser.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ def __init__(
3838
get_nav_for_dir: Callable[[str], tuple[str, str] | None],
3939
globber,
4040
implicit_index: bool = False,
41+
markdown_config: dict | None = None,
4142
):
4243
self.get_nav_for_dir = get_nav_for_dir
4344
self.globber = globber
4445
self.implicit_index = implicit_index
46+
self._markdown_config = markdown_config or {}
4547
self.seen_items: set[str] = set()
4648
self._warn = functools.lru_cache()(log.warning)
4749

@@ -51,7 +53,11 @@ def markdown_to_nav(self, roots: tuple[str, ...] = (".",)) -> Nav:
5153
dir_nav = self.get_nav_for_dir(root)
5254
if dir_nav:
5355
nav_file_name, md = dir_nav
54-
markdown.markdown(md, extensions=[ext])
56+
markdown_config = dict(
57+
self._markdown_config,
58+
extensions=[ext, *self._markdown_config.get("extensions", ())],
59+
)
60+
markdown.markdown(md, **markdown_config)
5561
if ext.nav is not None:
5662
self_path = posixpath.normpath(posixpath.join(root, nav_file_name))
5763
if not (self.implicit_index and self_path == self.globber.find_index(root)):

mkdocs_literate_nav/plugin.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
class _PluginConfig:
3030
nav_file = mkdocs.config.config_options.Type(str, default="SUMMARY.md")
3131
implicit_index = mkdocs.config.config_options.Type(bool, default=False)
32+
markdown_extensions = mkdocs.config.config_options.MarkdownExtensions()
33+
tab_length = mkdocs.config.config_options.Type(int, default=4)
3234

3335

3436
class LiterateNavPlugin(mkdocs.plugins.BasePlugin):
@@ -45,6 +47,11 @@ def on_files(self, files: mkdocs.structure.files.Files, config: mkdocs.config.Co
4547
files,
4648
nav_file_name=self.config["nav_file"],
4749
implicit_index=self.config["implicit_index"],
50+
markdown_config=dict(
51+
extensions=self.config["markdown_extensions"],
52+
extension_configs=self.config["mdx_configs"],
53+
tab_length=self.config["tab_length"],
54+
),
4855
)
4956
self._files = files
5057

@@ -62,7 +69,11 @@ def on_nav(
6269

6370

6471
def resolve_directories_in_nav(
65-
nav_data, files: mkdocs.structure.files.Files, nav_file_name: str, implicit_index: bool
72+
nav_data,
73+
files: mkdocs.structure.files.Files,
74+
nav_file_name: str,
75+
implicit_index: bool,
76+
markdown_config: dict | None = None,
6677
):
6778
"""Walk through a standard MkDocs nav config and replace `directory/` references.
6879
@@ -86,7 +97,9 @@ def get_nav_for_dir(path: str) -> tuple[str, str] | None:
8697
return nav_file_name, f.read()
8798

8899
globber = MkDocsGlobber(files)
89-
nav_parser = parser.NavParser(get_nav_for_dir, globber, implicit_index=implicit_index)
100+
nav_parser = parser.NavParser(
101+
get_nav_for_dir, globber, implicit_index=implicit_index, markdown_config=markdown_config
102+
)
90103

91104
result = None
92105
if not nav_data or get_nav_for_dir("."):

0 commit comments

Comments
 (0)