Skip to content

Commit

Permalink
fix: download local copy of images (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
parkerbxyz authored Oct 11, 2023
1 parent 9b3792c commit b4578b0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions .yamllint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ extends: default
rules:
braces:
max-spaces-inside: 1
line-length: disable
12 changes: 12 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ runs:
using: "composite"
steps:
- uses: actions/checkout@v3
- name: git config user
shell: bash
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
with:
lfs: true
- name: Install pipenv
shell: bash
run: pip install --user pipenv
Expand All @@ -37,6 +44,11 @@ runs:
- name: Pull changes from sync so we can update the metadata file
shell: bash
run: git pull
- uses: stefanzweifel/git-auto-commit-action@3ea6ae190baf489ba007f7c92608f33ce20ef04a # v4.16.0
with:
file_pattern: ".gitattributes **/resources/*"
commit_message: "Update resources"
commit_author: "github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>"
- uses: stefanzweifel/git-auto-commit-action@3ea6ae190baf489ba007f7c92608f33ce20ef04a # v4.16.0
with:
file_pattern: "*GitHubPublisher.json"
Expand Down
22 changes: 22 additions & 0 deletions github_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import base64
import re
import subprocess # nosec B404
import time
import uuid
from functools import lru_cache
Expand Down Expand Up @@ -629,6 +630,27 @@ def convert_card_content(self, card: guru.Card):
for iframe in content.select("iframe"):
iframe.replace_with(iframe.attrs.get("src"))

# Download images and replace image URLs with local file paths
for image in content.select("img"):
filename = image.attrs.get("data-ghq-card-content-image-filename")
file_extension = path.splitext(filename)[1]

collection_path: str = self.get_external_collection_path(card.collection)
image_relative_path = f"resources/{filename}"
image_absolute_path = f"{collection_path}/{image_relative_path}"
guru.download_file(
image.attrs.get("src"),
image_absolute_path,
headers={"Authorization": source._Guru__get_basic_auth_value()},
)
image.attrs["src"] = image_relative_path

# Ensure the file extension is tracked by Git LFS
subprocess.run(["/usr/bin/git", "lfs", "track", f"*{file_extension}"], check=True) # nosec B603

# Stage the file for commit
subprocess.run(["/usr/bin/git", "add", image_absolute_path], check=True) # nosec B603

# Add a title to the content that links to the card in Guru
return f"# [{card.title}]({card.url})\n\n{content.prettify()}"

Expand Down

0 comments on commit b4578b0

Please sign in to comment.