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

Handle internal links without extensions #3384

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Release type: minor

Allow extensionless internal links
19 changes: 14 additions & 5 deletions pelican/contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
except ModuleNotFoundError:
from backports.zoneinfo import ZoneInfo


from pelican.plugins import signals
from pelican.settings import DEFAULT_CONFIG, Settings

Expand Down Expand Up @@ -282,7 +281,9 @@ def _link_replacer(self, siteurl: str, m: re.Match) -> str:
# XXX Put this in a different location.
if what in {"filename", "static", "attach"}:

def _get_linked_content(key: str, url: ParseResult) -> Optional[Content]:
def _get_linked_content(
key: str, url: ParseResult, extension: Optional[str] = None
) -> Optional[Content]:
nonlocal value

def _find_path(path: str) -> Optional[Content]:
Expand All @@ -295,13 +296,15 @@ def _find_path(path: str) -> Optional[Content]:
)
return self._context[key].get(path, None)

url_path = url.path if not extension else f"{url.path}.{extension}"

# try path
result = _find_path(url.path)
result = _find_path(url_path)
if result is not None:
return result

# try unquoted path
result = _find_path(unquote(url.path))
result = _find_path(unquote(url_path))
if result is not None:
return result

Expand Down Expand Up @@ -331,7 +334,13 @@ def _find_path(path: str) -> Optional[Content]:
else:
key = "static_content"

linked_content = _get_linked_content(key, value)
# Where do we find a comprehensive list of Reader extensions?
reader_ext = ["md", "markdown"]
extensions = [None] + reader_ext
for extension in extensions:
if linked_content := _get_linked_content(key, value, extension):
break

if linked_content:
if what == "attach":
linked_content.attach_to(self) # type: ignore
Expand Down