在 Sphinx/reStructuredText 中添加在新选项卡中打开的链接

Han*_* Yu 5 restructuredtext hyperlink python-sphinx

这是同一问题的解决方案:

在 reStructuredText 中的新窗口中打开链接

但是,当文档有很多链接时(特别是当链接位于表格中时),此解决方案将无法正常工作。

还有其他解决方案吗?谢谢你的帮助!

小智 1

如果您希望在新选项卡中打开外部链接,请将以下代码添加到您的conf.py

from sphinx.writers.html import HTMLTranslator
class PatchedHTMLTranslator(HTMLTranslator):
   def visit_reference(self, node):
      if node.get('newtab') or not (node.get('target') or node.get('internal') 
         or 'refuri' not in node):
            node['target'] = '_blank'
            super().visit_reference(node)

def setup(app):
    app.set_translator('html', PatchedHTMLTranslator)
Run Code Online (Sandbox Code Playgroud)

这还使您能够使用 newtab 参数进行引用,使其显示为内部,但在新选项卡中打开。我用它来链接幻灯片 PDF。

来源: http ://jack.rosenth.al/hacking-docutils.html#external-links-in-new-tabs