Skip to content

Commit

Permalink
♻️ (docs): Replace prepare-docs script with MkDocs plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Mar 23, 2023
1 parent b9a6511 commit 1afbd8d
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 30 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ jobs:
- name: Checkout
uses: actions/checkout@v3

- name: Prepare docs
run: ./docs/hack/prepare-docs.sh "${{ github.server_url }}/${{ github.repository }}"

- name: Set up Python
uses: actions/setup-python@v4
with:
Expand All @@ -74,9 +71,6 @@ jobs:
working-directory: docs
run: mkdocs build

- name: Remove sitemap.xml.gz
run: rm docs/site/sitemap.xml.gz

- name: Deploy docs
uses: JamesIves/github-pages-deploy-action@v4
with:
Expand Down
24 changes: 0 additions & 24 deletions docs/hack/prepare-docs.sh

This file was deleted.

5 changes: 5 additions & 0 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ plugins:
cache_safe: true
css_files:
- stylesheets/extra.css
- mkdocs-simple-hooks:
hooks:
on_files: plugins.add_files:main
on_page_markdown: plugins.filter_readmes:main
on_post_build: plugins.remove_sitemap_gz:main

markdown_extensions:
- attr_list
Expand Down
41 changes: 41 additions & 0 deletions docs/plugins/add_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import os

from mkdocs.config.base import Config
from mkdocs.structure.files import File, Files


def main(files: Files, config: Config):
files.append(readme_file(config))

charts = []
for root, dirs, dirFiles in os.walk("../charts"):
for file in dirFiles:
if file.endswith(".md"):
charts.append(os.path.join(root, file))
charts.sort()
for path in charts:
files.append(chart_file(path, config))

return files


def readme_file(config: Config):
return File(
path="README.md",
src_dir="..",
dest_dir=config.site_dir,
use_directory_urls=config.use_directory_urls
)


def chart_file(src_path: str, config: Config):
src_dir = os.path.dirname(src_path)
chart_name = os.path.basename(src_dir)
f = File(
path=os.path.join("charts", chart_name + ".md"),
src_dir=src_dir,
dest_dir=config.site_dir,
use_directory_urls=config.use_directory_urls
)
f.abs_src_path = src_path
return f
35 changes: 35 additions & 0 deletions docs/plugins/filter_readmes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import re

from mkdocs.config.base import Config
from mkdocs.structure.files import Files
from mkdocs.structure.pages import Page


def main(markdown: str, page: Page, config: Config, files: Files):
if page.title == "Charts":
return filter_repo_readme(markdown, page, config)
elif page.url.startswith("charts"):
return filter_chart_readme(markdown, page, config)


def filter_repo_readme(markdown: str, page: Page, config: Config):
# Update page title
page.title = "Introduction"
markdown = markdown.split('\n', 1)[-1]
markdown = "# Introduction\n" + markdown

# Exclude chart overview from search
overview_re = re.compile(r"^(## Chart Overview)", re.MULTILINE)
markdown = overview_re.sub(r"\1 { data-search-exclude }", markdown)

return markdown


def filter_chart_readme(markdown: str, page: Page, config: Config):
homepage_re = re.compile(r"^\*\*Homepage:\*\*.*$", re.MULTILINE)
markdown = homepage_re.sub("", markdown)

yaml_re = re.compile(r"\(\./(.*\.yaml)\)")
markdown = yaml_re.sub(f"({config.repo_url}/blob/main/{page.url}\\1)", markdown)

return markdown
7 changes: 7 additions & 0 deletions docs/plugins/remove_sitemap_gz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import os

from mkdocs.config.base import Config


def main(config: Config):
os.remove(os.path.join(config.site_dir, "sitemap.xml.gz"))
1 change: 1 addition & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ mkdocs==1.4.2
mkdocs-material==9.1.3
mkdocs-material-extensions==1.1.1
mkdocs-minify-plugin==0.6.4
mkdocs-simple-hooks==0.1.5

0 comments on commit 1afbd8d

Please sign in to comment.