diff --git a/src/markdown/directives/apply.js b/src/markdown/directives/apply.js index ee0105e..687aadb 100644 --- a/src/markdown/directives/apply.js +++ b/src/markdown/directives/apply.js @@ -34,28 +34,24 @@ function _apply(md, opts = {}) { (state) => { if (state.inlineMode) return - // compute the total number of skipped pages - let totalSkippedSlides = 0 - for (const token of state.tokens) { - const { marpitDirectives } = token.meta || {} - if ( - marpitDirectives && - (marpitDirectives.paginate === 'hold' || - marpitDirectives.paginate === 'skip') - ) { - totalSkippedSlides++ - } - } + let pageNumber = 0 - // keep track of slides that were skipped using one of the following - // directives: - // `paginate: skip`, `_paginate: skip`, - // `paginate: hold`, or `_paginate: hold - let currentSkippedSlides = 0 + const tokensForPaginationTotal = [] for (const token of state.tokens) { - const { marpitDirectives, marpitSlide, marpitSlideTotal } = - token.meta || {} + const { marpitDirectives, marpitSlideElement } = token.meta || {} + + if (marpitSlideElement === 1) { + // `skip` and `hold` disable increment of the page number + if ( + !( + marpitDirectives?.paginate === 'skip' || + marpitDirectives?.paginate === 'hold' + ) + ) { + pageNumber += 1 + } + } if (marpitDirectives) { const style = new InlineStyle(token.attrGet('style')) @@ -103,24 +99,12 @@ function _apply(md, opts = {}) { style.set('background-size', marpitDirectives.backgroundSize) } - if (marpitDirectives.paginate) { - if ( - marpitDirectives.paginate === 'hold' || - marpitDirectives.paginate === 'skip' - ) { - currentSkippedSlides++ - } - - if (marpitDirectives.paginate !== 'skip') { - token.attrSet( - 'data-marpit-pagination', - marpitSlide - currentSkippedSlides + 1, - ) - token.attrSet( - 'data-marpit-pagination-total', - marpitSlideTotal - totalSkippedSlides, - ) - } + if ( + marpitDirectives.paginate && + marpitDirectives.paginate !== 'skip' + ) { + token.attrSet('data-marpit-pagination', pageNumber) + tokensForPaginationTotal.push(token) } if (marpitDirectives.header) @@ -133,6 +117,11 @@ function _apply(md, opts = {}) { if (styleStr !== '') token.attrSet('style', styleStr) } } + + // Set total page number to each slide + for (const token of tokensForPaginationTotal) { + token.attrSet('data-marpit-pagination-total', pageNumber) + } }, ) } diff --git a/src/markdown/directives/directives.js b/src/markdown/directives/directives.js index 8bda455..f85e370 100644 --- a/src/markdown/directives/directives.js +++ b/src/markdown/directives/directives.js @@ -67,6 +67,8 @@ export const globals = Object.assign(Object.create(null), { * @prop {Directive} header Specify the content of slide header. It will insert * a `
` element to the first of each slide contents. * @prop {Directive} paginate Show page number on the slide if you set `true`. + * `hold` and `skip` are also available to disable increment of the page + * number. */ export const locals = Object.assign(Object.create(null), { backgroundColor: (v) => ({ backgroundColor: v }), @@ -78,11 +80,13 @@ export const locals = Object.assign(Object.create(null), { color: (v) => ({ color: v }), footer: (v) => (typeof v === 'string' ? { footer: v } : {}), header: (v) => (typeof v === 'string' ? { header: v } : {}), - paginate: (v) => ({ - paginate: ['hold', 'skip'].includes(v) - ? v - : (v || '').toLowerCase() === 'true', - }), + paginate: (v) => { + const normalized = (v || '').toLowerCase() + + if (['hold', 'skip'].includes(normalized)) return { paginate: normalized } + + return { paginate: normalized === 'true' } + }, }) export default [...Object.keys(globals), ...Object.keys(locals)]