Skip to content

Commit

Permalink
Add correct URL to mkdocs config file when executing the social image…
Browse files Browse the repository at this point in the history
… generate CLI command (#202)

* Add correct url in config and template files

* Add correct url in config and template files

* Update CLI help text

* Update guide

---------

Co-authored-by: Sean Bartik <[email protected]>
  • Loading branch information
harishmohanraj and Sean Bartik authored Jun 27, 2023
1 parent bc21192 commit 030b98c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 150 deletions.
4 changes: 2 additions & 2 deletions nbdev_mkdocs/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ def generate(
),
prompt: str = typer.Option(
"Cute animal wearing hoodie sitting in high chair in purple room, browsing computer, 3d render",
help="The prompt to use for generating the image.",
help="The prompt to use for generating the image. Please pass this option only when using 'dall_e' as the generator.",
),
image_path: Optional[str] = typer.Option(
None,
help="Image file path to use in the social share image. Use images with a 1:1 aspect ratio and at least 512x512 pixels for the best results. If None, then the default image will be used.",
help="Image file path to use in the social share image. Use images with a 1:1 aspect ratio and at least 512x512 pixels for the best results. If None, then the default image will be used. Please pass this option only when using 'file' as the generator.",
),
) -> None:
"""CLI command for generating a custom social share image"""
Expand Down
20 changes: 8 additions & 12 deletions nbdev_mkdocs/social_image_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ def _update_social_image_in_mkdocs_yml(root_path: str, image_url: str) -> None:
The above docstring is autogenerated by docstring-gen library (https://github.com/airtai/docstring-gen)
"""
image_url = (
"overrides/images/social_image.png" if is_local_path(image_url) else image_url
image_url
if "https://opengraph.githubassets.com" in image_url
else "overrides/images/social_image.png"
)

mkdocs_yml_path = Path(root_path) / "mkdocs" / "mkdocs.yml"
Expand All @@ -205,23 +207,17 @@ def _update_social_image_in_mkdocs_yml(root_path: str, image_url: str) -> None:
yaml.dump(config, mkdocs_yml_path, transform=_unescape_exclamation_mark)

# %% ../nbs/Social_Image_Generator.ipynb 19
def _update_social_image_in_site_overrides(root_path: str, image_url: str) -> None:
def _update_social_image_in_site_overrides(root_path: str) -> None:
"""Update social image link in site_overrides HTML template
Args:
root_path: The root path of the project
image_url: The social image url
!!! note
The above docstring is autogenerated by docstring-gen library (https://github.com/airtai/docstring-gen)
"""
_replace_str = (
'config.site_url ~ ("" if config.site_url.endswith("/") else "/") ~ config.extra.social_image '
if is_local_path(image_url)
else "config.extra.social_image "
)

_replace_str = 'config.site_url ~ ("" if config.site_url.endswith("/") else "/") ~ config.extra.social_image '
with set_cwd(root_path):
site_overrides_path = (
Path(root_path) / "mkdocs" / "site_overrides" / "main.html"
Expand All @@ -240,7 +236,7 @@ def _update_social_image_in_site_overrides(root_path: str, image_url: str) -> No
with open(site_overrides_path, "w", encoding="utf-8") as f:
f.write(_new_text)

# %% ../nbs/Social_Image_Generator.ipynb 22
# %% ../nbs/Social_Image_Generator.ipynb 21
class _IMG_Generator(str, Enum):
"""An enumeration class for the different types of image generators.
Expand Down Expand Up @@ -313,7 +309,7 @@ def _generate_image_url(

return image_url

# %% ../nbs/Social_Image_Generator.ipynb 29
# %% ../nbs/Social_Image_Generator.ipynb 28
async def generate_social_image(
root_path: str,
generator: str = "file",
Expand All @@ -338,4 +334,4 @@ async def generate_social_image(

_update_social_image_in_mkdocs_yml(root_path, image_url)

_update_social_image_in_site_overrides(root_path, image_url)
_update_social_image_in_site_overrides(root_path)
4 changes: 2 additions & 2 deletions nbs/CLI.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,11 @@
" ),\n",
" prompt: str = typer.Option(\n",
" \"Cute animal wearing hoodie sitting in high chair in purple room, browsing computer, 3d render\",\n",
" help=\"The prompt to use for generating the image.\",\n",
" help=\"The prompt to use for generating the image. Please pass this option only when using 'dall_e' as the generator.\",\n",
" ),\n",
" image_path: Optional[str] = typer.Option(\n",
" None,\n",
" help=\"Image file path to use in the social share image. Use images with a 1:1 aspect ratio and at least 512x512 pixels for the best results. If None, then the default image will be used.\",\n",
" help=\"Image file path to use in the social share image. Use images with a 1:1 aspect ratio and at least 512x512 pixels for the best results. If None, then the default image will be used. Please pass this option only when using 'file' as the generator.\",\n",
" ),\n",
" ) -> None:\n",
" \"\"\"CLI command for generating a custom social share image\"\"\"\n",
Expand Down
Loading

0 comments on commit 030b98c

Please sign in to comment.