0

我已将 Sphinx 配置为使用降价文件。

在我的 index.rst 文件中,我有

.. toctree::
   :maxdepth: 2
   :caption: Contents:

   documents/Markdown

在 Markdown.md 我有

# Markdown

## H2 Heading

当我渲染主页时,我得到 H2 标题出现在目录树中。

在此处输入图像描述

我的目录树有其他部分,我希望 :maxdepth 大于 1。为什么 sphinx 将 H2 标题作为目录树的一部分读取,我怎样才能让它停止这样做,而不必设置 :maxdepth到 1?

4

3 回答 3

1

添加到@SuperKogito 的答案。如果您希望您的 TOC 支持不同的深度级别,同时仍然看起来很完整,您可以通过 CSS 来实现。

例如,给定以下部分

Contents  # this will create a <div id="contents>...</>
========

.. toctree::
   :maxdepth: 1

   documents/Markdown1

.. toctree::
   :maxdepth: 2

   documents/Markdown2

在您的conf.py, 在底部,添加以下行

def setup(app):
    app.add_css_file('styles.css')

现在在您的 _static 文件夹(与您的文件夹级别相同conf.py)中,添加一个名为styles.css以下行的文件

// this selects the contents section, the first table of contents and the list item
// underneath it. Then it removes the spacing to make it look whole. If you have more
// items, you can consider using :not(:last-of-type) selector. 
#contents .toctree-wrapper:first-of-type ul {
    margin-bottom: 0;
}
于 2020-08-07T16:21:21.647 回答
1

@mzjn 部分回答您的请求。就个人而言,我不确定这在 Markdown 中是如何完成的,但我认为它类似于 reStructuredText。不幸的是,目前还没有一种直观的方法可以做到这一点。但是,您可以执行以下操作:

.. toctree::
   :maxdepth: 1

   documents/Markdown1

.. toctree::
   :maxdepth: 2

   documents/Markdown2

这将输出所需的行为,但在这种情况下,两棵树之间会有一些垂直间距。要么你这样做,要么你可以使用:

.. toctree::
   :maxdepth: 2

   documents/Markdown1
   documents/Markdown2

但是您需要将不希望显示的内容转移到较低级别(例如 H3)。

于 2019-03-12T14:18:14.943 回答
-1

maxdepth选项指示所需的 TOC 深度。

如果您使用:maxdepth: 1,“H2 标题”应该会消失。

于 2019-02-21T06:46:51.533 回答