diff --git a/.github/utils/add-category-id.py b/.github/utils/add-category-id.py new file mode 100644 index 00000000..e70d34ba --- /dev/null +++ b/.github/utils/add-category-id.py @@ -0,0 +1,56 @@ +import os +import re +from typing import List + + +def read_file(file_path: str) -> List[str]: + """Reads the content of a markdown file and returns it as a list of lines.""" + with open(file_path, "r", encoding="utf-8") as file: + content = file.readlines() + return content + + +def modify_header(content: List[str], category_id: str) -> List[str]: + """Modifies the YAML front matter in the markdown content to include the category.""" + in_header = False + new_content = [] + category_added = False + end_header_pattern = r"^---$" + start_header_found = False + + for line in content: + if re.match(end_header_pattern, line) and start_header_found: + in_header = False + if not category_added: + new_content.append(f"category: {category_id}\n") + new_content.append(line) + elif in_header: + if line.startswith("category:"): + new_content.append(f"category: {category_id}\n") + category_added = True + else: + new_content.append(line) + else: + if line.strip() == "---": + in_header = True + start_header_found = True + new_content.append(line) + return new_content + + +def update_markdown_files(directory: str, category_id: str) -> None: + """Updates all markdown files in a given directory by modifying their headers.""" + for filename in os.listdir(directory): + if filename.endswith(".md"): + file_path = os.path.join(directory, filename) + content = read_file(file_path) + modified_content = modify_header(content, category_id) + with open(file_path, "w", encoding="utf-8") as file: + file.writelines(modified_content) + + +# Example usage +if __name__ == "__main__": + directory = os.getenv("MARKDOWN_FILES_DIRECTORY", "default_directory") + category_id = os.getenv("CATEGORY_ID", "default_category_id") + update_markdown_files(directory, category_id) diff --git a/.github/workflows/api-docs.yaml b/.github/workflows/api-docs.yaml index 4ef75245..e72a211a 100644 --- a/.github/workflows/api-docs.yaml +++ b/.github/workflows/api-docs.yaml @@ -1,14 +1,12 @@ name: API Docs -# on: -# push: -# branches: -# - on: release: types: - published +env: + CATEGORY_ID: ${{ secrets.CATEGORY_ID }} permissions: contents: write @@ -17,12 +15,16 @@ jobs: deploy: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Setup python + uses: actions/setup-python@v5 with: python-version: 3.x - - uses: actions/cache@v4 + - name: Setup cache + uses: actions/cache@v4 with: key: ${{ github.ref }} path: .cache @@ -37,7 +39,6 @@ jobs: - name: Generate API docs run: ./.github/utils/pydoc-markdown.sh - - name: Configure git to push docs run: | git config --global user.name docs-bot @@ -48,7 +49,18 @@ jobs: - name: Install dependencies for doc deployment run: pip install mkdocs-material mkdocstrings[python] mkdocs-mermaid2-plugin mike - - name: Publish docs + - name: Publish docs to pages run: | mike deploy --push --update-aliases ${{github.ref_name}} && \ mike set-default --push ${{github.ref_name}} + + - name: Add Category ID to all API docs + run: python ./.github/utils/add-category-id.py + env: + MARKDOWN_FILES_DIRECTORY: docs/_pydoc/temp/ + CATEGORY_ID: ${{env.CATEGORY_ID}} + + - name: Run `docs` command 🚀 + uses: readmeio/rdme@v8 + with: + rdme: docs docs/_pydoc/temp --key=${{ secrets.README_API_KEY }} --version=1.0