mirror of
https://github.com/zadam/trilium.git
synced 2025-10-20 15:19:01 +02:00
feat(docs): try to fix local doc links
This commit is contained in:
parent
94089113ef
commit
e8ca443697
@ -84,6 +84,24 @@ def update_references(docs_dir):
|
|||||||
|
|
||||||
# Resolve the link path relative to current file
|
# Resolve the link path relative to current file
|
||||||
current_dir = Path(root)
|
current_dir = Path(root)
|
||||||
|
current_file = Path(root) / file
|
||||||
|
|
||||||
|
# Check if the current file is an index.md that was moved
|
||||||
|
# If so, we need to adjust relative paths
|
||||||
|
is_index = file == 'index.md'
|
||||||
|
|
||||||
|
# Special case: if we're in index.md and the link starts with the parent directory name
|
||||||
|
# This happens when a file was converted to index.md and had links to siblings
|
||||||
|
if is_index and '/' in decoded_link:
|
||||||
|
path_parts = decoded_link.split('/')
|
||||||
|
# Check if first part matches the parent directory name
|
||||||
|
if path_parts[0] == current_dir.name:
|
||||||
|
# This is a self-referential path, strip the first part
|
||||||
|
fixed_link = '/'.join(path_parts[1:])
|
||||||
|
# Re-encode spaces for URL compatibility before recursing
|
||||||
|
fixed_link_encoded = fixed_link.replace(' ', '%20')
|
||||||
|
# Recursively process the fixed link
|
||||||
|
return fix_link(re.match(pattern, f'[{text}]({fixed_link_encoded})'))
|
||||||
|
|
||||||
# For any .md link, check if there's a directory with index.md
|
# For any .md link, check if there's a directory with index.md
|
||||||
# that should be used instead
|
# that should be used instead
|
||||||
@ -99,17 +117,26 @@ def update_references(docs_dir):
|
|||||||
|
|
||||||
# If a directory with index.md exists, update the link
|
# If a directory with index.md exists, update the link
|
||||||
if potential_index.exists():
|
if potential_index.exists():
|
||||||
|
# If we're in an index.md file and linking to a file that's now
|
||||||
|
# in a sibling directory, adjust the path
|
||||||
|
if is_index:
|
||||||
|
# Check if they share the same parent directory
|
||||||
|
if potential_dir.parent == current_dir.parent:
|
||||||
|
# It's a sibling - just use directory name
|
||||||
|
dir_name = potential_dir.name
|
||||||
|
dir_name = dir_name.replace(' ', '%20')
|
||||||
|
return f'[{text}]({dir_name}/)'
|
||||||
|
|
||||||
# Calculate relative path from current file to the directory
|
# Calculate relative path from current file to the directory
|
||||||
new_path = os.path.relpath(potential_dir, current_dir)
|
new_path = os.path.relpath(potential_dir, current_dir)
|
||||||
# Return link pointing to directory (MkDocs will serve index.md)
|
|
||||||
# Replace backslashes with forward slashes for consistency
|
# Replace backslashes with forward slashes for consistency
|
||||||
new_path = new_path.replace('\\', '/')
|
new_path = new_path.replace('\\', '/')
|
||||||
# Re-encode spaces in the path for URL compatibility
|
# Re-encode spaces in the path for URL compatibility
|
||||||
new_path = new_path.replace(' ', '%20')
|
new_path = new_path.replace(' ', '%20')
|
||||||
return f'[{text}]({new_path}/)'
|
return f'[{text}]({new_path}/)'
|
||||||
|
|
||||||
# Also handle local references (same directory)
|
# Also handle local references (same directory) - should be 'if', not 'elif'
|
||||||
elif '/' not in decoded_link:
|
if '/' not in decoded_link:
|
||||||
basename = decoded_link[:-3] # Remove .md
|
basename = decoded_link[:-3] # Remove .md
|
||||||
possible_dir = current_dir / basename
|
possible_dir = current_dir / basename
|
||||||
if possible_dir.exists() and possible_dir.is_dir():
|
if possible_dir.exists() and possible_dir.is_dir():
|
||||||
|
Loading…
x
Reference in New Issue
Block a user