Witiko / markdown

:notebook_with_decorative_cover: A package for converting and rendering markdown documents in TeX
http://ctan.org/pkg/markdown
LaTeX Project Public License v1.3c
331 stars 31 forks source link

Check that standard LaTeX hooks work as expected #464

Closed Witiko closed 1 month ago

Witiko commented 3 months ago

As previously discussed with @frankmittelbach in https://github.com/Witiko/markdown/issues/93, we won't necessarily define our own LaTeX hooks for the Markdown package. However, we should at least ensure that standard hooks such as {cmd,env}/⟨name⟩/{before,after} are properly invoked before and after the markdown environments, renderers, and renderer prototypes. This will allow LaTeX users to use hooks as an alternative to redefining and patching the renderers and renderer prototypes directly or through the \markdownSetup command.

Witiko commented 1 month ago

Using env/⟨name⟩/{before,after} and cmd/⟨name⟩/before seems OK:

\documentclass{article}
\usepackage{markdown}
\begin{document}
\AddToHook{cmd/markdownRendererEmphasis/before}{emphasis: }
\AddToHook{env/markdown/before}{<markdown>}
\AddToHook{env/markdown/after}{</markdown>}
\begin{markdown}
foo _bar_ baz!
\end{markdown}
\end{document}

image

However, using env/⟨name⟩/after works only for renderers with a fixed number of arguments that are defined without currying.

For example, in the above example, if you explicitly defined the renderer emphasis with the command \markdownSetup or using \def without currying, then you would be able to also use the hook cmd/markdownRendererEmphasis/after:

\documentclass{article}
\usepackage{markdown}
\markdownSetup{renderers={emphasis={\emph{#1}}}}
\begin{document}
\AddToHook{cmd/markdownRendererEmphasis/before}{<emphasis>}
\AddToHook{cmd/markdownRendererEmphasis/after}{</emphasis>}
\AddToHook{env/markdown/before}{<markdown>}
\AddToHook{env/markdown/after}{</markdown>}
\begin{markdown}
foo _bar_ baz!
\end{markdown}
\end{document}

image

However, the default renderer emphasis uses currying and calls the renderer prototype emphasis in a way that prevents the use of the hook cmd/markdownRendererEmphasis/after:

https://github.com/Witiko/markdown/blob/a494c095f5b2ad35865e6cb7e8baa91fd6aef530/markdown.dtx#L15751-L15752

I will document this.