Skip to content

Commit e3aa0d4

Browse files
jacobstrclaude
andcommitted
Add support for partial nav inclusion using anchor syntax
This commit adds the ability to include only specific sections of a navigation tree from an included mkdocs.yml file using anchor syntax. Syntax: !include path/to/mkdocs.yml#SectionName Changes: - Parse anchor fragments from include paths (e.g., #Guides) - Extract only the specified nav section from the included file - Generate unique aliases by appending section names to avoid conflicts - Update path resolution to handle anchored includes correctly Example usage: nav: - Guides: - My Section: "!include docs/other/mkdocs.yml#Guides" This allows for more flexible documentation organization by reusing specific sections of navigation trees without duplicating content or including entire documentation hierarchies. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent f62ae22 commit e3aa0d4

1 file changed

Lines changed: 39 additions & 5 deletions

File tree

mkdocs_monorepo_plugin/parser.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,12 @@ def getResolvedPaths(self):
7373
def extractAliasAndPath(absPath):
7474
loader = IncludeNavLoader(self.config, absPath).read()
7575
alias = loader.getAlias()
76+
# Use the actual navPath (without anchor) for directory calculations
77+
actualPath = loader.navPath
7678
docsDir = os.path.join(
77-
loader.rootDir, os.path.dirname(absPath), loader.getDocsDir()
79+
loader.rootDir, os.path.dirname(actualPath), loader.getDocsDir()
7880
)
79-
return [alias, docsDir, os.path.join(loader.rootDir, absPath)]
81+
return [alias, docsDir, os.path.join(loader.rootDir, actualPath)]
8082

8183
resolvedPaths = list(
8284
map(extractAliasAndPath, self.__loadAliasesAndResolvedPaths())
@@ -172,7 +174,12 @@ def __init__(self, config, navPath, ancestors=None):
172174
self.rootDir = os.path.normpath(
173175
os.path.join(os.getcwd(), config["config_file_path"], "../")
174176
)
175-
self.navPath = navPath
177+
# Parse navPath for anchor (e.g., "path/to/mkdocs.yml#Guides")
178+
if "#" in navPath:
179+
self.navPath, self.navSection = navPath.split("#", 1)
180+
else:
181+
self.navPath = navPath
182+
self.navSection = None
176183
self.absNavPath = os.path.normpath(os.path.join(self.rootDir, self.navPath))
177184
self.navYaml = None
178185
# Track ancestor paths to detect cycles
@@ -285,15 +292,42 @@ def getDocsDir(self):
285292

286293
def getAlias(self):
287294
alias = self.navYaml["site_name"]
295+
296+
# If a section is specified, append it to the alias to make it unique
297+
if self.navSection:
298+
alias = f"{alias}-{self.navSection}"
299+
288300
regex = "^[a-zA-Z0-9_\.\-/]+$" # noqa: W605
289301

290302
if re.match(regex, alias) is None:
291-
alias = slugify(self.navYaml["site_name"])
303+
alias = slugify(alias)
292304

293305
return alias
294306

295307
def getNav(self):
296-
return self._prependAliasToNavLinks(self.getAlias(), self.navYaml["nav"])
308+
nav = self.navYaml["nav"]
309+
310+
# If a section is specified, extract only that section
311+
if self.navSection:
312+
nav = self._extractNavSection(nav, self.navSection)
313+
if nav is None:
314+
log.critical(
315+
f"[mkdocs-monorepo] Could not find nav section '{self.navSection}' "
316+
f"in {self.absNavPath}"
317+
)
318+
raise SystemExit(1)
319+
320+
return self._prependAliasToNavLinks(self.getAlias(), nav)
321+
322+
def _extractNavSection(self, nav, section_name):
323+
"""Extract a specific section from the nav by its name."""
324+
for item in nav:
325+
if isinstance(item, dict):
326+
key = list(item.keys())[0]
327+
if key == section_name:
328+
# Return the contents of this section
329+
return item[key]
330+
return None
297331

298332
def _prependAliasToNavLinks(self, alias, nav):
299333
for index, item in enumerate(nav):

0 commit comments

Comments
 (0)