elastic / elastic/ai-github-actions
[framework-best-practices] Parse mkdocs nav with YAML instead of regex
- Dominant language
- Python
- Stars
- 11
- Forks
- 16
- Avg merge
- 22h 9m
- Merged PRs (30d)
- 31
Description
## Framework / Library Best Practices Findings
### 1. Parse MkDocs nav with YAML instead of regex
**Library:** PyYAML `>=6.0` from `pyproject.toml`, with MkDocs `>=1.6.0,<2.0`.
**Library feature:** `yaml.safe_load()` can parse `mkdocs.yml` according to YAML semantics before the script recursively walks the `nav` structure. MkDocs' config loader is another option if the check should mirror MkDocs' complete config processing.
**Current code:** `scripts/check-nav-catalog.py:56-91` manually finds the `nav:` block by splitting lines, then applies a regex to the raw text:
```python
lines = mkdocs_text.splitlines()
# ... locate nav: by indentation ...
nav_text = "\n".join(nav_lines)
return {
m.group(1)
for m in re.finditer(r"workflows/gh-agent-workflows/([a-z0-9-]+)\.md", nav_text)
}
```
Because this scans raw YAML text instead of parsed YAML values, it counts workflow paths inside inline comments as real nav entries. A minimal reproduction against the current function returns both `comment-only` and `real` even though only `real` is an actual nav value:
```python
mkdocs_text = """site_name: demo
nav:
- Home: index.md # workflows/gh-agent-workflows/comment-only.md
- Real: workflows/gh-agent-workflows/real.md
plugins:
- search
"""
print(sorted(extract_nav_slugs(mkdocs_text)))
# ['comment-only', 'real']
```
**Simplification:** Replace `extract_nav_slugs()` with YAML-backed parsing, then recursively collect string values from `config["nav"]`. That removes the custom indentation scanner and prevents comments, quoting, or other valid YAML forms from diverging from MkDocs' actual configuration. The CI job at `.github/workflows/ci.yml:27-33` can continue invoking the same script after installing/running with the project dependencies, or the script can use MkDocs' config loader if exact MkDocs semantics are desired.
**Documentation:** (pyyaml.org/redacted) and (www.mkdocs.org/redacted)
## Suggested Actions
- [ ] Refactor `scripts/check-nav-catalog.py` to load `mkdocs.yml` with `yaml.safe_load()` or MkDocs' config loader instead of scanning raw lines.
- [ ] Add a regression test where an inline YAML comment contains `workflows/gh-agent-workflows/.md` and confirm it is not counted as reachable nav.
- [ ] Update the CI invocation if needed so the nav/catalog check runs with the dependency that performs YAML/MkDocs parsing.
---
[What is this?](https://ela.st/github-ai-tools) | [From workflow: Trigger Framework Best Practices](https://github.com/elastic/ai-github-actions/actions/runs/28448713032)
Give us feedback! React with 🚀 if perfect, 👍 if helpful, 👎 if not.
Contributor guide
Research direction
Start with extract_nav_slugs() in scripts/check-nav-catalog.py:56-91, then inspect the PyYAML/MkDocs dependencies in pyproject.toml and the invocation in .github/workflows/ci.yml:27-33. Add the requested regression test for a workflow path inside an inline YAML comment, and verify that only actual nav values are collected while CI still runs the check.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- ci-cd, tooling
- Issue type
- Refactor
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 78/100