Skip to content

Commit 9368ed4

Browse files
authored
Merge pull request #1 from stared/refactor
Code cleanup
2 parents 20b9b9e + ee0d18a commit 9368ed4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2270
-3329
lines changed

.github/workflows/test-latex-beamer.yml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,18 @@ jobs:
2828

2929
- name: Generate LaTeX files
3030
run: |
31-
pnpm tsx test-latex-export-real.ts
32-
pnpm tsx test-beamer-export.ts
33-
pnpm tsx test-latex-export-maxwell.ts
34-
pnpm tsx test-beamer-export-maxwell.ts
31+
pnpm tsx tests/export/test-latex-exports.ts
32+
pnpm tsx tests/export/test-beamer-exports.ts
3533
3634
- name: Verify generated files exist
3735
run: |
38-
ls -lh ./test-output/test-euler-export.tex ./test-output/test-euler-beamer.tex ./test-output/test-maxwell.tex ./test-output/test-maxwell-beamer.tex
36+
ls -lh ./test-output/test-euler.tex ./test-output/test-euler-beamer.tex ./test-output/test-maxwell.tex ./test-output/test-maxwell-beamer.tex
3937
echo "All LaTeX files generated successfully"
4038
4139
- name: Compile Euler LaTeX to PDF
4240
uses: xu-cheng/latex-action@v4
4341
with:
44-
root_file: test-euler-export.tex
42+
root_file: test-euler.tex
4543
working_directory: test-output
4644
latexmk_use_lualatex: false
4745

@@ -72,8 +70,8 @@ jobs:
7270
with:
7371
name: latex-outputs
7472
path: |
75-
test-output/test-euler-export.tex
76-
test-output/test-euler-export.pdf
73+
test-output/test-euler.tex
74+
test-output/test-euler.pdf
7775
test-output/test-euler-beamer.tex
7876
test-output/test-euler-beamer.pdf
7977
test-output/test-maxwell.tex

.github/workflows/test-typst.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
run: pnpm install
2828

2929
- name: Generate Typst file
30-
run: pnpm tsx test-typst-export.ts
30+
run: pnpm tsx tests/export/test-typst-export.ts
3131

3232
- name: Verify generated file exists
3333
run: |

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
"build": "pnpm validate && tsc && vite build",
1010
"preview": "vite preview",
1111
"lint": "eslint .",
12-
"test": "tsx test-export.ts",
13-
"test:interactive": "tsx test-export.ts && playwright test test-html-interactivity.spec.ts",
12+
"test": "tsx tests/export/test-export.ts",
13+
"test:interactive": "tsx tests/export/test-export.ts && playwright test tests/e2e/test-html-interactivity.spec.ts",
1414
"test:playwright": "playwright test"
1515
},
1616
"keywords": [
@@ -38,7 +38,6 @@
3838
"codejar": "^4.3.0",
3939
"katex": "^0.16.25",
4040
"prismjs": "^1.30.0",
41-
"tex-to-typst": "^0.0.19",
4241
"tex2typst": "^0.4.1"
4342
}
4443
}

pnpm-lock.yaml

