arnaucube
arnaucube/davinci-paperPublic Self-hosted repository
fix: normalize latex macros and cleanup markdown conversion output
Pau Escrich p4u@dabax.net
Fri, 27 Feb 2026 11:31:17 +0100
commit

7cf2223d9e91cbe6cef0c5480b12e96d04715995

parent

7856c3133a87c6987292f1bd239fdab097e678d5

2 files changed, 63 insertions(+), 0 deletions(-)

jump to
M .github/workflows/build-and-deploy.yml → .github/workflows/build-and-deploy.yml

@@ -40,6 +40,7 @@ <meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>DaVinci Paper (Markdown)</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@5.8.1/github-markdown.min.css"> + <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.css"> <style> :root { color-scheme: light;

@@ -144,6 +145,8 @@ </div>

<article id="content" class="markdown-body"></article> <script src="https://cdn.jsdelivr.net/npm/marked@15.0.12/lib/marked.umd.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/dompurify@3.1.7/dist/purify.min.js"></script> + <script src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.js"></script> + <script src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/contrib/auto-render.min.js"></script> <script> (async () => { const status = document.getElementById('status');

@@ -157,6 +160,15 @@ const md = await response.text();

marked.setOptions({ gfm: true, breaks: false }); const html = marked.parse(md); content.innerHTML = DOMPurify.sanitize(html); + if (window.renderMathInElement) { + window.renderMathInElement(content, { + throwOnError: false, + delimiters: [ + { left: '$$', right: '$$', display: true }, + { left: '$', right: '$', display: false } + ] + }); + } status.textContent = 'Loaded'; } catch (err) { status.textContent = 'Failed to load markdown';
M scripts/convert-paper.sh → scripts/convert-paper.sh

@@ -22,6 +22,24 @@ "$tex_file" > "${tex_file}.tmp"

mv "${tex_file}.tmp" "$tex_file" done < <(find "$TMP_TEX_ROOT" -type f -name '*.tex' -print0) +# Use markdown-friendly macro styles for conversion output. +cat >> "$TMP_TEX_ROOT/build/preamble-macros.tex" <<'EOF' +\renewcommand{\hlset}[1]{#1} +\renewcommand{\hlget}[1]{#1} +\renewcommand{\circuitstyle}[1]{\texttt{#1}\xspace} +\renewcommand{\smartcontractstyle}[1]{\texttt{#1}\xspace} +\renewcommand{\blobstyle}[1]{\texttt{#1}\xspace} +\renewcommand{\methodstyle}[1]{\texttt{#1}\xspace} +\renewcommand{\vocstyle}[1]{\texttt{#1}\xspace} +\renewcommand{\blinder}[1][]{\texttt{blinder}\xspace} +\renewcommand{\msg}[1][]{\texttt{message}\xspace} +\renewcommand{\enc}[1][]{\texttt{ciphertext}\xspace} +\renewcommand{\pk}[1][]{\texttt{pk}\xspace} +\renewcommand{\sk}[1][]{\texttt{sk}\xspace} +\renewcommand{\conc}{||} +\renewcommand{\F}{\mathbb{F}\xspace} +EOF + # Support extensionless \includegraphics{...} paths by adding symlink aliases. if [[ -d "$TMP_TEX_ROOT/figures" ]]; then while IFS= read -r -d '' figure_file; do

@@ -37,6 +55,20 @@ cd "$TMP_TEX_ROOT/build"

latexpand main.tex > "$ROOT_DIR/build/flat/flattened.tex" ) +# Preserve references as labels (instead of dropping to empty text) so markdown +# remains readable even without TeX cross-reference resolution. +perl -0777 -i -pe ' + s/\\labelcref\{([^}]+)\}/$1/g; + s/\\eqref\{([^}]+)\}/$1/g; + s/\\ref\{([^}]+)\}/$1/g; + s/\\Cref\{([^}]+)\}/$1/g; + s/\\cref\{([^}]+)\}/$1/g; + s/\{\\sf\{([^{}]+)\}\}/\\texttt{$1}/g; + s/\\sf\{([^{}]+)\}/\\texttt{$1}/g; + s/\{\\tt\{([^{}]+)\}\}/\\texttt{$1}/g; + s/\\tt\{([^{}]+)\}/\\texttt{$1}/g; +' build/flat/flattened.tex + pandoc build/flat/flattened.tex \ --from=latex+raw_tex \ --to=gfm+tex_math_dollars \

@@ -48,5 +80,24 @@ --bibliography=v2/bibliography/bibtex.bib \

--extract-media=docs/media \ --strip-comments \ -o docs/paper.md + +# Clean residual LaTeX wrappers that are not useful in markdown output. +perl -0777 -i -pe ' + s/\\hyperlink\{[^{}]*\}\{([^{}]*)\}/$1/g; + s/\\hypertarget\{[^{}]*\}\{([^{}]*)\}/$1/g; + s/\\ensuremath\{([^{}]+)\}/$1/g; + s/\\detokenize\{([^{}]+)\}/$1/g; + s/\{\\sf\{([^{}]+)\}\}/\\texttt{$1}/g; + s/\\sf\{([^{}]+)\}/\\texttt{$1}/g; + s/\{\\tt\{([^{}]+)\}\}/\\texttt{$1}/g; + s/\\tt\{([^{}]+)\}/\\texttt{$1}/g; + s/\\xspace//g; + s/\\hspace\{[^}]*\}//g; + s/\\rule\{[^}]*\}\{[^}]*\}//g; + s/\\allowbreak//g; + s/\$\$\\label\{[^}]+\}\s*/\$\$/g; + s/\\:\|\|\\:/ || /g; + s/\[\]\(#\\detokenize\{([^}]*)\}\)/$1/g; +' docs/paper.md test -s docs/paper.md