Skip to content

Commit

Permalink
Add option to make types bold
Browse files Browse the repository at this point in the history
This makes xrefs look a bit better because sphinx seems to automatically render
them in bold
  • Loading branch information
hoodmane committed Sep 28, 2023
1 parent 6b47806 commit 2a6c5c0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions sphinx_js/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def setup(app: Sphinx) -> None:
)
app.add_config_value("jsdoc_config_path", default=None, rebuild="env")
app.add_config_value("ts_type_xref_formatter", None, "env")
app.add_config_value("ts_type_bold", False, "env")
app.add_config_value("ts_should_destructure_arg", None, "env")
app.add_config_value("ts_post_convert", None, "env")

Expand Down
21 changes: 20 additions & 1 deletion sphinx_js/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class JsRenderer:
_renderer_type: Literal["function", "class", "attribute"]
_template: str
_type_xref_formatter: Callable[[TypeXRef], str]
_type_text_formatter: Callable[[str], str]
_partial_path: list[str]
_explicit_formal_params: str
_content: list[str]
Expand All @@ -74,7 +75,18 @@ def _set_type_xref_formatter(
def default_type_xref_formatter(xref: TypeXRef) -> str:
return xref.name

self._type_xref_formatter = default_type_xref_formatter

def _set_type_text_formatter(
self, formatter: Callable[[Config, str], str] | None
) -> None:
if formatter:
self._type_text_formatter = partial(formatter, self._app.config)
return

def default_type_text_formatter(text: str) -> str:
return text

self._type_text_formatter = default_type_text_formatter

def __init__(
self,
Expand All @@ -91,6 +103,11 @@ def __init__(
self._directive = directive
self._app = app
self._set_type_xref_formatter(app.config.ts_type_xref_formatter)
if app.config.ts_type_bold:
self._set_type_text_formatter(lambda conf, text: "**" + text + "**")
else:
self._set_type_text_formatter(None)


# content, arguments, options, app: all need to be accessible to
# template_vars, so we bring them in on construction and stow them away
Expand Down Expand Up @@ -301,6 +318,8 @@ def strs() -> Iterator[str]:
while True:
xref: list[TypeXRef] = []
s = "".join(strs())
if s:
s = self._type_text_formatter(s)
if escape:
s = rst.escape(s)
if s:
Expand Down

0 comments on commit 2a6c5c0

Please sign in to comment.