Skip to content

Commit 5bc613e

Browse files
committed
📚 Add design discussion
1 parent 8c600f8 commit 5bc613e

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

docs/index.rst

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,78 @@ This allows you to customize the CSS of the target document, e.g. to hide header
7676
display: none;
7777
}
7878
79+
Design Discussion
80+
-----------------
81+
82+
It can be desirable to show previews to referenced content, without navigating away from the current page.
83+
84+
This feature is not usually provided by the browser itself,
85+
and Sphinx extensions also have other constraints:
86+
87+
- Sphinx documentation is usually built as static HTML files, so any dynamic modifications to the page must be done on the front-end (in the browser), using JavaScript.
88+
- Front-end Javascript can only access same-origin_ resources, or resources that explicitly allows cross-origin resource sharing (CORS_).
89+
- It is desirable for the extension to be able to work with any theme, without requiring modifications to the theme itself
90+
- It is desirable for the previews to work for different types of references, even ones created by other extensions
91+
92+
.. note::
93+
94+
If opening a HTML file directly in the browser, from the file system,
95+
other local files will be considered cross-origin, and will not be accessible to the extension.
96+
To make the local files same-origin, a simple HTTP server can be used, e.g.::
97+
98+
cd docs/_build/html
99+
python -m http.server
100+
101+
Then, the documentation can be accessed at ``http://localhost:8000``.
102+
103+
To show preview content, the following options could be considered:
104+
105+
1. Use an iframe to load the target URL, and show it in a modal window.
106+
107+
This is probably the simplest option, and is the one currently implemented here.
108+
109+
| It does not require much build-time processing ✅
110+
| It will work for any URL (same-origin or cross-origin) ✅
111+
| It will work with any theme (since the CSS/JS is sand-boxed) ✅
112+
| It allows for following nested previews ✅
113+
| However, it will need to load the entire page,
114+
which can be slow and will clutter the preview with unnecessary artefacts, like the page header ❌
115+
| Some browsers (like Chrome) appear to not handle loading a URL like ``page.html#id`` with the target id correctly in view within the iframe ❌
116+
117+
The loaded document, within the iframe, is only available to be modified with Javascript if it is same-origin or CORS enabled.
118+
If same-origin, then the issues can be mitigated with Javascript / CSS,
119+
120+
- by hiding unnecessary elements (e.g. header and footer) in the target document
121+
- by scrolling to the target element (e.g. ``#id``) within the target document
122+
123+
2. Identify all the previewable content at build time, and allow it to be accessed by the reference page(s).
124+
125+
| This allows for a more efficient preview, since only the relevant content needs to be shown ✅
126+
| The content must "work" with the referencing page's CSS / Javascript though ❌
127+
| Also any local references (like links and images) may need to be modified to be relative to the referencing page ❌
128+
| If there are a lot of previewable content, then this can increase the size and time of the build ❌
129+
| The implementation can be tricky to implement into the Sphinx build flow, since we need to gather the previewable content from all the pages, and then make it available to the referencing pages
130+
131+
This is done in `sphinx-tippy <https://github.com/sphinx-extensions2/sphinx-tippy>`__, but is not applicable to all use cases.
132+
133+
3. Use a server-side proxy to load the target URL content on the front-end
134+
135+
| The primary disadvantage is that it requires the documentation to be hosted on a server that supports the proxy, and will not work locally ❌
136+
137+
This is done in `sphinx-hoverxref <https://github.com/readthedocs/sphinx-hoverxref>`__, which only works when the documentation is hosted on ReadTheDocs, and also suffers in complexity and applicability the same as (2).
138+
139+
Positioning the preview window
140+
..............................
141+
142+
Currently, the preview window is positioned/sized relative to the reference preview icon and page window, via custom JavaScript, which is triggered on page load and on window resize or scroll.
143+
This appears to work fine, although may not work as well with asynchronous panning (see `this discussion <https://firefox-source-docs.mozilla.org/performance/scroll-linked_effects.html>`__).
144+
145+
One could also look to use an existing library, like `@popperjs/core <https://www.npmjs.com/package/@popperjs/core>`__ (now called Floating UI).
146+
If this is done, it might be nice to have the option of bundling the library with the extension, rather than requiring it to be downloaded on page load.
147+
148+
In the future, it may be possible to use the `CSS Anchor API <https://drafts.csswg.org/css-anchor-position-1/>`__.
149+
150+
79151
More Examples
80152
-------------
81153

0 commit comments

Comments
 (0)