File size: 2,322 Bytes
f41aded
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import assert from 'node:assert/strict';
import { mkdirSync, mkdtempSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { test } from 'node:test';
import { flattenIncludes, sanitizeAst } from './tools.js';

test('flattenIncludes yalnızca proje dosyalarını birleştirir', () => {
  const files = new Map([
    ['main.tex', 'A\n\\input{bolumler/giris}\n\\include{son.tex}\n\\input{C:/Windows/win.ini}\n\\input{../disari}\nB'],
    ['bolumler/giris.tex', 'GİRİŞ'],
    ['son.tex', 'SON'],
  ]);
  const out = flattenIncludes(files, 'main.tex');
  assert.match(out, /GİRİŞ/);
  assert.match(out, /SON/);
  // Proje dışı yollar çözülmez, olduğu gibi kalır (Pandoc da sandbox içinde onları okuyamaz).
  assert.match(out, /\\input\{C:\/Windows\/win\.ini\}/);
  assert.match(out, /\\input\{\.\.\/disari\}/);
});

test('flattenIncludes yorumdaki komutlara ve döngülere dokunmaz', () => {
  const files = new Map([
    ['main.tex', '% \\input{a}\nmetin % \\input{a}\n\\input{a}'],
    ['a.tex', 'A \\input{main}'],
  ]);
  assert.equal(flattenIncludes(files, 'main.tex'), '% \\input{a}\nmetin % \\input{a}\n\nA \\input{main}\n');
});

test('sanitizeAst yalnızca çalışma klasöründeki resimleri ve güvenli üst bilgileri bırakır', () => {
  const work = mkdtempSync(path.join(tmpdir(), 'thoth-ast-'));
  mkdirSync(path.join(work, 'sekiller'));
  writeFileSync(path.join(work, 'sekiller', 'grafik.png'), 'x');
  const image = (url) => ({ t: 'Image', c: [['', [], []], [{ t: 'Str', c: 'alt' }], [url, '']] });
  const ast = {
    'pandoc-api-version': [1, 23],
    meta: { title: { t: 'MetaInlines', c: [] }, bibliography: { t: 'MetaString', c: '../../baska.bib' } },
    blocks: [{ t: 'Para', c: [image('sekiller/grafik'), image('C:/gizli.png'), image('../disari.png'), image('https://ornek.com/a.png'), image('sekil.pdf')] }],
  };
  const { ast: clean, skipped } = sanitizeAst(ast, work);
  assert.deepEqual(Object.keys(clean.meta), ['title']);
  const [ok, ...rest] = clean.blocks[0].c;
  assert.equal(ok.t, 'Image');
  assert.equal(ok.c[2][0], path.join(work, 'sekiller', 'grafik.png'));
  assert.ok(rest.every((n) => n.t === 'Emph'));
  assert.deepEqual(skipped, ['C:/gizli.png', '../disari.png', 'https://ornek.com/a.png', 'sekil.pdf']);
});