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

🔧 MAINT: Update path function for static asset #129

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions sphinx_copybutton/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ def add_to_context(app, config):
)


def add_static_path_to_js(app, pagename, templatename, context, doctree):
"""Define the static path to the copybutton image in JS for use in the .js file."""
copybutton_image_full_path = context["pathto"](
"_static/" + app.config.copybutton_image_path, 1
)
image_js = f"const copybutton_image_path = '{copybutton_image_full_path}'"
app.add_js_file(None, body=image_js)


def setup(app):

logger.verbose("Adding copy buttons to code blocks...")
Expand All @@ -60,6 +69,9 @@ def setup(app):
# Add configuration value to the template
app.connect("config-inited", add_to_context)

# Add variables we'll need in the JS file
app.connect("html-page-context", add_static_path_to_js)

# Add relevant code to headers
app.add_css_file("copybutton.css")
app.add_js_file("clipboard.min.js")
Expand Down
2 changes: 1 addition & 1 deletion sphinx_copybutton/_static/copybutton.js_t
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const addCopyButtonToCodeCells = () => {

const clipboardButton = id =>
`<a class="copybtn o-tooltip--left" style="background-color: ${pre_bg}" data-tooltip="${messages[locale]['copy']}" data-clipboard-target="#${id}">
<img src="${DOCUMENTATION_OPTIONS.URL_ROOT}_static/{{ copybutton_image_path }}" alt="${messages[locale]['copy_to_clipboard']}">
<img src="${copybutton_image_path}" alt="${messages[locale]['copy_to_clipboard']}">
</a>`
codeCell.insertAdjacentHTML('afterend', clipboardButton(id))
})
Expand Down