Lines changed: 0 additions & 367 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/export/beamer-export.ts

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// Beamer export module with TikZ arrows (presentation format)
2+
// Based on texample.net/tikz/examples/beamer-arrows/ pattern
3+
4+
import type { ParsedContent } from '../parser';
5+
import type { ColorScheme } from './types';
6+
import { transformHtmlClass } from '../utils/latex-parser';
7+
import { convertHtmlDescription } from '../utils/html-converter';
8+
import { escapePreservingMath, escapeLaTeX } from '../utils/escape';
9+
import { getTermColor } from '../utils/color-utils';
10+
11+
// Escape LaTeX text while preserving inline math ($...$)
12+
const escapeLatexPreservingMath = (text: string) => escapePreservingMath(text, escapeLaTeX);
13+
14+
// Inject TikZ coordinate nodes into LaTeX equation
15+
function injectTikzNodesInLatex(latex: string): { latex: string; nodeCount: number } {
16+
let nodeCount = 0;
17+
const result = transformHtmlClass(latex, (className, content, index) => {
18+
nodeCount++;
19+
return `\\textcolor{term${className}}{${content}}\\tikz[baseline,remember picture,overlay] \\coordinate (n${index});`;
20+
});
21+
return { latex: result, nodeCount };
22+
}
23+
24+
// Convert HTML description to LaTeX text
25+
function convertDescriptionToLatex(html: string): string {
26+
return convertHtmlDescription(
27+
html,
28+
escapeLaTeX,
29+
(className, content) => `\\textcolor{term${className}}{${escapeLaTeX(content)}}`
30+
);
31+
}
32+
33+
/**
34+
* Export to Beamer with TikZ arrows (presentation format)
35+
* Based on texample.net/tikz/examples/beamer-arrows/ pattern
36+
*/
37+
export function exportToBeamer(
38+
content: ParsedContent,
39+
colorScheme: ColorScheme
40+
): string {
41+
// Generate color definitions for preamble
42+
const colorDefinitions = content.termOrder
43+
.map((className) => {
44+
const color = getTermColor(className, content.termOrder, colorScheme);
45+
const latexColorName = `term${className}`;
46+
const hexColor = color.replace('#', '').toUpperCase();
47+
return `\\definecolor{${latexColorName}}{HTML}{${hexColor}}`;
48+
})
49+
.join('\n');
50+
51+
// Convert LaTeX with TikZ nodes at each term
52+
const { latex: equationWithNodes } = injectTikzNodesInLatex(content.latex);
53+
54+
// Convert description
55+
const descriptionLatex = convertDescriptionToLatex(content.description);
56+
57+
// Generate individual frames for each term with arrow
58+
const definitionFrames = Array.from(content.definitions.entries())
59+
.map(([className, definition], index) => {
60+
const latexColorName = `term${className}`;
61+
const nodeId = `def${index}`;
62+
const equationNodeId = `n${index}`;
63+
const definitionLatex = escapeLatexPreservingMath(definition);
64+
65+
return `\\begin{frame}<${index + 2}>[label=term${index}]
66+
\\frametitle{${escapeLaTeX(content.title!)}}
67+
68+
\\begin{equation*}
69+
${equationWithNodes}
70+
\\end{equation*}
71+
72+
\\vspace{0.5em}
73+
74+
\\begin{block}{}
75+
\\tikz[na] \\node[coordinate] (${nodeId}) {};
76+
${definitionLatex}
77+
\\end{block}
78+
79+
% Draw clean arrow from term to definition (offset left to approximate center, down for margin)
80+
\\begin{tikzpicture}[overlay,remember picture]
81+
\\draw[->,${latexColorName},line width=1.5pt,rounded corners=5pt] ($(${equationNodeId})+(-0.3em,-0.2em)$) -- ++(0,-0.6) -| (${nodeId});
82+
\\end{tikzpicture}
83+
84+
\\end{frame}`;
85+
})
86+
.join('\n\n');
87+
88+
return `\\documentclass{beamer}
89+
90+
% Beamer theme
91+
\\usetheme{default}
92+
\\setbeamertemplate{navigation symbols}{}
93+
94+
% TikZ for arrows
95+
\\usepackage{tikz}
96+
\\usetikzlibrary{arrows,shapes,calc}
97+
\\tikzstyle{every picture}+=[remember picture]
98+
\\tikzstyle{na} = [baseline=-.5ex]
99+
100+
% Colors and math
101+
\\usepackage{amsmath}
102+
\\usepackage{xcolor}
103+
104+
% Define colors from scheme
105+
${colorDefinitions}
106+
107+
\\title{${escapeLaTeX(content.title!)}}
108+
\\date{}
109+
110+
\\begin{document}
111+
112+
\\begin{frame}
113+
\\titlepage
114+
\\end{frame}
115+
116+
\\begin{frame}[label=overview]
117+
\\frametitle{Equation}
118+
119+
\\begin{equation*}
120+
${equationWithNodes}
121+
\\end{equation*}
122+
123+
\\vspace{1em}
124+
125+
${descriptionLatex}
126+
127+
\\end{frame}
128+
129+
${definitionFrames}
130+
131+
\\end{document}`;
132+
}

0 commit comments

Comments
 (0)