Here is a solution that more or less implements your pseudo code. You can add this in the beginning of your converted LaTeX code (pandoc output).
Update: we have to be careful not to use this trick for chapters that put non-alphabetic characters in \leftmark or in their title. See below.
\documentclass{book}
\usepackage{fancyhdr}
\usepackage{lipsum}
\pagestyle{fancy}
\fancyhead{}
\fancyfoot{}
% Pseudo code:
% var headerText = \leftmark
% switch headerText:
% case "The very long chapter name":
% headerText = "The chapter name"
% case "Chapter two is also very long":
% headerText = "Chapter two"
% ...
% \fancyhead[LO,RE]{\slshape headerText}
% Implementation:
\newcommand\chaptertitle[2]{\expandafter\def\csname=#1=\endcsname{#2}}
\newcommand\shorttitle[1]{\expandafter\csname=\leftmark=\endcsname}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\chaptertitle{One chapter with a very large chapter title}{Chapter One}
\chaptertitle{Another chapter title that really is much too long}{Chapter Two}
\fancyhead[LO,RE]{\slshape \shorttitle{\leftmark}} % I would like to implement the string converter here
%
\begin{document}
\chapter{One chapter with a very large chapter title}
\lipsum
\chapter{Another chapter title that really is much too long}
\lipsum
\end{document}
This gives problems for chapters that use non-alphabetic characters in \markboth, for example \tableofcontents and similar ones. You can solve this by using the original header for these:
Update 2: The \newpage after \tableofcontents should be a \cleardoublepage (if documentclass book is used), otherwise there might be an empty page that still uses the bad \leftmark. Actually it is nicer to do a \newpage first, then change the header to be empty, then do a \cleardoublepage, and then change the header to its new form. In this case, if there is an empty page it will not have a header, which looks nicer. Such a page is always an even page, so we just change the header for even pages. So that is done with this update.
\renewcommand\contentsname{Index}
\fancyhead[RE]{\slshape \leftmark}
\tableofcontents
\newpage
\fancyhead[RE]{}
\cleardoublepage
\fancyhead[RE]{\slshape \shorttitle{\leftmark}}

\documentclassare you using? – Werner Dec 21 '21 at 18:27\documentclassI'm using? – RilDev Dec 21 '21 at 19:40.texoutput, from where you can see what\documentclassis being used. This answer shows it's possible to sprinkle LaTeX throughout the markdown code. – Werner Dec 21 '21 at 22:19--verboserun and got this in the outputDocument Class: article 2019/12/20 v1.4l Standard LaTeX document class. After doing some research I understood that I should use a\documentclass:bookin order to access the\chater{}module. – RilDev Dec 23 '21 at 07:57