This is a procedure that could be handled using a custom Sphinx writer instead. If we want to switch the theme to always using the HTML5 writer, regardless of Sphinx version, we can extend this writer to output structures we're expecting in the JS, instead of hacking it into the output afterwards.
I don't have public code to show the POC, but noting this issue for later.
We are currently altering the output slightly using JS after the build:
https://github.com/readthedocs/sphinx_rtd_theme/blob/bad21120c8d12b4dd15c6b5383dd3fe5909def56/js/theme.js#L101-L110
This is a procedure that could be handled using a custom Sphinx writer instead. If we want to switch the theme to always using the HTML5 writer, regardless of Sphinx version, we can extend this writer to output structures we're expecting in the JS, instead of hacking it into the output afterwards.
I don't have public code to show the POC, but noting this issue for later.