You can patch \_outlinesB
using optex trick 137 in the following way
\replmacro \_outlinesB {} {} {"\\_tmp \{", "\\_tmp \{\#3 "} {}
\hyperlinks \Green \Red \outlines 0
\nonum\notoc\sec Contents
\maketoc
\sec Introduction
\lipsum[1]
\secc Motivation
\lipsum[2]
\secc Goals
\lipsum[4]
\sec Related Work
\lipsum[7]
\bye
or you can simply redefine it
\_def\_outlinesB#1#2#3#4#5#6#7{%
\_advance\_tocrefnum by1
\_isequal{\relax}{#4}\_iffalse
\_advance\_count#1 by1
\_ifcase#1%
\_tmpnum=\_trycs{_ol:\_the\_count0}{0}\_or
\_tmpnum=\_trycs{_ol:\_the\_count0:\_the\_count1}{0}\_relax\_or
\_tmpnum=\_trycs{_ol:\_the\_count0:\_the\_count1:\_the\_count2}{0}\_relax\_or
\_tmpnum=\_trycs{_ol:\_the\_count0:\_the\_count1:\_the\_count2:\_the\_count3}{0}\_relax\_or
\_tmpnum = 0\_relax\_fi
\_isempty{#4}\_iftrue \_pdfunidef\_tmp{#3 #5}\_else \_pdfunidef\_tmp{#3 #4}\_fi % <------------------------
\_outlinesC{toc:\_the\_tocrefnum}{\_ifnum#1<\_outlinelevel\_space\_else-\_fi}{\_tmpnum}{\_tmp}%
\_fi
}
\hyperlinks \Green \Red \outlines 0
\nonum\notoc\sec Contents
\maketoc
\sec Introduction
\lipsum[1]
\secc Motivation
\lipsum[2]
\secc Goals
\lipsum[4]
\sec Related Work
\lipsum[7]
\bye
The arguments of \_outlinesB
are the same as for \_tocline
, i.e.
\_tocline{⟨level⟩}{⟨type⟩}{⟨number⟩}{⟨o-title⟩}{⟨title⟩}{⟨gpageno⟩}{⟨pageno⟩}
see page 129 of optex-doc.pdf.
since \_tmp
holds the text that is being converted to UTF-16 and written to the outline, you can add the section number in there, before \_pdfunidef
does its magic.