Here is a solution using tex4ht. I've modified your example slightly to provide definitions for thm and collapse environments:
% https://tex.stackexchange.com/q/467746/2891
\documentclass{article}
\usepackage{amsthm}
\newtheorem{thm}{Theorem}
\newenvironment{collapse}{}{}
\begin{document}
\begin{thm}
blah blah blah
\end{thm}
\begin{collapse}
\begin{proof}
First step is true.
\begin{collapse}
First step is true because blah blah blah.
\end{collapse}
Second step is true.
Final step is true.
\end{proof}
\end{collapse}
\end{document}
The JavaScript code should be put in the collapse.js file:
$('[data-toggle]').on('click', function(){
var id = $(this).data("toggle"),
$object = $("#" + id),
className = "collapse";
if ($object) {
if ($object.hasClass(className)) {
$object.removeClass(className)
$(this).text("Collapse");
} else {
$object.addClass(className)
$(this).text("Expand");
}
}
});
Lastly, we must configure tex4ht to include all the JavaScript and to produce a usable code for the collapse environment. Put the following code in the myconfig.cfg file:
\Preamble{xhtml}
\newcounter{collapseid}
\ConfigureEnv{collapse}{%
\stepcounter{collapseid}
\edef\currentid{collapse-\arabic{collapseid}}
\ifvmode\IgnorePar\fi\EndP\HCode{<div class="collapse-label"><a data-toggle="\currentid" href="\#">Expand</a></div>\Hnewline<div id="\currentid" class="collapse">}%
\par
}
{\ifvmode\IgnorePar\fi\EndP\HCode{</div>}\par}{}{}
\Css{.collapse{display:none;}}
\Css{.open{display:block;}}
\Configure{@/BODY}{\HCode{<script class="cssdeck" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>}}
\Configure{@/BODY}{\HCode{<script type="text/javascript" src="collapse.js"></script>}}
\begin{document}
\EndPreamble
The \ConfigureEnv{collapse} command configures HTML code inserted to the document. It also produces unique id attribute for each of the collapsed environments and handles paragraph endings. The \Css command is used for the necessary CSS configurations and the \Configure{@/BODY} commands are used to insert the requests for the Java Script files at the end of the page.
The document can be compiled using the following commnad:
make4ht -c myconfig.cfg filename.tex
This is the result (without the HTML header):
<div class="newtheorem">
<!--l. 12--><p class="noindent" ><span class="head">
<a
id="x1-2r1"></a>
<span
class="cmbx-10">Theorem 1.</span> </span><span
class="cmti-10">blah blah blah</span>
</p>
</div>
<div class="collapse-label"><a data-toggle="collapse-1" href="#">Expand</a></div>
<div id="collapse-1" class="collapse">
<!--l. 16--><p class="indent" >
</p>
<div class="proof">
<!--l. 17--><p class="indent" > <span class="head">
<span
class="cmti-10">Proof.</span> </span>First step is true. </p><div class="collapse-label"><a data-toggle="collapse-2" href="#">Expand</a></div>
<div id="collapse-2" class="collapse">
<!--l. 19--><p class="indent" > First step is true because blah blah blah.
</p></div>
<!--l. 21--><p class="indent" > Second step is true. Final step is true. □
</p>
</div>
</div>
<script class="cssdeck" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script><script type="text/javascript" src="collapse.js"></script>
</body>
</html>
Here is a interactive example
tex4htcan do this. We need a sample document and more concrete specification what should be collapsed, then it will be possible to provide a concrete solution. – michal.h21 Dec 29 '18 at 09:48