|
| 1 | +// Test LaTeX export functionality |
| 2 | +import { parseContent } from './src/parser'; |
| 3 | +import { exportToLaTeX } from './src/exporter'; |
| 4 | +import type { ColorScheme } from './src/exporter'; |
| 5 | +import { writeFileSync } from 'fs'; |
| 6 | + |
| 7 | +// Test markdown content (using actual syntax from the parser) |
| 8 | +const testMarkdown = `# Energy-mass equivalence |
| 9 | +
|
| 10 | +\`\`\`latex |
| 11 | +\\htmlClass{term-energy}{E} = \\htmlClass{term-mass}{m}\\htmlClass{term-speed}{c}^2 |
| 12 | +\`\`\` |
| 13 | +
|
| 14 | +The [energy]{.energy} of a body is equal to its [mass]{.mass} times the [speed of light]{.speed} squared. |
| 15 | +
|
| 16 | +## .energy |
| 17 | +Energy of the body |
| 18 | +
|
| 19 | +## .mass |
| 20 | +Rest mass of the body |
| 21 | +
|
| 22 | +## .speed |
| 23 | +Speed of light in vacuum ($c \\approx 3 \\times 10^8$ m/s) |
| 24 | +`; |
| 25 | + |
| 26 | +// Test color scheme |
| 27 | +const colorScheme: ColorScheme = { |
| 28 | + name: 'viridis', |
| 29 | + colors: ['#440154', '#31688e', '#35b779'], |
| 30 | +}; |
| 31 | + |
| 32 | +async function testLatexExport() { |
| 33 | + console.log('Testing LaTeX export...\n'); |
| 34 | + |
| 35 | + // Parse content |
| 36 | + const parsed = await parseContent(testMarkdown); |
| 37 | + console.log('✓ Content parsed successfully'); |
| 38 | + console.log(` Title: ${parsed.title}`); |
| 39 | + console.log(` Terms: ${parsed.termOrder.join(', ')}`); |
| 40 | + console.log(` Definitions: ${parsed.definitions.size}\n`); |
| 41 | + |
| 42 | + // Generate LaTeX export |
| 43 | + const latex = exportToLaTeX(parsed, colorScheme); |
| 44 | + console.log('✓ LaTeX export generated successfully\n'); |
| 45 | + |
| 46 | + // Validation checks |
| 47 | + const checks = [ |
| 48 | + { name: 'Document class', test: () => latex.includes('\\documentclass{article}') }, |
| 49 | + { name: 'xcolor package', test: () => latex.includes('\\usepackage{xcolor}') }, |
| 50 | + { name: 'amsmath package', test: () => latex.includes('\\usepackage{amsmath}') }, |
| 51 | + { name: 'Color definitions', test: () => latex.includes('\\definecolor{termenergy}{HTML}') }, |
| 52 | + { name: 'Colored equation terms (energy)', test: () => latex.includes('\\textcolor{termenergy}{E}') }, |
| 53 | + { name: 'Colored equation terms (mass)', test: () => latex.includes('\\textcolor{termmass}{m}') }, |
| 54 | + { name: 'Colored equation terms (speed)', test: () => latex.includes('\\textcolor{termspeed}{c}') }, |
| 55 | + { name: 'No \\htmlClass', test: () => !latex.includes('\\htmlClass') }, |
| 56 | + { name: 'Title escaped', test: () => latex.includes('Energy-mass equivalence') }, |
| 57 | + { name: 'Description section', test: () => latex.includes('\\section*{Description}') }, |
| 58 | + { name: 'Definitions section', test: () => latex.includes('\\section*{Definitions}') }, |
| 59 | + { name: 'Colored definition heading', test: () => latex.includes('\\subsection*{\\textcolor{termenergy}{energy}}') }, |
| 60 | + { name: 'Escaped LaTeX chars', test: () => !latex.match(/(?<!\\)[&%#_]/) }, // No unescaped special chars |
| 61 | + { name: 'Math mode preserved', test: () => latex.includes('$c \\approx 3 \\times 10^8$') }, |
| 62 | + { name: 'Document structure', test: () => latex.includes('\\begin{document}') && latex.includes('\\end{document}') }, |
| 63 | + { name: 'Equation environment', test: () => latex.includes('\\begin{equation}') && latex.includes('\\end{equation}') }, |
| 64 | + ]; |
| 65 | + |
| 66 | + let passed = 0; |
| 67 | + let failed = 0; |
| 68 | + |
| 69 | + checks.forEach((check) => { |
| 70 | + try { |
| 71 | + if (check.test()) { |
| 72 | + console.log(` ✓ ${check.name}`); |
| 73 | + passed++; |
| 74 | + } else { |
| 75 | + console.log(` ✗ ${check.name}`); |
| 76 | + failed++; |
| 77 | + } |
| 78 | + } catch (error) { |
| 79 | + console.log(` ✗ ${check.name} (error: ${error})`); |
| 80 | + failed++; |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + console.log(`\n${passed}/${checks.length} checks passed\n`); |
| 85 | + |
| 86 | + // Write to file for manual inspection |
| 87 | + const outputPath = '/tmp/test-export.tex'; |
| 88 | + writeFileSync(outputPath, latex); |
| 89 | + console.log(`LaTeX output written to: ${outputPath}`); |
| 90 | + console.log('You can compile it with: pdflatex /tmp/test-export.tex\n'); |
| 91 | + |
| 92 | + // Show first few lines |
| 93 | + console.log('First 30 lines of LaTeX output:'); |
| 94 | + console.log('---'); |
| 95 | + console.log(latex.split('\n').slice(0, 30).join('\n')); |
| 96 | + console.log('...\n'); |
| 97 | + |
| 98 | + if (failed > 0) { |
| 99 | + console.error(`❌ ${failed} checks failed`); |
| 100 | + process.exit(1); |
| 101 | + } else { |
| 102 | + console.log('✅ All checks passed!'); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +testLatexExport().catch((error) => { |
| 107 | + console.error('Test failed:', error); |
| 108 | + process.exit(1); |
| 109 | +}); |
0 commit comments