Skip to content

Commit

Permalink
sphinx-agent: Use only builtin nodes for line numbers
Browse files Browse the repository at this point in the history
To remove the possibility of conflicting with extensions that introduce
custom doctree nodes, the sphinx agent will only consider nodes defined
in `docutils.nodes` or `sphinx.addnodes` when injecting line numbers
  • Loading branch information
alcarney committed Aug 24, 2024
1 parent b875c03 commit 12c9bd9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/esbonio/changes/874.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Esbonio's preview generation should no longer conflict with extensions like `ablog` which call methods like `replac_self()` on custom doctree nodes.
11 changes: 11 additions & 0 deletions lib/esbonio/esbonio/sphinx_agent/handlers/webview.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@


STATIC_DIR = (pathlib.Path(__file__).parent.parent / "static").resolve()
ALLOWED_MODULES = {"docutils.nodes", "sphinx.addnodes"}


def has_source(node):
Expand All @@ -31,6 +32,16 @@ def has_source(node):
if isinstance(node, addnodes.toctree) and version_info[0] < 7:
return False

# It's not only limited to `toctreenodes`!
#
# The identical error is thrown when using esbonio with the `ablog` extension
# See: https://github.com/swyddfa/esbonio/issues/874
#
# I think for now, the safest approach is to only handle nodes defined by Sphinx or
# docutils.
if node.__module__ not in ALLOWED_MODULES:
return False

return (node.line or 0) > 0 and node.source is not None


Expand Down

0 comments on commit 12c9bd9

Please sign in to comment.