|
| 1 | +Adding New Tab |
| 2 | +============== |
| 3 | + |
| 4 | +.. figure:: images/layout_top.png |
| 5 | + :class: sd-border-2 |
| 6 | + |
| 7 | + This walkthrough will cover how to add new tabs to the top bar (highlighted in red). |
| 8 | + |
| 9 | +Find the Root Index |
| 10 | +------------------- |
| 11 | +The :code:`index.rst` files contain :code:`toctrees` that specify what rST documents should be included in the layout. |
| 12 | + |
| 13 | +In this walkthrough, we will need to edit the **root index**, which is the first **index.rst** file you find in the docs folder located within the project directory: |
| 14 | + |
| 15 | +.. code-block:: |
| 16 | +
|
| 17 | + 📦sphinx-documentation-demo |
| 18 | + ┗ 📂docs |
| 19 | + ┗ 📜index.rst ← this is the root index |
| 20 | +
|
| 21 | +
|
| 22 | +
|
| 23 | +Explore the Root Index |
| 24 | +---------------------- |
| 25 | +If you open up :code:`sphinx-documentation-demo/index.rst`, you will notice a :code:`tocree` containing the following: |
| 26 | + |
| 27 | +.. code-block:: |
| 28 | +
|
| 29 | + .. toctree:: |
| 30 | + :maxdepth: 1 |
| 31 | +
|
| 32 | + guides/index |
| 33 | + tab_2/index |
| 34 | + tab_3/index |
| 35 | +
|
| 36 | +| |
| 37 | +
|
| 38 | +.. admonition:: Why don't we add .rst? |
| 39 | + :class: sidebar note |
| 40 | + |
| 41 | + When we add :code:`guides/index` to the toctree in :code:`docs/index.rst`, Sphinx infers it's an rST and looks for a file called :code:`docs/guides/index.rst` |
| 42 | + |
| 43 | +This tells Sphinx to add three tabs. |
| 44 | +Like the root index, each tab will need to have their own :code:`index.rst`, as these indices will tell Sphinx what documents to include in each tab. |
| 45 | + |
| 46 | +It's good idea to keep the contents of each tab in a separate directory in :code:`docs/` |
| 47 | + |
| 48 | +| |
| 49 | +
|
| 50 | + |
| 51 | +Index File Layout for Tabs |
| 52 | +-------------------------- |
| 53 | +The following is a quick sketch of where Sphinx would expect files to be based on the :code:`toctree` in :code:`docs/index.rst`: |
| 54 | + |
| 55 | +.. code-block:: |
| 56 | +
|
| 57 | + 📦sphinx-documentation-demo |
| 58 | + ┗ 📂docs |
| 59 | + ┣ 📂guides |
| 60 | + ┃ ┗ 📜index.rst ← index for tab "guides" |
| 61 | + ┣ 📂tab_2 |
| 62 | + ┃ ┗ 📜index.rst ← index for tab "tab_2" |
| 63 | + ┣ 📂tab_3 |
| 64 | + ┃ ┗ 📜index.rst ← index for tab "tab_3" |
| 65 | + ┗ 📜index.rst ← root index |
| 66 | +
|
| 67 | +Creating "test" tab folder and empty index |
| 68 | +------------------------------------------ |
| 69 | +We will now try to add a new tab to Sphinx. |
| 70 | +It will be called "test_tab" and will be next to the other tabs already in our documentation. |
| 71 | + |
| 72 | + |
| 73 | +.. admonition:: Remember to build! |
| 74 | + :class: sidebar warning |
| 75 | + |
| 76 | + You need to use :code:`sphinx-build` or :code:`sphinx-autobuild` to build documentation before you notice any changes in the following steps. |
| 77 | + |
| 78 | +1. Create a new empty directory called "test" at: :code:`docs/test_tab/` |
| 79 | +2. Create a new empty "index.rst" file at: :code:`docs/test_tab/index.rst` |
| 80 | +3. Inside :code:`docs/index.rst`, in the indented :code:`toctree`, append the line :code:`test_tab/index`. It should look like the following: |
| 81 | + |
| 82 | + |
| 83 | +.. code-block:: |
| 84 | +
|
| 85 | + .. toctree:: |
| 86 | + :maxdepth: 1 |
| 87 | +
|
| 88 | + guides/index |
| 89 | + tab_2/index |
| 90 | + tab_3/index |
| 91 | + test_tab/index |
| 92 | +
|
| 93 | +Creating a "test document" |
| 94 | +-------------------------- |
| 95 | +To make our new "test_tab" meaningful, we need to also add a document that can be viewed while within that tab. |
| 96 | +We will do this now: |
| 97 | + |
| 98 | +1. Create an empty "test" doc at :code:`docs/test_tab/test_doc.rst` |
| 99 | +2. Add the following text to the "test_doc" document: |
| 100 | + |
| 101 | +.. code-block:: |
| 102 | +
|
| 103 | + Test Document |
| 104 | + ============= |
| 105 | +
|
| 106 | + Hi there! This document is just a stub. |
| 107 | +
|
| 108 | +Note that :code:`Test Document` above refers to the title of the document. |
| 109 | +This will be displayed when viewing the document, and it will also **be the name that will appear in the navigation bar to the left when we add this document to the tab**. |
| 110 | + |
| 111 | +Add "test document" to "test tab" index |
| 112 | +------------------------------------------ |
| 113 | +Now we need to tell Sphinx that the "test document" will be located in our new "test_tab". |
| 114 | + |
| 115 | +Add the following to :code:`docs/test_tab/index.rst` (if you mouse over the following code block, you can copy it by clicking the **copy button**): |
| 116 | + |
| 117 | +.. code-block:: |
| 118 | +
|
| 119 | + Test_Tab |
| 120 | + ======== |
| 121 | +
|
| 122 | + This is the index for the "Test Tab". |
| 123 | +
|
| 124 | + .. toctree:: |
| 125 | + :maxdepth: 1 |
| 126 | +
|
| 127 | + test_doc |
| 128 | +
|
| 129 | +Remember that this is the index file at :code:`docs/test_tab/index.rst`. |
| 130 | +This means that when we point to documents in this index's toctree, the path is relative to the :code:`docs/test_tab` directory. |
| 131 | + |
| 132 | +So when we added :code:`test_doc` to the previous toctree, it's assuming that the file structure looks like this: |
| 133 | + |
| 134 | +.. code-block:: |
| 135 | +
|
| 136 | + 📦sphinx-documentation-demo |
| 137 | + ┗ 📂docs |
| 138 | + ┗ 📂test_tab |
| 139 | + ┣ 📜index.rst |
| 140 | + ┗ 📜test_doc.rst |
| 141 | +
|
| 142 | +Build docs and explore the "test_tab" |
| 143 | +------------------------------------------ |
| 144 | +Make sure to build your documentation using :code:`sphinx-build` or :code:`sphinx-autobuild`. |
| 145 | + |
| 146 | +.. figure:: images/guide_add_tab_final.png |
| 147 | + :class: sd-border-2 |
| 148 | + |
| 149 | + Once the documentation is built, you should see something like this. |
| 150 | + |
| 151 | + |
| 152 | +You should see a new "Test_Tab" at the top navigation bar. |
| 153 | +This name comes from the title of the :code:`docs/test_tab/index.rst` document. |
| 154 | +Click on it to see the contents of the new tab. |
| 155 | + |
| 156 | +In the left sidebar, you should see "Test Document". |
| 157 | +This name comes from the title of the :code:`docs/test_tab/test_doc.rst` document. |
| 158 | +Click on it to view the contents (it should contain the text :code:`Hi there! This document is just a stub.`) |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | + |
0 commit comments