Debian Codegraph @

docs/user_guide/static_assets.md

raw
1 # Add custom CSS and JS assets
2
3 If you'd like to modify this theme or sections on the page, you'll need to add custom CSS or JavaScript to your theme.
4 Since this is a common operation we cover a few ways to do this here.
5
6 ````{admonition} Sample site structure
7 In all examples below, assume we have a site structure like this:
8
9 ```
10 mysphinxsite/
11 ├── _static
12 │   ├── mycss.css
13 │   └── myjs.js
14 └── conf.py
15 ```
16 ````
17
18 ## First: define your `html_static_path`
19
20 Any folders that are listed in `html_static_path` will be treated as containing static assets for your build.
21 All files within these folders will be copied to your build's `_static` folder at build time.
22 For example, with an HTML builder, files will be copied to `_build/html/_static`.
23
24 These files are _flattened_ when they are copied, so any folder hierarchies will be lost.
25
26 Listing folders with your static assets must be done before any of the methods described below.
27 When you define asset names in the methods described below, they generally assume paths that are _relative to this `_static` output folder_.
28
29 ## Define a list of assets in `conf.py`
30
31 The simplest way to add JS and CSS assets is to use [`html_css_files`](https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-html_css_files) and [`html_js_files`](https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-html_js_files) in your `conf.py` file.
32 Each can be a list of paths, _relative to your `html_static_path`_.
33 They will be added to the end of the `<head>` of your site.
34
35 For example:
36
37 ```{code-block} python
38 ---
39 caption: '`conf.py`'
40 ---
41
42 html_static_path = ["_static"]
43 html_css_files = ["mycss.css"]
44 html_js_files = ["myjs.js"]
45 ```
46
47 This will cause each to be linked in your `<head>`.
48
49 ## Add assets to your setup function
50
51 Additionally, you may add assets manually, to do so, use the `app` object in [the Sphinx `setup()` function](https://www.sphinx-doc.org/en/master/extdev/appapi.html#extension-setup).
52 The `app` object has two relevant methods here:
53
54 [**`app.add_css_file`**](https://www.sphinx-doc.org/en/master/extdev/appapi.html#sphinx.application.Sphinx.add_css_file) allows you to add CSS files directly.
55
56 [**`app.add_js_file`**](https://www.sphinx-doc.org/en/master/extdev/appapi.html#sphinx.application.Sphinx.add_js_file) allows you to add JS files directly.
57
58 Both of them expect you to add files **relative to the `html_static_path`**.
59
60 In addition, `app.add_js_file` allows you to add _raw JavaScript_ in addition to linking files (see example below).
61 For example:
62
63 ```{code-block} python
64 ---
65 caption: '`conf.py`'
66 ---
67
68 html_static_path = ["_static"]
69
70 def setup(app):
71 app.add_css_file("mycss.css")
72 app.add_js_file("myjs.js")
73
74 # Add raw JavaScript
75 rawjs = """
76 let a = "foo";
77 console.log(a + " bar")
78 """
79 app.add_js_file(None, body=rawjs)
80 ```
81
82 ## Use an event to add it to specific pages
83
84 If you'd like to use logic to only add a script to certain pages or to trigger different behavior depending on the page, use [a Sphinx event hook](https://www.sphinx-doc.org/en/master/extdev/appapi.html#sphinx-core-events).
85 This involves defining a function that runs when a particular event is emitted in the Sphinx build, and using [`app.connect()`](https://www.sphinx-doc.org/en/master/extdev/appapi.html#sphinx.application.Sphinx.connect) to connect it to your build.
86
87 The event you'll likely want to use is [`html-page-context`](https://www.sphinx-doc.org/en/master/extdev/appapi.html#event-html-page-context).
88 This is triggered just before the HTML for an _individual page_ is created.
89 If you run `app.add_js_file` or `app.add_css_file`, it will _only be added for that page_.
90
91 For example:
92
93 ```{code-block} python
94 ---
95 caption: '`conf.py`'
96 ---
97 html_static_path = ["_static"]
98
99 def add_my_files(app, pagename, templatename, context, doctree):
100 if pagename == "dontaddonthispage":
101 return
102
103 app.add_css_file("mycss.css")
104
105 def setup(app):
106 app.connect("html-page-context", add_my_files)
107 ```
108
109 ## Add it directly to the page content
110
111 Finally, you can add CSS or JS directly to a page's content.
112 If you're using reStructuredText you can use the `.. raw::` directive; if you're using MyST Markdown you can simply include the HTML content in-line with your Markdown-formatted content.
113
114 ``````{tab-set}
115 `````{tab-item} rST
116 ````{code-block} rst
117 :caption: some_page_in_my_site.rst
118 My title
119 ========
120
121 Some text
122
123 .. raw:: html
124
125 <style>
126 /* Make h2 bigger */
127 h2 {
128 font-size: 3rem;
129 }
130 </style>
131
132 A bigger title
133 --------------
134
135 Some other text
136 ````
137 `````
138 `````{tab-item} Markdown
139 ````{code-block} md
140 :caption: some_page_in_my_site.md
141 # My title
142
143 Some text
144
145 <style>
146 /* Make h2 bigger */
147 h2 {
148 font-size: 3rem;
149 }
150 </style>
151
152 ## A bigger title
153
154 Some other text
155 ````
156 `````
157 ``````