|
| 1 | +(linktree-directive)= |
| 2 | + |
| 3 | +# Link Tree |
| 4 | + |
| 5 | + |
| 6 | +## About |
| 7 | + |
| 8 | +Similar but different from a Toc Tree. |
| 9 | + |
| 10 | +```{attention} |
| 11 | +This component is a work in progress. Breaking changes should be expected until a |
| 12 | +1.0 release, so version pinning is recommended. |
| 13 | +``` |
| 14 | + |
| 15 | +### Problem |
| 16 | + |
| 17 | +So much work went into the toctree mechanics, it is sad that it is not a reusable |
| 18 | +component for building any kinds of navigation structures, and to be able to define |
| 19 | +its contents more freely. |
| 20 | + |
| 21 | +### Solution |
| 22 | + |
| 23 | +This component implements a programmable toc tree component, the link tree. |
| 24 | + |
| 25 | + |
| 26 | +## Details |
| 27 | + |
| 28 | +The link tree component builds upon the Sphinx [toc] and [toctree] subsystem. It provides |
| 29 | +both a rendered primary navigation within the `sde_linktree_primary` context variable |
| 30 | +for use from HTML templates, and a Sphinx directive, `linktree`, for rendering |
| 31 | +navigation trees into pages, similar but different from the [toctree directive]. The |
| 32 | +user interface mechanics and styles are based on [Furo]'s primary sidebar component. |
| 33 | + |
| 34 | + |
| 35 | +## Customizing |
| 36 | + |
| 37 | +Link trees can be customized by creating them programmatically, similar to how |
| 38 | +the `sde_linktree_primary` context variable is populated with the default Sphinx |
| 39 | +toc tree. |
| 40 | + |
| 41 | +The section hidden behind the dropdown outlines how the "custom linktree" is |
| 42 | +defined, which is displayed at the bottom of the page in a rendered variant. |
| 43 | +:::{dropdown} Custom linktree example code |
| 44 | + |
| 45 | +```python |
| 46 | +import typing as t |
| 47 | + |
| 48 | +from sphinx.application import Sphinx |
| 49 | +from sphinx_design_elements.lib.linktree import LinkTree |
| 50 | + |
| 51 | + |
| 52 | +def demo_tree(app: Sphinx, context: t.Dict[str, t.Any], docname: str = None) -> LinkTree: |
| 53 | + """ |
| 54 | + The demo link tree showcases some features what can be done. |
| 55 | +
|
| 56 | + It uses regular page links to documents in the current project, a few |
| 57 | + intersphinx references, and a few plain, regular, URL-based links. |
| 58 | + """ |
| 59 | + linktree = LinkTree.from_context(app=app, context=context) |
| 60 | + doc = linktree.api.doc |
| 61 | + ref = linktree.api.ref |
| 62 | + link = linktree.api.link |
| 63 | + |
| 64 | + linktree \ |
| 65 | + .title("Project-local page links") \ |
| 66 | + .add( |
| 67 | + doc(name="gridtable"), |
| 68 | + doc(name="infocard"), |
| 69 | + ) |
| 70 | + |
| 71 | + linktree \ |
| 72 | + .title("Intersphinx links") \ |
| 73 | + .add( |
| 74 | + ref("sd:index"), |
| 75 | + ref("sd:badges", label="sphinx{design} badges"), |
| 76 | + ref("myst:syntax/images_and_figures", "MyST » Images and figures"), |
| 77 | + ref("myst:syntax/referencing", "MyST » Cross references"), |
| 78 | + ) |
| 79 | + |
| 80 | + linktree \ |
| 81 | + .title("URL links") \ |
| 82 | + .add( |
| 83 | + link(uri="https://example.com"), |
| 84 | + link(uri="https://example.com", label="A link to example.com, using a custom label ⚽."), |
| 85 | + ) |
| 86 | + |
| 87 | + return linktree |
| 88 | +``` |
| 89 | +::: |
| 90 | + |
| 91 | +```{todo} |
| 92 | +- Use the `linktree` directive to define custom link trees. |
| 93 | +- Link to other examples of custom link trees. |
| 94 | +- Maybe use `:link:` and `:link-type:` directive options of `grid-item-card` directive. |
| 95 | +``` |
| 96 | + |
| 97 | + |
| 98 | +## Directive examples |
| 99 | + |
| 100 | +### Example 1 |
| 101 | + |
| 102 | +The link tree of the `index` page, using a defined maximum depth, and a custom title. |
| 103 | +```{linktree} |
| 104 | +:docname: index |
| 105 | +:maxdepth: 1 |
| 106 | +:title: Custom title |
| 107 | +``` |
| 108 | + |
| 109 | + |
| 110 | +## Appendix |
| 111 | + |
| 112 | +Here, at the bottom of the page, different global template variables are presented, |
| 113 | +which contain representations of navigation trees, rendered to HTML. |
| 114 | + |
| 115 | +- `sde_linktree_primary`: The classic toctree, like it will usually be rendered |
| 116 | + into the primary sidebar. |
| 117 | +- `demo_synthetic_linktree`: A customized link tree composed of links to project-local |
| 118 | + pages, intersphinx links, and URLs, for demonstration purposes. |
| 119 | + |
| 120 | +```{hint} |
| 121 | +The corresponding template, `linktree-demo.html` will exclusively be rendered |
| 122 | +here, and not on other pages. |
| 123 | +``` |
| 124 | + |
| 125 | +[Furo]: https://pradyunsg.me/furo/ |
| 126 | +[toctree directive]: https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-toctree |
| 127 | +[toc]: https://www.sphinx-doc.org/en/master/development/templating.html#toc |
| 128 | +[toctree]: https://www.sphinx-doc.org/en/master/development/templating.html#toctree |
0 commit comments