From 9a70709bba8a52a76c7647a6ddaf02ceaa3db8e5 Mon Sep 17 00:00:00 2001 From: pagedown Date: Wed, 24 Aug 2022 13:27:19 +0800 Subject: [PATCH] Shorten the reference link to the top of the page Reduce the length of the link :ref:`shell_integration` in commented conf. --- docs/extract-rst-targets.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/extract-rst-targets.py b/docs/extract-rst-targets.py index cff2afc5b..b349eb385 100755 --- a/docs/extract-rst-targets.py +++ b/docs/extract-rst-targets.py @@ -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}