Shorten the reference link to the top of the page

Reduce the length of the link :ref:`shell_integration` in commented conf.
This commit is contained in:
pagedown 2022-08-24 13:27:19 +08:00
parent c437a36794
commit 9a70709bba
No known key found for this signature in database
GPG Key ID: E921CF18AC8FF6EB

View File

@ -25,7 +25,17 @@ def main() -> Dict[str, str]:
raw = stream.read()
href = os.path.relpath(stream.name, base).replace(os.sep, '/')
href = href.rpartition('.')[0] + '/'
first_line = raw.lstrip('\n').partition('\n')[0]
first_target_added = False
for explicit_target in find_explicit_targets(raw):
# Shorten the reference link to the top of the page.
# Note that anchor links should still be used in HTML docs
# to allow jumping within the same page.
if not first_target_added:
first_target_added = True
if first_line.startswith(f'.. _{explicit_target}:'):
refs[explicit_target] = href
continue
refs[explicit_target] = href + f'#{explicit_target.replace("_", "-")}'
return {'ref': refs}