| 1 | import type { SlidevConfig, SlidevPreparserExtension } from '../packages/types/src' |
| 2 | import { basename, relative, resolve } from 'node:path' |
| 3 | import { objectMap, slash } from '@antfu/utils' |
| 4 | import fg from 'fast-glob' |
| 5 | import { describe, expect, it } from 'vitest' |
| 6 | import { extractImagesUsage, parseSync } from '../packages/parser/src/core' |
| 7 | import { getDefaultConfig, load, parse, prettify, resolveConfig, stringify } from '../packages/parser/src/fs' |
| 8 | |
| 9 | function configDiff(v: SlidevConfig) { |
| 10 | const defaults = getDefaultConfig() |
| 11 | const res: Record<string, any> = {} |
| 12 | for (const key of Object.keys(v) as (keyof SlidevConfig)[]) { |
| 13 | if (JSON.stringify(v[key]) !== JSON.stringify(defaults[key])) |
| 14 | res[key] = v[key] |
| 15 | } |
| 16 | return res |
| 17 | } |
| 18 | |
| 19 | function replaceCRLF(s: string) { |
| 20 | return s.replace(/\r\n/g, '\n') |
| 21 | } |
| 22 | |
| 23 | describe('advance debounce config', () => { |
| 24 | it('is disabled by default and resolves deck and nested config values', () => { |
| 25 | expect(getDefaultConfig().advanceDebounce).toBe(0) |
| 26 | expect(resolveConfig({}).advanceDebounce).toBe(0) |
| 27 | expect(resolveConfig({ advanceDebounce: 100 }).advanceDebounce).toBe(100) |
| 28 | expect(resolveConfig({ |
| 29 | config: { advanceDebounce: 250 }, |
| 30 | }).advanceDebounce).toBe(250) |
| 31 | }) |
| 32 | }) |
| 33 | |
| 34 | describe('md parser', () => { |
| 35 | const userRoot = resolve(__dirname, 'fixtures/markdown') |
| 36 | const files = fg.sync('*.md', { |
| 37 | cwd: userRoot, |
| 38 | absolute: true, |
| 39 | }) |
| 40 | |
| 41 | for (const file of files) { |
| 42 | it(basename(file), async () => { |
| 43 | const data = await load({ |
| 44 | userRoot, |
| 45 | roots: [userRoot], |
| 46 | }, file) |
| 47 | |
| 48 | expect(stringify(data.entry).trim()).toEqual(replaceCRLF(data.entry.raw.trim())) |
| 49 | |
| 50 | prettify(data.entry) |
| 51 | |
| 52 | // File path tests & convert to relative paths |
| 53 | data.markdownFiles = objectMap(data.markdownFiles, (path, md) => { |
| 54 | expect(md.filepath).toBe(path) |
| 55 | const relativePath = slash(relative(userRoot, path)) |
| 56 | md.slides.forEach((slide) => { |
| 57 | expect(slide.filepath).toBe(path) |
| 58 | slide.filepath = relativePath |
| 59 | }) |
| 60 | md.filepath = relativePath |
| 61 | return [relativePath, md] |
| 62 | }) |
| 63 | |
| 64 | expect(data.slides).toMatchSnapshot('slides') |
| 65 | expect(configDiff(resolveConfig(data.headmatter, {}))).toMatchSnapshot('config') |
| 66 | expect(data.features).toMatchSnapshot('features') |
| 67 | }) |
| 68 | } |
| 69 | |
| 70 | it('parse', async () => { |
| 71 | const data = await parse(` |
| 72 | a |
| 73 | |
| 74 | --- |
| 75 | |
| 76 | b |
| 77 | |
| 78 | --- |
| 79 | layout: z |
| 80 | --- |
| 81 | c |
| 82 | ---- |
| 83 | d |
| 84 | ---- |
| 85 | e |
| 86 | |
| 87 | --- |
| 88 | |
| 89 | f |
| 90 | |
| 91 | `, 'file.md') |
| 92 | expect(data.slides.map(i => i.content.trim())) |
| 93 | .toEqual(Array.from('abcdef')) |
| 94 | expect(data.slides[2].frontmatter) |
| 95 | .toEqual({ layout: 'z' }) |
| 96 | expect(data.slides[3].frontmatter) |
| 97 | .toEqual({ }) |
| 98 | }) |
| 99 | |
| 100 | it('parse section matter', async () => { |
| 101 | const data = await parse(` |
| 102 | a |
| 103 | |
| 104 | --- |
| 105 | |
| 106 | b |
| 107 | |
| 108 | ---section2 |
| 109 | layout: z |
| 110 | --- |
| 111 | c |
| 112 | ---- section 3 |
| 113 | d |
| 114 | ---- section-4 |
| 115 | e |
| 116 | |
| 117 | --- |
| 118 | |
| 119 | f |
| 120 | |
| 121 | `, 'file.md') |
| 122 | expect(data.slides.map(i => i.content.trim())) |
| 123 | .toEqual(Array.from('abcdef')) |
| 124 | expect(data.slides[2].frontmatter) |
| 125 | .toEqual({ layout: 'z' }) |
| 126 | expect(data.slides[3].frontmatter) |
| 127 | .toEqual({ }) |
| 128 | }) |
| 129 | |
| 130 | it('does not take the slide title from a heading inside a code block', async () => { |
| 131 | const data = await parse([ |
| 132 | '```bash', |
| 133 | '# Install the CLI', |
| 134 | 'npm i -g @slidev/cli', |
| 135 | '```', |
| 136 | '', |
| 137 | '## Getting started', |
| 138 | '', |
| 139 | '---', |
| 140 | '', |
| 141 | '```bash', |
| 142 | '# Only a code comment', |
| 143 | '```', |
| 144 | '', |
| 145 | ].join('\n'), 'file.md') |
| 146 | |
| 147 | expect(data.slides[0].title).toBe('Getting started') |
| 148 | expect(data.slides[0].level).toBe(2) |
| 149 | expect(data.slides[1].title).toBe(undefined) |
| 150 | }) |
| 151 | |
| 152 | it('ignores slide separators inside tilde code blocks', async () => { |
| 153 | const markdown = '# A\n\n~~~md\n---\ntitle: x\n---\n~~~\n\n---\n\n# B\n' |
| 154 | |
| 155 | const data = await parse(markdown, 'file.md') |
| 156 | expect(data.slides.map(i => i.content.trim())).toEqual(['# A\n\n~~~md\n---\ntitle: x\n---\n~~~', '# B']) |
| 157 | expect(data.slides[0].frontmatter).toEqual({}) |
| 158 | expect(parseSync(markdown, 'file.md').slides.map(i => i.content.trim())).toEqual(['# A\n\n~~~md\n---\ntitle: x\n---\n~~~', '# B']) |
| 159 | }) |
| 160 | |
| 161 | it('does not treat indented tildes as a code fence', async () => { |
| 162 | for (const indent of [' ', '\t']) { |
| 163 | const markdown = `# A\n\n${indent}~~~\n\n---\n\n# B\n\n${indent}~~~\n` |
| 164 | const expected = [`# A\n\n${indent}~~~`, `# B\n\n${indent}~~~`] |
| 165 | |
| 166 | expect((await parse(markdown, 'file.md')).slides.map(i => i.content.trim())).toEqual(expected) |
| 167 | expect(parseSync(markdown, 'file.md').slides.map(i => i.content.trim())).toEqual(expected) |
| 168 | } |
| 169 | }) |
| 170 | |
| 171 | it('ignores a leading UTF-8 BOM', async () => { |
| 172 | const markdown = '---\ntheme: seriph\ntitle: Hello\n---\n\n# Slide 1\n\n---\n\n# Slide 2\n' |
| 173 | |
| 174 | const data = await parse(markdown, 'file.md') |
| 175 | expect(data.slides.map(i => i.content.trim())).toEqual(['# Slide 1', '# Slide 2']) |
| 176 | expect(data.slides[0].frontmatter).toEqual({ theme: 'seriph', title: 'Hello' }) |
| 177 | |
| 178 | const userRoot = resolve(__dirname, 'fixtures/markdown') |
| 179 | const filepath = slash(resolve(userRoot, 'bom.md')) |
| 180 | const loaded = await load({ |
| 181 | userRoot, |
| 182 | roots: [userRoot], |
| 183 | }, filepath, { [filepath]: markdown }) |
| 184 | expect(loaded.headmatter).toEqual({ theme: 'seriph', title: 'Hello' }) |
| 185 | }) |
| 186 | |
| 187 | it('ignores slide separators inside HTML comments', async () => { |
| 188 | const data = await parse(`--- |
| 189 | src: ./pages/one.md |
| 190 | --- |
| 191 | |
| 192 | <!-- |
| 193 | --- |
| 194 | src: ./pages/two.md |
| 195 | --- |
| 196 | --> |
| 197 | |
| 198 | --- |
| 199 | src: ./pages/three.md |
| 200 | --- |
| 201 | `, 'slides.md') |
| 202 | |
| 203 | expect(data.slides).toHaveLength(2) |
| 204 | expect(data.slides.map(slide => slide.frontmatter.src)) |
| 205 | .toEqual(['./pages/one.md', './pages/three.md']) |
| 206 | }) |
| 207 | |
| 208 | describe('missing blank line after `---`', () => { |
| 209 | const cases = { |
| 210 | 'a multi-line comment': '<!--\nnote\n-->', |
| 211 | 'a comment containing a colon': '<!-- note: something -->', |
| 212 | 'plain text': 'hello', |
| 213 | 'a list': '- a\n- b', |
| 214 | } |
| 215 | |
| 216 | for (const [name, body] of Object.entries(cases)) { |
| 217 | it(`records an error for ${name}`, async () => { |
| 218 | const data = await parse(`# Slide 1\n\n---\n${body}\n\n---\n\n# Slide 3\n`, 'slides.md') |
| 219 | |
| 220 | expect(data.slides).toHaveLength(2) |
| 221 | expect(data.slides[1].frontmatter).toEqual({}) |
| 222 | expect(data.errors).toEqual([ |
| 223 | { |
| 224 | row: 2, |
| 225 | message: expect.stringContaining('blank line'), |
| 226 | }, |
| 227 | ]) |
| 228 | }) |
| 229 | } |
| 230 | |
| 231 | it('does not record an error when the blank line exists', async () => { |
| 232 | const data = await parse(`# Slide 1\n\n---\n\n<!--\nnote\n-->\n\n---\n\n# Slide 3\n`, 'slides.md') |
| 233 | |
| 234 | expect(data.slides[1].note).toBe('note') |
| 235 | expect(data.errors).toBeUndefined() |
| 236 | }) |
| 237 | |
| 238 | it('does not record an error for a valid or comment-only frontmatter', async () => { |
| 239 | const data = await parse(`# Slide 1\n\n---\nlayout: center\n---\n\n# Slide 2\n\n---\n# layout: center\n---\n\n# Slide 3\n`, 'slides.md') |
| 240 | |
| 241 | expect(data.slides[1].frontmatter).toEqual({ layout: 'center' }) |
| 242 | expect(data.slides[2].frontmatter).toEqual({}) |
| 243 | expect(data.errors).toBeUndefined() |
| 244 | }) |
| 245 | }) |
| 246 | |
| 247 | it('ignores an HTML comment inside frontmatter and records an error', async () => { |
| 248 | const data = await parse(`# Slide 1\n\n---\nlayout: center\n<!-- note -->\n---\n\n# Slide 2\n`, 'slides.md') |
| 249 | |
| 250 | expect(data.slides[1].frontmatter).toEqual({ layout: 'center' }) |
| 251 | expect(data.errors).toEqual([ |
| 252 | { |
| 253 | row: 2, |
| 254 | message: expect.stringContaining('HTML comments are not supported in frontmatter'), |
| 255 | }, |
| 256 | ]) |
| 257 | }) |
| 258 | |
| 259 | it('detects slide separator even when comment opens on the same line', async () => { |
| 260 | const data = await parse(`a |
| 261 | |
| 262 | ----<!-- |
| 263 | hidden |
| 264 | --> |
| 265 | |
| 266 | b |
| 267 | `, 'file.md') |
| 268 | |
| 269 | expect(data.slides).toHaveLength(2) |
| 270 | expect(data.slides.map(s => s.content.trim())).toEqual(['a', 'hidden\n-->\n\nb']) |
| 271 | }) |
| 272 | |
| 273 | async function parseWithExtension( |
| 274 | src: string, |
| 275 | transformRawLines: (lines: string[]) => void | Promise<void> = () => {}, |
| 276 | more = {}, |
| 277 | moreExts: SlidevPreparserExtension[] = [], |
| 278 | ) { |
| 279 | return await parse( |
| 280 | src, |
| 281 | 'file.md', |
| 282 | [ |
| 283 | { transformRawLines, ...more }, |
| 284 | ...moreExts, |
| 285 | ] as any, |
| 286 | ) |
| 287 | } |
| 288 | |
| 289 | it('parse with-extension replace', async () => { |
| 290 | const data = await parseWithExtension(`--- |
| 291 | ga: bu |
| 292 | --- |
| 293 | a @@v@@ |
| 294 | |
| 295 | --- |
| 296 | |
| 297 | b |
| 298 | @@v@@ |
| 299 | |
| 300 | @@v@@ = @@v@@ |
| 301 | `, (lines) => { |
| 302 | for (const i in lines) |
| 303 | lines[i] = lines[i].replace(/@@v@@/g, 'thing') |
| 304 | }) |
| 305 | |
| 306 | expect(data.slides.map(i => i.content.trim().replace(/\n/g, '%')).join('/')) |
| 307 | .toEqual('a thing/b%thing%%thing = thing') |
| 308 | }) |
| 309 | |
| 310 | it('parse with-extension custom-separator', async () => { |
| 311 | const data = await parseWithExtension(`--- |
| 312 | ga: bu |
| 313 | --- |
| 314 | a @@v@@ |
| 315 | |
| 316 | SEPARATOR |
| 317 | |
| 318 | b |
| 319 | @@v@@ |
| 320 | |
| 321 | @@v@@ = @@v@@ |
| 322 | `, (lines) => { |
| 323 | for (const i in lines) |
| 324 | lines[i] = lines[i].replace(/^SEPARATOR$/g, '---') |
| 325 | }) |
| 326 | |
| 327 | expect(data.slides.map(i => i.content.trim().replace(/\n/g, '%')).join('/')) |
| 328 | .toEqual('a @@v@@/b%@@v@@%%@@v@@ = @@v@@') |
| 329 | }) |
| 330 | |
| 331 | it('parse with-extension eg-easy-cover', async () => { |
| 332 | function cov(i: string, more = '.jpg') { |
| 333 | return `--- |
| 334 | layout: cover |
| 335 | background: ${i}${more} |
| 336 | --- |
| 337 | |
| 338 | ` |
| 339 | } |
| 340 | const data = await parseWithExtension(`@cov 1.jpg |
| 341 | @cov 2.jpg |
| 342 | # 2 |
| 343 | --- |
| 344 | |
| 345 | # 3 |
| 346 | @cov 4.jpg |
| 347 | `, (lines) => { |
| 348 | let i = 0 |
| 349 | while (i < lines.length) { |
| 350 | if (lines[i].startsWith('@cov ')) { |
| 351 | const repl = [ |
| 352 | ...cov(lines[i].substring(5), '').split('\n'), |
| 353 | ] |
| 354 | lines.splice(i, 1, ...repl) |
| 355 | } |
| 356 | i++ |
| 357 | } |
| 358 | }) |
| 359 | |
| 360 | expect(data.slides.map(s => s.content.trim().replace(/\n/g, '%')).join('/')) |
| 361 | .toEqual('/# 2/# 3/'.replace(/\n/g, '%')) |
| 362 | expect(data.slides[0].frontmatter) |
| 363 | .toEqual({ layout: 'cover', background: '1.jpg' }) |
| 364 | expect(data.slides[1].frontmatter) |
| 365 | .toEqual({ layout: 'cover', background: '2.jpg' }) |
| 366 | expect(data.slides[3].frontmatter) |
| 367 | .toEqual({ layout: 'cover', background: '4.jpg' }) |
| 368 | }) |
| 369 | |
| 370 | it('parse with-extension sequence', async () => { |
| 371 | const data = await parseWithExtension(` |
| 372 | a..A |
| 373 | a.a.A.A |
| 374 | .a.A. |
| 375 | `, undefined, {}, [ |
| 376 | { |
| 377 | name: 'test', |
| 378 | transformRawLines(lines: string[]) { |
| 379 | for (const i in lines) |
| 380 | lines[i] = lines[i].replace(/A/g, 'B').replace(/a/g, 'A') |
| 381 | }, |
| 382 | }, |
| 383 | { |
| 384 | name: 'test', |
| 385 | transformRawLines(lines: string[]) { |
| 386 | for (const i in lines) |
| 387 | lines[i] = lines[i].replace(/A/g, 'C') |
| 388 | }, |
| 389 | }, |
| 390 | ]) |
| 391 | expect(data.slides.map(i => i.content.trim().replace(/\n/g, '%')).join('/')) |
| 392 | .toEqual('C..B%C.C.B.B%.C.B.') |
| 393 | }) |
| 394 | |
| 395 | // Generate cartesian product of given iterables: |
| 396 | function* cartesian(...all: any[]): Generator<any[], void, void> { |
| 397 | const [head, ...tail] = all |
| 398 | const remainder = tail.length |
| 399 | ? cartesian(...tail) |
| 400 | : [[]] |
| 401 | for (const r of remainder) { |
| 402 | for (const h of head) |
| 403 | yield [h, ...r] |
| 404 | } |
| 405 | } |
| 406 | const B = [0, 1] |
| 407 | const Bs = [B, B, B, B, B, B] |
| 408 | const bNames = '_swScCF' |
| 409 | |
| 410 | for (const desc of cartesian(...Bs)) { |
| 411 | const [withSlideBefore, withFrontmatter, withSlideAfter, prependContent, appendContent, addFrontmatter] = desc |
| 412 | it(`parse with-extension wrap ${desc.map((b, i) => bNames[b * (i + 1)]).join('')}`, async () => { |
| 413 | const code = [ |
| 414 | withSlideBefore |
| 415 | ? [ |
| 416 | '.', |
| 417 | '', |
| 418 | '---', |
| 419 | ] |
| 420 | : [], |
| 421 | (!withSlideBefore && withFrontmatter) |
| 422 | ? [ |
| 423 | '---', |
| 424 | ] |
| 425 | : [], |
| 426 | withFrontmatter |
| 427 | ? [ |
| 428 | 'm: M', |
| 429 | 'n: N', |
| 430 | '---', |
| 431 | ] |
| 432 | : [], |
| 433 | '', |
| 434 | 'ccc', |
| 435 | '@a', |
| 436 | '@b', |
| 437 | 'ddd', |
| 438 | '', |
| 439 | withSlideAfter |
| 440 | ? [ |
| 441 | '', |
| 442 | '---', |
| 443 | '', |
| 444 | '..', |
| 445 | ] |
| 446 | : [], |
| 447 | ].flat().join('\n') |
| 448 | |
| 449 | const data = await parseWithExtension( |
| 450 | code, |
| 451 | undefined, |
| 452 | { |
| 453 | transformSlide(content: string, frontmatter: any) { |
| 454 | const lines = content.split('\n') |
| 455 | let i = 0 |
| 456 | let appendBeforeCount = 0 |
| 457 | let appendAfterCount = 0 |
| 458 | while (i < lines.length) { |
| 459 | const l = lines[i] |
| 460 | if (l.startsWith('@')) { |
| 461 | const t = l.substring(1) |
| 462 | lines.splice(i, 1) |
| 463 | if (prependContent) |
| 464 | lines.splice(appendBeforeCount++, 0, `<${t}>`) |
| 465 | if (appendContent) |
| 466 | lines.splice(lines.length - appendAfterCount++, 0, `</${t}>`) |
| 467 | if (addFrontmatter) |
| 468 | frontmatter[`add${t}`] = 'add' |
| 469 | i-- |
| 470 | } |
| 471 | i++ |
| 472 | } |
| 473 | return lines.join('\n') |
| 474 | }, |
| 475 | }, |
| 476 | ) |
| 477 | |
| 478 | function project(s: string) { |
| 479 | // like the trim in other tests, the goal is not to test newlines here |
| 480 | return s.replace(/%{2,}/g, '%') |
| 481 | } |
| 482 | expect(project(data.slides.map(i => i.content.replace(/\n/g, '%')).join('/'))) |
| 483 | .toEqual(project([ |
| 484 | ...withSlideBefore |
| 485 | ? ['./'] |
| 486 | : [], |
| 487 | ...prependContent |
| 488 | ? ['<a>%<b>%'] |
| 489 | : [], |
| 490 | 'ccc%ddd', |
| 491 | ...appendContent |
| 492 | ? ['%</b>%</a>'] |
| 493 | : [], |
| 494 | ...withSlideAfter |
| 495 | ? ['/..'] |
| 496 | : [], |
| 497 | ].join(''))) |
| 498 | |
| 499 | if (withFrontmatter || addFrontmatter) { |
| 500 | expect(data.slides[withSlideBefore ? 1 : 0].frontmatter) |
| 501 | .toEqual({ |
| 502 | ...withFrontmatter |
| 503 | ? { m: 'M', n: 'N' } |
| 504 | : {}, |
| 505 | ...addFrontmatter |
| 506 | ? { adda: 'add', addb: 'add' } |
| 507 | : {}, |
| 508 | }) |
| 509 | } |
| 510 | }) |
| 511 | } |
| 512 | |
| 513 | describe('extractImagesUsage', () => { |
| 514 | it('extracts from frontmatter image key', () => { |
| 515 | const images = extractImagesUsage('', { image: '/path/to/image.png' }) |
| 516 | expect(images).toEqual(['/path/to/image.png']) |
| 517 | }) |
| 518 | |
| 519 | it('extracts from frontmatter backgroundImage key', () => { |
| 520 | const images = extractImagesUsage('', { backgroundImage: 'https://example.com/bg.jpg' }) |
| 521 | expect(images).toEqual(['https://example.com/bg.jpg']) |
| 522 | }) |
| 523 | |
| 524 | it('extracts from frontmatter background key with image extension', () => { |
| 525 | const images = extractImagesUsage('', { background: '/assets/bg.webp' }) |
| 526 | expect(images).toEqual(['/assets/bg.webp']) |
| 527 | }) |
| 528 | |
| 529 | it('extracts from frontmatter background key with URL', () => { |
| 530 | const images = extractImagesUsage('', { background: 'https://example.com/image.png' }) |
| 531 | expect(images).toEqual(['https://example.com/image.png']) |
| 532 | }) |
| 533 | |
| 534 | it('ignores frontmatter background key without image extension or URL', () => { |
| 535 | const images = extractImagesUsage('', { background: 'gradient-to-r' }) |
| 536 | expect(images).toEqual([]) |
| 537 | }) |
| 538 | |
| 539 | it('ignores data URLs in frontmatter', () => { |
| 540 | const images = extractImagesUsage('', { image: 'data:image/png;base64,abc123' }) |
| 541 | expect(images).toEqual([]) |
| 542 | }) |
| 543 | |
| 544 | it('extracts markdown image syntax', () => { |
| 545 | const content = '' |
| 546 | const images = extractImagesUsage(content, {}) |
| 547 | expect(images).toEqual(['/images/photo.jpg']) |
| 548 | }) |
| 549 | |
| 550 | it('extracts multiple markdown images', () => { |
| 551 | const content = ` |
| 552 |  |
| 553 | Some text |
| 554 |  |
| 555 |  |
| 556 | ` |
| 557 | const images = extractImagesUsage(content, {}) |
| 558 | expect(images).toEqual(['/img1.png', 'https://example.com/img2.jpg', './relative/path.svg']) |
| 559 | }) |
| 560 | |
| 561 | it('ignores the optional title in markdown images', () => { |
| 562 | const content = ` |
| 563 | ` |
| 564 | const images = extractImagesUsage(content, {}) |
| 565 | expect(images).toEqual(['/images/photo.jpg', '/images/other.png']) |
| 566 | }) |
| 567 | |
| 568 | it('unwraps angle-bracketed markdown image destinations', () => { |
| 569 | const content = '' |
| 570 | const images = extractImagesUsage(content, {}) |
| 571 | expect(images).toEqual(['/images/my photo.jpg']) |
| 572 | }) |
| 573 | |
| 574 | it('ignores data URLs in markdown images', () => { |
| 575 | const content = '' |
| 576 | const images = extractImagesUsage(content, {}) |
| 577 | expect(images).toEqual([]) |
| 578 | }) |
| 579 | |
| 580 | it('extracts Vue component src prop', () => { |
| 581 | const content = '<img src="/assets/logo.png" />' |
| 582 | const images = extractImagesUsage(content, {}) |
| 583 | expect(images).toEqual(['/assets/logo.png']) |
| 584 | }) |
| 585 | |
| 586 | it('extracts Vue component image prop', () => { |
| 587 | const content = '<MyImage image="/path/to/image.jpg" />' |
| 588 | const images = extractImagesUsage(content, {}) |
| 589 | expect(images).toEqual(['/path/to/image.jpg']) |
| 590 | }) |
| 591 | |
| 592 | it('ignores Vue props without image extensions', () => { |
| 593 | const content = '<div src="/some/path" />' |
| 594 | const images = extractImagesUsage(content, {}) |
| 595 | expect(images).toEqual([]) |
| 596 | }) |
| 597 | |
| 598 | it('ignores Vue props with template expressions', () => { |
| 599 | const content = '<img src="{{imagePath}}.png" />' |
| 600 | const images = extractImagesUsage(content, {}) |
| 601 | expect(images).toEqual([]) |
| 602 | }) |
| 603 | |
| 604 | it('extracts Vue bound props with string literals', () => { |
| 605 | const content = `<img :src="'/static/image.png'" />` |
| 606 | const images = extractImagesUsage(content, {}) |
| 607 | expect(images).toEqual(['/static/image.png']) |
| 608 | }) |
| 609 | |
| 610 | it('extracts CSS url()', () => { |
| 611 | const content = ` |
| 612 | <div style="background: url(/bg/image.png)"> |
| 613 | <style> |
| 614 | .class { background-image: url('/another/image.jpg'); } |
| 615 | </style> |
| 616 | ` |
| 617 | const images = extractImagesUsage(content, {}) |
| 618 | expect(images).toEqual(['/bg/image.png', '/another/image.jpg']) |
| 619 | }) |
| 620 | |
| 621 | it('ignores CSS url() without image extensions', () => { |
| 622 | const content = `<div style="background: url(/fonts/font.woff2)">` |
| 623 | const images = extractImagesUsage(content, {}) |
| 624 | expect(images).toEqual([]) |
| 625 | }) |
| 626 | |
| 627 | it('strips code blocks to avoid false positives', () => { |
| 628 | const content = ` |
| 629 | Some content |
| 630 |  |
| 631 | |
| 632 | \`\`\`markdown |
| 633 |  |
| 634 | <img src="/also-fake.jpg" /> |
| 635 | \`\`\` |
| 636 | |
| 637 |  |
| 638 | ` |
| 639 | const images = extractImagesUsage(content, {}) |
| 640 | expect(images).toEqual(['/real.png', '/real2.jpg']) |
| 641 | }) |
| 642 | |
| 643 | it('handles multiple sources combined', () => { |
| 644 | const content = ` |
| 645 | # Title |
| 646 |  |
| 647 | <img src="/vue.jpg" /> |
| 648 | <div style="background: url(/css.webp)"> |
| 649 | ` |
| 650 | const frontmatter = { |
| 651 | image: '/frontmatter.png', |
| 652 | background: 'https://example.com/bg.svg', |
| 653 | } |
| 654 | const images = extractImagesUsage(content, frontmatter) |
| 655 | expect(images).toContain('/frontmatter.png') |
| 656 | expect(images).toContain('https://example.com/bg.svg') |
| 657 | expect(images).toContain('/md.png') |
| 658 | expect(images).toContain('/vue.jpg') |
| 659 | expect(images).toContain('/css.webp') |
| 660 | expect(images).toHaveLength(5) |
| 661 | }) |
| 662 | |
| 663 | it('deduplicates identical URLs', () => { |
| 664 | const content = ` |
| 665 |  |
| 666 |  |
| 667 | <img src="/same.png" /> |
| 668 | ` |
| 669 | const images = extractImagesUsage(content, {}) |
| 670 | expect(images).toEqual(['/same.png']) |
| 671 | }) |
| 672 | |
| 673 | it('trims whitespace from extracted URLs', () => { |
| 674 | const content = '' |
| 675 | const images = extractImagesUsage(content, {}) |
| 676 | expect(images).toEqual(['/path/with/spaces.png']) |
| 677 | }) |
| 678 | |
| 679 | it('handles various image extensions', () => { |
| 680 | const content = ` |
| 681 |  |
| 682 |  |
| 683 |  |
| 684 |  |
| 685 |  |
| 686 |  |
| 687 |  |
| 688 |  |
| 689 |  |
| 690 |  |
| 691 | ` |
| 692 | const images = extractImagesUsage(content, {}) |
| 693 | expect(images).toHaveLength(10) |
| 694 | }) |
| 695 | |
| 696 | it('handles case-insensitive image extensions', () => { |
| 697 | const content = ` |
| 698 | <img src="/image.PNG" /> |
| 699 | <img src="/image.JpG" /> |
| 700 | <img src="/image.WEBP" /> |
| 701 | ` |
| 702 | const images = extractImagesUsage(content, {}) |
| 703 | expect(images).toEqual(['/image.PNG', '/image.JpG', '/image.WEBP']) |
| 704 | }) |
| 705 | }) |
| 706 | |
| 707 | describe('resolveConfig drawings', () => { |
| 708 | it('applies drawings defaults from the theme', () => { |
| 709 | const config = resolveConfig({}, { |
| 710 | defaults: { |
| 711 | drawings: { presenterOnly: true }, |
| 712 | }, |
| 713 | } as any) |
| 714 | expect(config.drawings).toEqual({ |
| 715 | enabled: true, |
| 716 | persist: false, |
| 717 | presenterOnly: true, |
| 718 | syncAll: true, |
| 719 | }) |
| 720 | }) |
| 721 | |
| 722 | it('applies drawings from the nested `config` headmatter key', () => { |
| 723 | const config = resolveConfig({ |
| 724 | config: { |
| 725 | drawings: { syncAll: false }, |
| 726 | }, |
| 727 | }) |
| 728 | expect(config.drawings.syncAll).toBe(false) |
| 729 | }) |
| 730 | |
| 731 | it('lets headmatter override the theme defaults', () => { |
| 732 | const config = resolveConfig( |
| 733 | { |
| 734 | drawings: { enabled: false }, |
| 735 | }, |
| 736 | { |
| 737 | defaults: { |
| 738 | drawings: { enabled: true, presenterOnly: true }, |
| 739 | }, |
| 740 | } as any, |
| 741 | ) |
| 742 | expect(config.drawings.enabled).toBe(false) |
| 743 | expect(config.drawings.presenterOnly).toBe(true) |
| 744 | }) |
| 745 | }) |
| 746 | |
| 747 | it('detects circular src imports without overflowing', async () => { |
| 748 | const root = resolve(__dirname, 'fixtures/markdown/circular') |
| 749 | const data = await load({ |
| 750 | userRoot: root, |
| 751 | roots: [root], |
| 752 | }, resolve(root, 'a.md')) |
| 753 | // Must return (not hang / overflow) and record a circular-import diagnostic |
| 754 | const errors = Object.values(data.markdownFiles).flatMap(md => md.errors ?? []) |
| 755 | expect(errors.some(e => /circular/i.test(e.message))).toBe(true) |
| 756 | }) |
| 757 | |
| 758 | it('records an error when a src: import selects a slide below the first one', async () => { |
| 759 | const root = resolve(__dirname, 'fixtures/markdown/out-of-range') |
| 760 | const data = await load({ |
| 761 | userRoot: root, |
| 762 | roots: [root], |
| 763 | }, resolve(root, 'entry.md')) |
| 764 | const errors = Object.values(data.markdownFiles).flatMap(md => md.errors ?? []) |
| 765 | expect(errors.map(e => e.message)).toEqual([ |
| 766 | expect.stringContaining('Slide 0 does not exist'), |
| 767 | ]) |
| 768 | }) |
| 769 | |
| 770 | it('records an error when a src: import escapes the allowed roots', async () => { |
| 771 | const root = resolve(__dirname, 'fixtures/markdown/escaping/root') |
| 772 | const data = await load({ |
| 773 | userRoot: root, |
| 774 | roots: [root], |
| 775 | allowedRoots: [root], |
| 776 | }, resolve(root, 'entry.md')) |
| 777 | const errors = Object.values(data.markdownFiles).flatMap(md => md.errors ?? []) |
| 778 | expect(errors.some(e => /escapes the project root/i.test(e.message))).toBe(true) |
| 779 | }) |
| 780 | }) |
| 781 |