Skip to content

Extension template doesn't have access to updated HTML context? #7086

@choldgraf

Description

@choldgraf

I am developing an extension that includes some Javascript that is configurable based on the user's configuration input. The user should configure the extension with a key in conf.py, and then the value for this key needs to be inserted into a javascript file in the _static directory of the extension. Here's what I'm doing:

First I've defined a function that updates the context based on a config value.

def add_to_context(app, pagename, templatename, context, doctree):
    context['copybutton_skip_text'] = app.config['copybutton_skip_text']

Then I've added a new configuration key and connected the function to update
the HTML context:

def setup(app):
    app.add_config_value("copybutton_skip_text", ">>> ", "html")
    app.connect("html-page-context", add_to_context)

Finally, in my extension, I have a "templatable" javascript file (say, myjavascript.js_t). Inside I am trying to insert the variable like so

var skipText = "{{ copybutton_skip_text }}";

However, in the final rendered js file (myjavascript.js), the result is
always empty (e.g., var skipText = "") no matter how the value is configured.

I can confirm that default HTML context keys are inserted properly (e.g. docstitle)
so it seems like the HTML context update step isn't in effect when the extension JS file is being run through the template system.

Any idea why this would be? Am I doing things in the incorrect order or something?

If it's helpful, here's the PR where I am trying this out: https://github.com/choldgraf/sphinx-copybutton/pull/58

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions