Skip to content

Commit

Permalink
sphinx-agent: Inline webview.js into build files
Browse files Browse the repository at this point in the history
This is more robust than trying to compute a relative path between
esbonio's install location and the target project - especially when
you consider issues like separate Windows paritions
  • Loading branch information
alcarney committed Jun 3, 2024
1 parent 805825f commit c865c78
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 26 deletions.
1 change: 1 addition & 0 deletions lib/esbonio/changes/810.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The server should no longer throw path mount errors when used across partitions on Windows
24 changes: 6 additions & 18 deletions lib/esbonio/esbonio/sphinx_agent/handlers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import inspect
import logging
import os.path
import pathlib
import sys
import traceback
Expand Down Expand Up @@ -125,8 +124,7 @@ def create_sphinx_app(self, request: types.CreateApplicationRequest):
# TODO: Sphinx 7.x has introduced a `include-read` event
# See: https://github.com/sphinx-doc/sphinx/pull/11657

if request.params.enable_sync_scrolling:
_enable_sync_scrolling(self.app)
_enable_sync_scrolling(self.app)

response = types.CreateApplicationResponse(
id=request.id,
Expand Down Expand Up @@ -199,21 +197,11 @@ def _enable_sync_scrolling(app: Sphinx):
"""Given a Sphinx application, configure it so that we can support syncronised
scrolling."""

# On OSes like Fedora Silverblue where `/home` is a symlink for `/var/home`
# we could have a situation where `STATIC_DIR` and `app.confdir` have
# different root dirs... which is enough to cause `os.path.relpath` to return
# the wrong path.
# Inline the JS code we need to enable sync scrolling.
#
# Fully resolving both `STATIC_DIR` and `app.confdir` should be enough to
# mitigate this.
confdir = pathlib.Path(app.confdir).resolve()
# Yes this "bloats" every page in the generated docs, but is generally more robust
# see: https://github.com/swyddfa/esbonio/issues/810
webview_js = STATIC_DIR / "webview.js"
app.add_js_file(None, body=webview_js.read_text())

# Push our folder of static assets into the user's project.
# Path needs to be relative to their project's confdir.
reldir = os.path.relpath(str(STATIC_DIR), start=str(confdir))
app.config.html_static_path.append(reldir)

app.add_js_file("webview.js")

# Inject source line numbers into build output
app.add_transform(LineNumberTransform)
17 changes: 9 additions & 8 deletions lib/esbonio/tests/sphinx-agent/test_sa_build.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import pathlib
import re
import sys

import pytest
Expand All @@ -19,25 +18,27 @@
from esbonio.server.features.sphinx_manager.config import SphinxConfig

logger = logging.getLogger(__name__)
STATIC_DIR = (
pathlib.Path(__file__).parent.parent.parent / "esbonio" / "sphinx_agent" / "static"
).resolve()


@pytest.mark.asyncio
async def test_build_includes_webview_js(client: SubprocessSphinxClient, uri_for):
"""Ensure that builds include the ``webview.js`` script."""

out = client.build_uri
src = client.src_uri
assert out is not None and src is not None
assert out is not None

# Ensure the script is included in the build output
webview_js = pathlib.Path(out / "_static" / "webview.js")
webview_js = STATIC_DIR / "webview.js"
assert webview_js.exists()
assert "editor/scroll" in webview_js.read_text()

webview_script = webview_js.read_text()
assert "editor/scroll" in webview_script

# Ensure the script is included in the page
index_html = pathlib.Path(out / "index.html")
pattern = re.compile(r'<script src="_static/webview.js(\?v=[\w]+)?"></script>')
assert pattern.search(index_html.read_text()) is not None
assert webview_script in index_html.read_text()


@pytest.mark.asyncio
Expand Down

0 comments on commit c865c78

Please sign in to comment.