Skip to content

Commit 672ece5

Browse files
Copilothumitosericholscher
authored
Docs: Add documentation for CustomScript addon (#12623)
- [x] Understand the existing addons documentation structure - [x] Review the CustomScript addon feature implementation and usage - [x] Create documentation for the CustomScript addon in dedicated `docs/user/custom-script.rst` file - [x] Document the `window.ReadTheDocsEventData` flag for accessing addons data - [x] Add information about contacting support to enable the feature - [x] Include an example of how to use this feature - [x] Build and verify the documentation renders correctly - [x] Run code review on documentation changes - [x] Run security scanning (no code changes detected) - [x] Address PR feedback: add Custom Script to initial features list in `addons.rst` - [x] Address PR feedback: update code comments for better clarity - [x] Address PR feedback: move Custom Script documentation to separate file following addon documentation pattern <!-- START COPILOT CODING AGENT SUFFIX --> <details> <summary>Original prompt</summary> > > ---- > > *This section details on the original issue you should resolve* > > <issue_title>Addons: add documentation for `CustomScript`</issue_title> > <issue_description>We need to document this addon and put some examples on how to solve common issues with this feature. > > * Refs: #11758 > * Requires: #11774 > * Requires: readthedocs/ext-theme#527 > * Requires: https://github.com/readthedocs/addons/pull/474</issue_description> > > <agent_instructions>We want to document this feature, even it's not exposed in the UI. Tell people to contact support if they want to make usage of this feature. > > Explain the flag we expose in `window` object. > > Mention an example of what this feature is useful for.</agent_instructions> > > ## Comments on the Issue (you are @copilot in this section) > > <comments> > <comment_new><author>@ericholscher</author><body> > We actually aren't exposing this to users -- should we, and write docs for it? </body></comment_new> > <comment_new><author>@humitos</author><body> > Yeah. I think it's fine to have small docs for this addon. It's not polished/perfect, but it's useful. We can add the other features mentioned in the description of the issue in the following iteration when more people are using it and requesting for upgrades.</body></comment_new> > </comments> > </details> - Fixes #11775 <!-- START COPILOT CODING AGENT TIPS --> --- 💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more [Copilot coding agent tips](https://gh.io/copilot-coding-agent-tips) in the docs. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: humitos <244656+humitos@users.noreply.github.com> Co-authored-by: ericholscher <25510+ericholscher@users.noreply.github.com> Co-authored-by: Manuel Kaufmann <humitos@gmail.com>
1 parent 19f8bfe commit 672ece5

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

docs/user/addons.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ and can be accessed via hotkeys or on screen UI elements.
3232
:doc:`Search analytics </search-analytics>`
3333
Understand what your users are searching for
3434

35+
:doc:`Custom script </custom-script>`
36+
Inject custom JavaScript into your documentation
37+
3538
Configuring Read the Docs Addons
3639
--------------------------------
3740

docs/user/custom-script.rst

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
Custom script
2+
=============
3+
4+
The Custom Script addon allows you to inject a custom JavaScript file into your documentation at serve time.
5+
This enables you to modify or enhance frozen documentation without rebuilding it.
6+
7+
.. note::
8+
9+
The Custom Script addon is not currently exposed in the user interface.
10+
If you would like to use this feature,
11+
please :doc:`contact support </support>`.
12+
13+
Using custom scripts
14+
--------------------
15+
16+
This addon allows specifying a URL to a JavaScript file that will be injected into all pages of your documentation.
17+
The script can be hosted on Read the Docs itself (as a relative URL) or on an external server (as an absolute URL).
18+
19+
Common use cases for custom scripts include:
20+
21+
* Fixing bugs or adding features to frozen documentation
22+
* Injecting analytics or tracking code
23+
* Customizing the appearance or behavior of specific documentation versions
24+
25+
Accessing Addons data from your script
26+
--------------------------------------
27+
28+
Custom scripts are loaded asynchronously after the initial page load,
29+
which means the ``readthedocs-addons-data-ready`` event has already been fired by the time your script executes.
30+
To access the Addons data from your custom script,
31+
check the ``window.ReadTheDocsEventData`` object first,
32+
then subscribe to the event for future updates (for example, when the URL changes in a single-page application):
33+
34+
.. code-block:: javascript
35+
36+
function handleReadTheDocsData(data) {
37+
// Access the Addons data here
38+
console.log("Project slug:", data.projects.current.slug);
39+
40+
// You can filter by version to apply changes selectively
41+
if (data.versions.current.slug === "v3.0") {
42+
// Do something specific for version v3.0
43+
}
44+
}
45+
46+
// The event "readthedocs-addons-data-ready" has been already fired when this script is run.
47+
// We need to check for `window.ReadTheDocsEventData` first, and if it's available
48+
// use that data to call the handler.
49+
if (window.ReadTheDocsEventData !== undefined) {
50+
handleReadTheDocsData(window.ReadTheDocsEventData.data());
51+
}
52+
53+
// After that, we subscribe to the Read the Docs Addons event to access data
54+
// on future dispatchs (e.g. when a URL changes on a SPA)
55+
document.addEventListener("readthedocs-addons-data-ready", function (event) {
56+
handleReadTheDocsData(event.detail.data());
57+
});

docs/user/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Read the Docs: documentation simplified
6565
/server-side-search/index
6666
/server-side-search/syntax
6767
/flyout-menu
68+
/custom-script
6869

6970
.. toctree::
7071
:maxdepth: 2

0 commit comments

Comments
 (0)