Skip to content

Commit 9aeae40

Browse files
mahirhirMahiro Hirakawa
andauthored
fix(parser): report a src: range that points below the first slide (#2734)
Co-authored-by: Mahiro Hirakawa <mahirohirakawa@glovrex.com>
1 parent 612476d commit 9aeae40

4 files changed

Lines changed: 29 additions & 0 deletions

File tree

packages/parser/src/fs.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ export async function load(
9898
const directImporter = importers?.at(-1)
9999
for (const index of parseRangeString(md.slides.length, range)) {
100100
const subSlide = md.slides[index - 1]
101+
if (!subSlide) {
102+
// `parseRangeString` only drops indexes above the total, so a range
103+
// such as `#0` or `#-3` still reaches here and has no slide.
104+
md.errors ??= []
105+
md.errors.push({
106+
row: 0,
107+
message: `Slide ${index} does not exist in "${path}", which has ${md.slides.length} slides`,
108+
})
109+
continue
110+
}
101111
try {
102112
await loadSlide(md, subSlide, frontmatterOverride, importers)
103113
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Entry
2+
3+
---
4+
src: ./sub.md#0
5+
---
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# One
2+
3+
---
4+
5+
# Two

test/parser.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,15 @@ Some content
594594
expect(errors.some(e => /circular/i.test(e.message))).toBe(true)
595595
})
596596

597+
it('records an error when a src: import selects a slide below the first one', async () => {
598+
const root = resolve(__dirname, 'fixtures/markdown/out-of-range')
599+
const data = await load({ userRoot: root, roots: [root] }, resolve(root, 'entry.md'))
600+
const errors = Object.values(data.markdownFiles).flatMap(md => md.errors ?? [])
601+
expect(errors.map(e => e.message)).toEqual([
602+
expect.stringContaining('Slide 0 does not exist'),
603+
])
604+
})
605+
597606
it('records an error when a src: import escapes the allowed roots', async () => {
598607
const root = resolve(__dirname, 'fixtures/markdown/escaping/root')
599608
const data = await load({ userRoot: root, roots: [root], allowedRoots: [root] }, resolve(root, 'entry.md'))

0 commit comments

Comments
 (0)