Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: publish to readme #188

Merged
merged 5 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions .github/utils/add-category-id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import os
ArzelaAscoIi marked this conversation as resolved.
Show resolved Hide resolved
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)
30 changes: 21 additions & 9 deletions .github/workflows/api-docs.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
name: API Docs

# on:
# push:
# branches:
# - <your-branch>
on:
release:
types:
- published

env:
CATEGORY_ID: ${{ secrets.CATEGORY_ID }}

permissions:
contents: write
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Loading