Skip to content

Commit 628f7f2

Browse files
authored
ENH: Allowing inline elements in sidenotes and marginnotes (#641)
* changes in _transforms to accomodate other nodes * modifying test * correcting marginnote test
1 parent 30be6df commit 628f7f2

File tree

4 files changed

+34
-32
lines changed

4 files changed

+34
-32
lines changed

src/sphinx_book_theme/_transforms.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,30 @@ def run(self, **kwargs: Any) -> None:
3131
):
3232
parent = foot_node.parent
3333
# second children of footnote node is the content text
34-
text = foot_node.children[1].astext()
34+
foot_node_content = foot_node.children[1].children
3535

3636
sidenote = SideNoteNode()
3737
para = docutil_nodes.inline()
3838
# first children of footnote node is the label
3939
label = foot_node.children[0].astext()
4040

41-
if text.startswith("{-}"):
41+
if foot_node_content[0].astext().startswith("{-}"):
4242
# marginnotes will have content starting with {-}
4343
# remove the number so it doesn't show
4444
para.attributes["classes"].append("marginnote")
45-
para.append(docutil_nodes.Text(text.replace("{-}", "")))
45+
foot_node_content[0] = docutil_nodes.Text(
46+
foot_node_content[0].replace("{-}", "")
47+
)
48+
para.children = foot_node_content
4649

4750
sidenote.attributes["names"].append(f"marginnote-role-{label}")
4851
else:
4952
# sidenotes are the default behavior if no {-}
5053
# in this case we keep the number
5154
superscript = docutil_nodes.superscript("", label)
5255
para.attributes["classes"].append("sidenote")
53-
para.extend([superscript, docutil_nodes.Text(text)])
56+
parachildren = [superscript] + foot_node_content
57+
para.children = parachildren
5458

5559
sidenote.attributes["names"].append(f"sidenote-role-{label}")
5660
sidenote.append(superscript)

tests/sites/base/page2.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ Some text to test out one sidenote[^myref] and another sidenote[^3]
77

88
[^myref]: This is a sidenote.
99

10-
[^3]: This is another sidenote.
10+
[^3]: This is *another* **sidenote**.
1111

1212
## Marginnotes
1313

1414
Some text to test out one marginnote[^somemargin] and another marginnote[^6]
1515

16-
[^somemargin]: {>} This is a marginnote.
17-
[^6]: {>} This is another marginnote.
16+
[^somemargin]: {-} This is a marginnote.
17+
[^6]: {-} This is *another* **marginnote**.
1818

1919
This is the end of Page 2.

tests/test_build/test_marginnote.html

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,25 @@ <h2>
1010
</h2>
1111
<p>
1212
Some text to test out one marginnote
13-
<label class="margin-toggle" for="sidenote-role-2">
14-
<span id="id3">
15-
<sup>
16-
2
17-
</sup>
18-
</span>
13+
<label class="margin-toggle marginnote-label" for="marginnote-role-2">
1914
</label>
20-
<input class="margin-toggle" id="sidenote-role-2" name="sidenote-role-2" type="checkbox"/>
21-
<span class="sidenote">
22-
<sup>
23-
2
24-
</sup>
25-
{&gt;} This is a marginnote.
15+
<input class="margin-toggle" id="marginnote-role-2" name="marginnote-role-2" type="checkbox"/>
16+
<span class="marginnote">
17+
This is a marginnote.
2618
</span>
2719
and another marginnote
28-
<label class="margin-toggle" for="sidenote-role-6">
29-
<span id="id4">
30-
<sup>
31-
6
32-
</sup>
33-
</span>
20+
<label class="margin-toggle marginnote-label" for="marginnote-role-6">
3421
</label>
35-
<input class="margin-toggle" id="sidenote-role-6" name="sidenote-role-6" type="checkbox"/>
36-
<span class="sidenote">
37-
<sup>
38-
6
39-
</sup>
40-
{&gt;} This is another marginnote.
22+
<input class="margin-toggle" id="marginnote-role-6" name="marginnote-role-6" type="checkbox"/>
23+
<span class="marginnote">
24+
This is
25+
<em>
26+
another
27+
</em>
28+
<strong>
29+
marginnote
30+
</strong>
31+
.
4132
</span>
4233
</p>
4334
<p>

tests/test_build/test_sidenote.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,14 @@ <h2>
3737
<sup>
3838
3
3939
</sup>
40-
This is another sidenote.
40+
This is
41+
<em>
42+
another
43+
</em>
44+
<strong>
45+
sidenote
46+
</strong>
47+
.
4148
</span>
4249
</p>
4350
</section>

0 commit comments

Comments
 (0)