In this case, we need to construct unique id for each \cmhcommand, which will serve as link to the TOC. We will modify the sty file slightly:
\ProvidesPackage{cmhloc}
\newcommand\printcmhentry[1]{#1}
\newcommand{\cmhcommand}[1]{\addcontentsline{cmh}{subsection}{#1}\printcmhentry{#1}}
\newcommand{\listofcmh}{\subsection*{List of cmh}\@starttoc{cmh}}
\endinput
In this way, we need to redefine only the \printcmhentry command, without need to duplicate the logic of \cmhcommand in the 4ht file:
\RequirePackage{etoolbox}
\def\toccmh#1#2#3{%
\bgroup% make the changes local
\Configure{TocLink}{% This will print the link in TOC.
\Link{##2}{\@nameuse{cmhcn-##4}}##4\EndLink% We need to construct the ID parameter in second parameter for \Link
}
\HCode{<div class="sectionToc">}#2\HCode{</div>\Hnewline}% Print the entry
\egroup%
}%
\newcount\cmhcount % each cmhentry will have it's unique idntifier
\renewcommand\printcmhentry[1]{%
\advance\cmhcount by1\relax%
\edef\cmhlink{cmh-\the\cmhcount}% this will be the id for the corresponding entry in the \listcmh
\csxdef{cmhcn-#1}{\cmhlink}\Link{\cmhlink}{}#1\EndLink} % we use the entry text in csname,
% in order to be able to retrieve the id later
% in the \listcmh entries
\append:def\listofcmh{\TableOfContents[cmh]}
\endinput
In this case, \toccmh macro is defined instead of \ConfigureToc{cmh}, it is more flexible. We use \Configure{TocLink} to configure the hyperlinks in the TOC entries, the first argument is id of the \addcontentsline, the second one is anchor for the entry. \@nameuse{cmhcn-##4} exapnds to \nameuse{cmhcn-Here is some text} for example. This macro is defined in \renewcommand\printcmhentry and it contains the id the text links to.
This is the result:
<h3 class="sectionHead"><span class="titlemark">1 </span> <a
id="x1-20001"></a>normal section</h3>
<a
id="x1-2001"></a>
<!--l. 9--><p class="noindent" ><a
href="#cmh-1">Here is some text</a> some text
</p>
<h3 class="sectionHead"><span class="titlemark">2 </span> <a
id="x1-30002"></a>another secton</h3>
<a
id="x1-3001"></a>
<!--l. 13--><p class="noindent" ><a
href="#cmh-2">Another text</a>
</p><!--l. 16--><p class="indent" > Inside paragraph: <a
id="x1-3002"></a><a
href="#cmh-3">some more</a>. It doesn’t work well.
</p><!--l. 25--><p class="noindent" >
</p>
<h4 class="likesubsectionHead"> <a
id="x1-40002"></a>List of cmh</h4>
<div class="tableofcontents"> <div class="sectionToc"><a
href="#x1-2001" id="cmh-1">Here is some text</a></div>
<div class="sectionToc"><a
href="#x1-3001" id="cmh-2">Another text</a></div>
<div class="sectionToc"><a
href="#x1-3002" id="cmh-3">some more</a></div>
</div>
\cmhcommandbe part of paragraph, or can it act like an section? – michal.h21 Sep 24 '17 at 10:47