Skip to content

Commit

Permalink
fix: export pages range
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 10, 2021
1 parent 2603ba1 commit 3378281
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 15 deletions.
5 changes: 3 additions & 2 deletions docs/.vitepress/theme/components/ThemeInfo.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import { defineProps, ref } from 'vue'
import { useIntervalFn } from '@vueuse/core'
import { isClient, useIntervalFn } from '@vueuse/core'
import type { ThemeInfo } from '../../themes'
const props = defineProps<{
Expand All @@ -9,10 +9,11 @@ const props = defineProps<{
const index = ref(0)
if (props.theme.previews.length > 1) {
if (props.theme.previews.length > 1 && isClient) {
const { resume } = useIntervalFn(() => {
index.value = (index.value + 1) % props.theme.previews.length
}, 3000, false)
// add random defer so they don't starts together
setTimeout(resume, Math.round(1000 * Math.random()))
}
</script>
Expand Down
2 changes: 1 addition & 1 deletion packages/client/builtin/CodeHighlightController.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ onMounted(() => {
const lines = Array.from(target.querySelectorAll('.line'))
const highlights: number[] = parseRangeString(lines.length, rangeStr.value)
lines.forEach((line, idx) => {
const highlighted = highlights.includes(idx)
const highlighted = highlights.includes(idx + 1)
line.classList.toggle(CLASS_VCLICK_TARGET, true)
line.classList.toggle('highlighted', highlighted)
line.classList.toggle('dishonored', !highlighted)
Expand Down
8 changes: 4 additions & 4 deletions packages/parser/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,20 @@ export function parse(
*/
export function parseRangeString(total: number, rangeStr?: string) {
if (!rangeStr || rangeStr === 'all' || rangeStr === '*')
return range(total)
return range(1, total + 1)

const pages: number[] = []
for (const part of rangeStr.split(/[,;]/g)) {
if (!part.includes('-')) {
pages.push(+part - 1)
pages.push(+part)
}
else {
const [start, end] = part.split('-', 2)
pages.push(
...range(+start - 1, end === '' ? total : +end),
...range(+start, !end ? (total + 1) : (+end + 1)),
)
}
}

return uniq(pages).sort().filter(i => i < total)
return uniq(pages).filter(i => i <= total).sort((a, b) => a - b)
}
4 changes: 2 additions & 2 deletions packages/slidev/node/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export async function exportSlides({
const progress = createSlidevProgress()

async function go(no: number) {
progress.update(no + 1)
progress.update(no)
await page.goto(`http://localhost:${port}${base}${no}?print`, {
waitUntil: 'networkidle',
})
Expand Down Expand Up @@ -130,7 +130,7 @@ export async function exportSlides({
await go(i)
await page.screenshot({
omitBackground: false,
path: path.join(output, `${(i + 1).toString().padStart(2, '0')}.png`),
path: path.join(output, `${i.toString().padStart(2, '0')}.png`),
})
}
}
Expand Down
13 changes: 7 additions & 6 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { parseRangeString } from '../packages/parser/src'

describe('utils', () => {
it('page-range', () => {
expect(parseRangeString(5)).toEqual([0, 1, 2, 3, 4])
expect(parseRangeString(5, 'all')).toEqual([0, 1, 2, 3, 4])
expect(parseRangeString(5, '1')).toEqual([0])
expect(parseRangeString(10, '1,2-3,5')).toEqual([0, 1, 2, 4])
expect(parseRangeString(10, '1;2-3;5')).toEqual([0, 1, 2, 4])
expect(parseRangeString(10, '6-')).toEqual([5, 6, 7, 8, 9])
expect(parseRangeString(5)).toEqual([1, 2, 3, 4, 5])
expect(parseRangeString(5, 'all')).toEqual([1, 2, 3, 4, 5])
expect(parseRangeString(2, '*')).toEqual([1, 2])
expect(parseRangeString(5, '1')).toEqual([1])
expect(parseRangeString(10, '1,2-3,5')).toEqual([1, 2, 3, 5])
expect(parseRangeString(10, '1;2-3;5')).toEqual([1, 2, 3, 5])
expect(parseRangeString(10, '6-')).toEqual([6, 7, 8, 9, 10])
})
})

0 comments on commit 3378281

Please sign in to comment.