Wholly adapted from my answer here: Extracting the structure of a LaTex document, including comments
Your document, here mydoc.tex
\documentclass{article}
\newcommand\addxyzline[2]{\addtocontents {xyz}{\protect \xyzline {#1}{#2}}}
\makeatletter
\newcommand\writexyz{\newcommand\xyzline[2]{}\@starttoc{xyz}}
\makeatother
\newcommand\Subsection[1]{\subsection{#1}\addxyzline{Subsection}{#1}}
\newcommand\Subsubsection[1]{\subsubsection{#1}\addxyzline{Subsubsection}{#1}}
\newcommand\foo[1]{#1\addxyzline{par}{#1}}
\begin{document}
%\tableofcontents% CAN UNCOMMMENT TO SEE THAT toc WORKS FINE
\Subsection{Stuff}
some text.
\foo{Inside Text.}
Some text again.
\Subsubsection{Another stuff}
blah blah.
\foo{Another inside text.}
blah-blah.
\writexyz
\end{document}
Compile it to produce
Via the \writexyz
which concludes the document, it also produces a file mydoc.xyz
containing:
\xyzline {Subsection}{Stuff}
\xyzline {par}{Inside Text.}
\xyzline {Subsubsection}{Another stuff}
\xyzline {par}{Another inside text.}
which is used by the following stencil.
Second stencil file:
\documentclass{article}
\newcommand\xyzline[2]{\csname#1\endcsname {#2}\par}
\newcommand\Subsection[1]{\medskip**#1**}
\newcommand\Subsubsection[1]{\medskip** #1 **}
\begin{document}
\input{mydoc.xyz}% NAME OF .xyz FILE GOES HERE IN THE ARGUMENT
\end{document}
Compile it to produce