I'm trying to modify https://tex.stackexchange.com/a/399944/2066 to adjust the spacing after sections headings which are not immediately followed by other section headings. However, if I replace the \hrule\vspace{1ex} with \vspace{-\baselineskip} in that answer, using the code
\documentclass{article}
\usepackage{lipsum}
\makeatletter
\def\@afterheading{%
\@nobreaktrue
\everypar{%
\if@nobreak
\@nobreakfalse
\clubpenalty \@M
\if@afterindent \else
{\setbox\z@\lastbox
\vspace{-\baselineskip}%
}%
\fi
\else
\clubpenalty \@clubpenalty
\everypar{}%
\fi}}
\makeatother
\begin{document}
\section{Test}
\subsection{Sub Test}
\lipsum[4]
\subsection{Sub Test}
\lipsum[4]
\section{Test}
\subsection{Sub Test}
\lipsum[4]
\section{Test}
\lipsum[4]
\subsection{Sub Test}
\section[optional]{Test}
\subsection[optional]{Sub Test}
\lipsum[4]
\section{blblblb}
\lipsum[4]
\end{document}
I get the very ugly
How do I get the first line of the paragraph to be moved up, rather than the second line?

