Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/app/news/+api-reference-search-titles.docs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lead API reference page titles with the public name, such as `rx.State API Reference`, so searching for `rx.State` finds its reference page.
24 changes: 21 additions & 3 deletions docs/app/reflex_docs/pages/docs/apiref.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,25 @@

from .env_vars import env_vars_doc


def _public_name(cls: type) -> str:
"""Return the name users reach a class by, such as ``rx.State``.

Search matches the identifier users type, so API reference titles lead
with it, like component titles do.

Args:
cls: A documented class.

Returns:
The ``rx`` or ``rx.event`` attribute path, or the bare class name.
"""
for namespace, prefix in ((rx, "rx"), (rx.event, "rx.event")):
if getattr(namespace, cls.__name__, None) is cls:
return f"{prefix}.{cls.__name__}"
return cls.__name__


pages = []
for module in modules:
name = module.__name__.lower()
Expand All @@ -72,10 +91,9 @@
f"/api-reference/{name}/", title, source_path=inspect.getsourcefile(module)
)(docs)
# Keep the short sidebar/nav label (e.g. "App"), but emit a descriptive HTML
# <title> for SEO. Use the real class name (e.g. "ComponentState") so it
# reads as a proper API symbol.
# <title> that leads with the public name (e.g. "rx.State") for search.
page_data.title = title
page_data.seo_title = f"{module.__name__} API Reference · Reflex Docs"
page_data.seo_title = f"{_public_name(module)} API Reference · Reflex Docs"
pages.append(page_data)

pages.append(env_vars_doc)
17 changes: 17 additions & 0 deletions docs/app/tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,3 +585,20 @@ def test_library_route_titles(routes_fixture, path, expected):
"""Published library pages carry the final title search engines see."""
titles = {route.path: route.seo_title or route.title for route in routes_fixture}
assert titles[path] == expected


@pytest.mark.parametrize(
("path", "expected"),
[
("/api-reference/app/", "rx.App API Reference · Reflex Docs"),
("/api-reference/state/", "rx.State API Reference · Reflex Docs"),
("/api-reference/var/", "rx.Var API Reference · Reflex Docs"),
("/api-reference/eventhandler/", "rx.EventHandler API Reference · Reflex Docs"),
("/api-reference/eventspec/", "rx.event.EventSpec API Reference · Reflex Docs"),
("/api-reference/statemanager/", "StateManager API Reference · Reflex Docs"),
],
)
def test_api_reference_route_titles(routes_fixture, path, expected):
"""API reference titles lead with the public name users type into search."""
titles = {route.path: route.seo_title or route.title for route in routes_fixture}
assert titles[path] == expected
Loading