Skip to content

Commit 8ec7204

Browse files
authored
✨ NEW: Add ref-type option to button-ref directive (#19)
1 parent b258e89 commit 8ec7204

File tree

6 files changed

+39
-10
lines changed

6 files changed

+39
-10
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ subtitle for card (see <https://material.io/components/cards#anatomy>)
7474

7575
rtd PRs not working
7676

77-
octicon in button text shown as raw HTML
78-
7977

8078
[github-ci]: https://github.com/executablebooks/sphinx-design/workflows/continuous-integration/badge.svg?branch=main
8179
[github-link]: https://github.com/executablebooks/sphinx-design

docs/badges_buttons.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,25 @@ Reference Button text
108108
````
109109
`````
110110

111+
Note that by default sphinx converts the content of references to raw text.
112+
For example `**Bold text**` with `ref-type` set to `ref` will be rendered without bold:
113+
114+
```{button-ref} buttons
115+
:ref-type: ref
116+
:color: primary
117+
118+
**Bold text**
119+
```
120+
121+
However, if using [myst-parser](https://myst-parser.readthedocs.io/), you can set the `ref-type` to `myst`, and the content will be properly rendered:
122+
123+
```{button-ref} buttons
124+
:ref-type: myst
125+
:color: primary
126+
127+
**Bold text**
128+
```
129+
111130
Use the `click-parent` option to make the button's parent container also clickable.
112131

113132
:::{card} Card with an expanded button
@@ -124,6 +143,9 @@ See the [Material Design](https://material.io/components/buttons) and [Bootstrap
124143

125144
### `button-link` and `button-ref` options
126145

146+
ref-type (`button-ref` only)
147+
: Type of reference to use; `any` (default), `ref`, `doc`, or `myst`
148+
127149
color
128150
: Set the color of the button (background and font).
129151
One of the semantic color names: `primary`, `secondary`, `success`, `danger`, `warning`, `info`, `light`, `dark`, `muted`.
@@ -132,7 +154,7 @@ outline
132154
: Outline color variant
133155

134156
align
135-
: Align the button on the page; left, right, center or justify
157+
: Align the button on the page; `left`, `right`, `center` or `justify`
136158

137159
expand
138160
: Expand to fit parent width

docs/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ sd_hide_title: true
2929
A sphinx extension for designing beautiful, screen-size responsive web-components.
3030

3131
```{button-ref} get_started
32+
:ref-type: doc
3233
:outline:
3334
:color: white
34-
:class: sd-fs-5
35+
:class: sd-px-4 sd-fs-5
3536
3637
Get Started
3738
```

sphinx_design/badges_buttons.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ class _ButtonDirective(SphinxDirective):
144144
"click-parent": directives.flag,
145145
"tooltip": directives.unchanged_required,
146146
"shadow": directives.flag,
147+
# ref button only
148+
"ref-type": make_choice(["any", "ref", "doc", "myst"]),
147149
"class": directives.class_option,
148150
}
149151

@@ -174,13 +176,16 @@ def run(self) -> List[nodes.Node]:
174176
self.set_source_info(node)
175177
if "tooltip" in self.options:
176178
node["reftitle"] = self.options["tooltip"] # TODO escape HTML
179+
177180
if self.content:
178181
textnodes, _ = self.state.inline_text(
179182
"\n".join(self.content), self.lineno + self.content_offset
180183
)
181-
node.extend(textnodes)
184+
content = nodes.inline("", "")
185+
content.extend(textnodes)
182186
else:
183-
node += nodes.inline(target, target)
187+
content = nodes.inline(target, target)
188+
node.append(content)
184189

185190
if "expand" in self.options:
186191
grid_container = nodes.inline(classes=["sd-d-grid"])
@@ -217,13 +222,14 @@ def create_ref_node(
217222
self, rawtext: str, target: str, explicit_title: bool, classes: List[str]
218223
) -> nodes.Node:
219224
"""Create the reference node."""
225+
ref_type = self.options.get("ref-type", "any")
220226
options = {
221227
# TODO the presence of classes raises an error if the link cannot be found
222228
"classes": classes,
223229
"reftarget": target,
224230
"refdoc": self.env.docname,
225-
"refdomain": "",
226-
"reftype": "any", # TODO allow for variable ref type
231+
"refdomain": "std" if ref_type in {"ref", "doc"} else "",
232+
"reftype": ref_type,
227233
"refexplicit": explicit_title,
228234
"refwarn": True,
229235
}

tests/test_snippets/snippet_post_button-link.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
https://example.com
99
<paragraph>
1010
<reference classes="sd-sphinx-override sd-btn sd-text-wrap" refuri="https://example.com">
11-
Button text
11+
<inline>
12+
Button text
1213
<paragraph>
1314
<reference classes="sd-sphinx-override sd-btn sd-text-wrap sd-btn-primary sd-shadow-sm" refuri="https://example.com">
1415
<inline>

tests/test_snippets/snippet_pre_button-link.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
https://example.com
99
<paragraph>
1010
<reference classes="sd-sphinx-override sd-btn sd-text-wrap" refuri="https://example.com">
11-
Button text
11+
<inline>
12+
Button text
1213
<paragraph>
1314
<reference classes="sd-sphinx-override sd-btn sd-text-wrap sd-btn-primary sd-shadow-sm" refuri="https://example.com">
1415
<inline>

0 commit comments

Comments
 (0)