sphinx-book-theme icon indicating copy to clipboard operation
sphinx-book-theme copied to clipboard

Force download of source files

Open afeld opened this issue 1 year ago • 3 comments

Context

See https://github.com/readthedocs/readthedocs.org/issues/11728 for the background. In short: When I click the .md or .ipynb download links, I would expect the files to be downloaded, rather than shown as plain text.

Proposal

While this can be set server-side using the Content-Disposition header, ReadTheDocs doesn't plan to do so. I imagine they are a common host for users of this theme, so that's a shame. Thankfully there's a workaround that can be implemented by the theme, using the download attribute on the anchor tags.

I glanced at the relevant source, and there wasn't a super-obvious place to add that attribute. I'm open to doing the work, but not sure when I can get to it. Would welcome guidance.

Thanks!

Tasks and updates

No response

afeld avatar Oct 31 '24 02:10 afeld

Bump!

afeld avatar Oct 17 '25 02:10 afeld

My workaround was to add it via JavaScript:

# _config.yml
sphinx:
  config:
    html_js_files:
      - download.js
// download.js
window.addEventListener("load", () => {
  const links = document.querySelectorAll(
    '.dropdown-download-buttons a[href$=".ipynb"]'
  );

  for (const link of links) {
    link.setAttribute("download", "");
  }
});

afeld avatar Oct 24 '25 21:10 afeld

Better yet, turns out there's an extension that handles this (including .md files)! https://github.com/TeachBooks/JupyterBook-Patches

afeld avatar Oct 25 '25 12:10 afeld