Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addons: integrate with new beta addons flyout #1526

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c1ea98e
Addons: integrate with new beta addons flyout
humitos Sep 13, 2023
476abbd
`READTHEDOCS` variable has to be passed to the `html_context`
humitos Sep 13, 2023
09c6a18
Update code to match the latest changes
humitos Sep 20, 2023
bc09448
Use `.join` to avoid rendering the Array with `,`
humitos Sep 20, 2023
71c978e
Update code to use the v1 API structure response
humitos Apr 15, 2024
70f94e1
Don't auto-remove the original flyout
humitos Apr 15, 2024
f6edac2
Join the values to avoid strange `,`s
humitos Apr 15, 2024
04d7909
Support translations
humitos Apr 15, 2024
775addd
Remove HTML tags for testing
humitos Apr 15, 2024
e068ac2
Merge branch 'master' into humitos/addons-flyout
humitos Apr 15, 2024
cb78b36
Show "Search" section on flyout
humitos Apr 16, 2024
577f5a6
Render footer
humitos Apr 16, 2024
6140050
Add some styling
humitos Apr 16, 2024
92f65aa
TODO comments to work on
humitos Apr 16, 2024
7a743bb
add `div.injected`
humitos Apr 16, 2024
7671e07
Apply suggestions from code review
humitos Apr 17, 2024
356cd80
Translate privacy policy
humitos Apr 17, 2024
0033307
Add `.rtd-current-item` class to selected version/language
humitos Apr 17, 2024
ef0a57e
Refactor code to show sections only if there is content
humitos Apr 17, 2024
ecebe4c
Epub instead of EPUB for backward compatibility
humitos Apr 17, 2024
c805605
Show the modal when clicking on the "Search docs" from navbar
humitos Apr 24, 2024
131de0d
Remove privacy policy link from flyout
humitos May 23, 2024
53e32c6
Add UTM analytics
humitos May 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
range(1, 100)
))

if 'READTHEDOCS' in os.environ:
html_context['READTHEDOCS'] = True

