Background (simplified)
I'm using the todonotes package to add todonotes to sections in my documents. I have created a macro to add the todonotes to sections such that they appear alongside the section's entry in the table of contents. I am also using the nameref macro from the hyperref package to refer to various sections of my document by name. Partly because of this, my macro also allows a label to be specified for the section. Here's a somewhat simplified version of the output my macro produces:
\section[{\todo{A todonote about the foo section}}{foo}]{foo}%
\label{sec:foo}
The problem
The trouble with this is that when using the \nameref to refer to a section with a todonote contained in the title, as above, the todonote also gets attached to the reference.
MWE:
\documentclass{article}
\usepackage{todonotes}
\usepackage[colorlinks,linkcolor=blue]{hyperref}
\begin{document}
\tableofcontents
\section{foo}
\begin{itemize}
\item A reference to the \nameref{sec:bar} section
\end{itemize}
\section[{\todo{A todonote about the bar section}}{bar}]{bar}
\label{sec:bar}
A section called bar
\section{baz}
A section called baz
\end{document}
Which produces (after three or four compiles/builds for good measure):

What's happening:
So the call to \nameref{sec:bar} is presumably producing a reference which includes {\todo{A todonote about the bar section}}{bar} and todonotes is (correctly but undesirably) producing a duplicate todonote because of this. To clarify, I would like the reference to the bar section to be produced as just bar without the duplicate todonote.
Approach to finding a solution
Although this is my use case, I would prefer to know how to make nameref ignore any commands within the section title as I feel this would probably be more generally useful (and not just for me).
As a side note, an alternative solution to ignoring the commands could possibly be to define some nameref-specific reference text for the section somehow. However, this is less desirable as it would require a little maintenance to keep it relevant to the section name.

nameref, it's not actually the cause. There may be a [tag:todonotes] related solution, but it's not really a tag:notes problem, if that makes sense. – Staves Mar 25 '12 at 00:17\namerefentirely and just use\hyperref[sec:section]{Section Name}as in this post. – teichert Jun 11 '21 at 16:31