Skip to content

Commit 1c7cd4d

Browse files
committed
Add GitHub widget integration to site
1 parent 96a1657 commit 1c7cd4d

1 file changed

Lines changed: 52 additions & 6 deletions

File tree

great_docs/core.py

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,21 @@ def install(self, force: bool = False, skip_quartodoc: bool = False) -> None:
197197
shutil.copy2(gitignore_src, gitignore_dst)
198198
print(f"Copied {gitignore_dst}")
199199

200+
# Copy GitHub widget JavaScript file
201+
gh_widget_src = self.assets_path / "github-widget.js"
202+
gh_widget_dst = self.project_path / "github-widget.js"
203+
204+
if gh_widget_dst.exists() and not force:
205+
response = input(f"{gh_widget_dst} already exists. Overwrite? [y/N]: ")
206+
if response.lower() != "y":
207+
print("Skipping github-widget.js")
208+
else:
209+
shutil.copy2(gh_widget_src, gh_widget_dst)
210+
print(f"Copied {gh_widget_dst}")
211+
else:
212+
shutil.copy2(gh_widget_src, gh_widget_dst)
213+
print(f"Copied {gh_widget_dst}")
214+
200215
# Update _quarto.yml configuration
201216
self._update_quarto_config()
202217

@@ -2393,17 +2408,42 @@ def _update_quarto_config(self) -> None:
23932408
]
23942409
}
23952410

2396-
# Add GitHub icon link on the right if repository URL is available
2397-
metadata = self._get_package_metadata()
2398-
repo_url = None
2399-
if metadata.get("urls"):
2400-
repo_url = metadata["urls"].get("repository") or metadata["urls"].get("Repository")
2411+
# Add GitHub widget on the right if repository URL is available
2412+
owner, repo, repo_url = self._get_github_repo_info()
24012413

2402-
if repo_url and "github.com" in repo_url:
2414+
if owner and repo and repo_url:
2415+
# Create GitHub widget HTML with data attributes
2416+
gh_widget_html = (
2417+
f'<div id="github-widget" data-owner="{owner}" data-repo="{repo}"></div>'
2418+
)
2419+
navbar_config["right"] = [{"text": gh_widget_html}]
2420+
elif repo_url:
2421+
# Fallback to simple icon if we can't parse owner/repo
24032422
navbar_config["right"] = [{"icon": "github", "href": repo_url}]
24042423

24052424
config["website"]["navbar"] = navbar_config
24062425

2426+
# Add GitHub widget script to page if not present
2427+
owner, repo, _ = self._get_github_repo_info()
2428+
if owner and repo:
2429+
if "include-after-body" not in config["format"]["html"]:
2430+
config["format"]["html"]["include-after-body"] = []
2431+
elif isinstance(config["format"]["html"]["include-after-body"], str):
2432+
config["format"]["html"]["include-after-body"] = [
2433+
config["format"]["html"]["include-after-body"]
2434+
]
2435+
2436+
# Add the GitHub widget script
2437+
gh_script_entry = {"text": '<script src="github-widget.js"></script>'}
2438+
if gh_script_entry not in config["format"]["html"]["include-after-body"]:
2439+
# Check if github-widget.js is already included
2440+
has_gh_widget = any(
2441+
"github-widget" in str(item)
2442+
for item in config["format"]["html"]["include-after-body"]
2443+
)
2444+
if not has_gh_widget:
2445+
config["format"]["html"]["include-after-body"].append(gh_script_entry)
2446+
24072447
# Add sidebar navigation for reference pages
24082448
if "sidebar" not in config["website"]:
24092449
config["website"]["sidebar"] = [
@@ -2844,6 +2884,12 @@ def show_progress(stop_event, message):
28442884
if post_render_src.exists():
28452885
shutil.copy2(post_render_src, post_render_dst)
28462886

2887+
# Ensure GitHub widget JS is in place
2888+
gh_widget_src = self.assets_path / "github-widget.js"
2889+
gh_widget_dst = self.project_path / "github-widget.js"
2890+
if gh_widget_src.exists():
2891+
shutil.copy2(gh_widget_src, gh_widget_dst)
2892+
28472893
# Step 0: Rebuild index.qmd from source file (README.md, index.md, or index.qmd)
28482894
print("\n📄 Step 0: Syncing landing page with source file...")
28492895
self._create_index_from_readme(force_rebuild=True)

0 commit comments

Comments
 (0)