Skip to content

Commit

Permalink
Fix notebook links in gallery files (#315)
Browse files Browse the repository at this point in the history
* Fix notebook links in gallery files

* Apply replacements
  • Loading branch information
philippjfr authored Sep 16, 2024
1 parent 1a5c235 commit 7c5aae2
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions nbsite/gallery/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import json
import logging
import os
import re

from concurrent.futures import ThreadPoolExecutor
from functools import partial
from urllib.parse import urlparse

import requests
import sphinx.util
Expand Down Expand Up @@ -259,6 +261,45 @@ def get_nblink_rst(
text += f'`Download this {ftype}. </assets/{path}/{basename}>`_'
return text

def replace_ipynb_links(line: str, filepath: str) -> str:
"""
Replace relative .ipynb links in a Markdown line with .md links if the .ipynb file exists.
Arguments
---------
line: str
A line of Markdown text.
filepath: str
The path to the Markdown file containing the line.
Returns
-------
The modified line with .ipynb links replaced by .md links where applicable.
"""
markdown_link_regex = re.compile(r'\[([^\]]+)\]\(([^)]+)\)')

def replace_link(match):
text = match.group(1)
link = match.group(2)

# Do not change absolute links and non-ipynb links
parsed_url = urlparse(link)
if parsed_url.scheme != '' or parsed_url.netloc != '' or link.lower().endswith('.ipynb'):
return match.group(0)

md_dir = os.path.dirname(os.path.abspath(filepath))
ipynb_path = os.path.normpath(os.path.join(md_dir, link))

if os.path.exists(ipynb_path):
new_link = re.sub(r'\.ipynb$', '.md', link, flags=re.IGNORECASE)
return f'[{text}]({new_link})'
else:
# The .ipynb file does not exist; do not modify
return match.group(0)

new_line = markdown_link_regex.sub(replace_link, line)
return new_line

def convert_notebook_to_md(filename, directive='{pyodide}'):
with open(filename, encoding='utf-8') as nb:
nb_json = json.loads(nb.read())
Expand All @@ -276,6 +317,8 @@ def convert_notebook_to_md(filename, directive='{pyodide}'):
backticks = '`'*nticks
md += f'{backticks}{directive}\n'
for src in source:
if ctype == 'markdown':
src = replace_ipynb_links(src, filename)
md += src
if ctype == 'code':
md += f'\n{backticks}'
Expand Down

0 comments on commit 7c5aae2

Please sign in to comment.