html_logo = "demo/static/logo-wordmark-light.svg"
html_show_sourcelink = True
html_favicon = "demo/static/favicon.ico"
Expand Down
1 change: 1 addition & 0 deletions sphinx_rtd_theme/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<html class="writer-html5" lang="{{ lang_attr }}"{% if sphinx_version_info >= (7, 2) %} data-content_root="{{ content_root }}"{% endif %}>
<head>
<meta charset="utf-8" />
<meta name="readthedocs-addons-api-version" content="1">
{{- metatags }}
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
{%- block htmltitle %}
Expand Down
150 changes: 124 additions & 26 deletions sphinx_rtd_theme/versions.html
Original file line number Diff line number Diff line change
@@ -1,34 +1,132 @@
{% if READTHEDOCS %}
{# Add rst-badge after rst-versions for small badge style. #}
<div class="rst-versions" data-toggle="rst-versions" role="note" aria-label="{{ _('Versions') }}">
<span class="rst-current-version" data-toggle="rst-current-version">
<span class="fa fa-book"> Read the Docs</span>
v: {{ current_version }}
<span class="fa fa-caret-down"></span>
</span>
<div class="rst-other-versions">
<!-- This style is injected ONLY on Read the Docs and it's missing. -->
<style>
.rtd-current-item {
font-weight: bold;
}
</style>

<script type="text/javascript">
function renderLanguages(config) {
if (!config.projects.translations.length) {
return "";
}

const languagesHTML = `
<dl>
<dt>{{ _('Languages') }}</dt>
${ config.projects.translations.map(
(translation) => `
<dd ${ translation.slug == config.projects.current.slug ? 'class="rtd-current-item"' : '' }>
<a href="${ translation.urls.documentation }">${ translation.language.code }</a>
</dd>
`).join("\n")}
</dl>
`;
return languagesHTML;
}

function renderVersions(config) {
if (!config.versions.active.length) {
return "";
}
const versionsHTML = `
<dl>
<dt>{{ _('Versions') }}</dt>
{% for slug, url in versions %}
<dd><a href="{{ url }}">{{ slug }}</a></dd>
{% endfor %}
${ config.versions.active.map(
(version) => `
<dd ${ version.slug === config.versions.current.slug ? 'class="rtd-current-item"' : '' }>
<a href="${ version.urls.documentation }">${ version.slug }</a>
</dd>
`).join("\n")}
</dl>
`;
return versionsHTML;
}

function renderDownloads(config) {
if (!Object.keys(config.versions.current.downloads).length) {
return "";
}
const downloadsNameDisplay = {
pdf: "PDF",
epub: "Epub",
humitos marked this conversation as resolved.
Show resolved Hide resolved
htmlzip: "HTML",
};

const downloadsHTML = `
<dl>
<dt>{{ _('Downloads') }}</dt>
{% for type, url in downloads %}
<dd><a href="{{ url }}">{{ type }}</a></dd>
{% endfor %}
</dl>
<dl>
{# Translators: The phrase "Read the Docs" is not translated #}
<dt>{{ _('On Read the Docs') }}</dt>
<dd>
<a href="//{{ PRODUCTION_DOMAIN }}/projects/{{ slug }}/?fromdocs={{ slug }}">{{ _('Project Home') }}</a>
</dd>
${ Object.entries(config.versions.current.downloads).map(
([name, url]) => `
<dd>
<a href="//{{ PRODUCTION_DOMAIN }}/builds/{{ slug }}/?fromdocs={{ slug }}">{{ _('Builds') }}</a>
<a href="${ url }">${ downloadsNameDisplay[name] }</a>
</dd>
`).join("\n")}
</dl>
</div>
</div>
{% endif %}
`;
return downloadsHTML;
}

document.addEventListener("readthedocs-addons-data-ready", function(event) {
const config = event.detail.data();

const flyout = `
<div class="rst-versions" data-toggle="rst-versions" role="note">
<span class="rst-current-version" data-toggle="rst-current-version">
<span class="fa fa-book"> Read the Docs</span>
v: ${ config.versions.current.slug }
<span class="fa fa-caret-down"></span>
</span>
<div class="rst-other-versions">
<div class="injected">
${ renderLanguages(config) }
${ renderVersions(config) }
${ renderDownloads(config) }
<dl>
<dt>{{ _('On Read the Docs') }}</dt>
<dd>
<a href="${ config.projects.current.urls.home }">{{ _('Project Home') }}</a>
</dd>
<dd>
<a href="${ config.projects.current.urls.builds }">{{ _('Builds') }}</a>
</dd>
<dd>
<a href="${ config.projects.current.urls.downloads }">{{ _('Downloads') }}</a>
</dd>
</dl>
<dl>
<dt>{{ _('Search') }}</dt>
<dd>
<div style="padding: 6px;">
<form id="flyout-search-form">
<input
class="wy-form"
type="text"
name="q"
aria-label="{{ _('Search docs') }}"
placeholder="{{ _('Search docs') }}"
/>
</form>
</div>
</dd>
</dl>
<hr />
<small>
<span>Hosted by <a href="https://readthedocs.org">Read the Docs</a></span>
<span> &middot; </span>
<a href="https://docs.readthedocs.io/page/privacy-policy.html">{{ _('Privacy Policy') }}</a>
</small>
</div>
</div>
`;

// Inject the generated flyout into the body HTML element.
document.body.insertAdjacentHTML("beforeend", flyout);

// Trigger the Read the Docs Addons Search modal when clicking on the "Search" form from inside the flyout.
document.querySelector("#flyout-search-form").addEventListener("focusin", () => {
const event = new CustomEvent("readthedocs-search-show");
document.dispatchEvent(event);
});
});
</script>