Skip to content

Commit

Permalink
Merge pull request #372 from marp-team/regression-paginate-skip-and-h…
Browse files Browse the repository at this point in the history
…old-for-inline-svg

Fix blocking `paginate: skip` and `hold` in v2.5.1 inline SVG mode
  • Loading branch information
yhatt authored Sep 11, 2023
2 parents 60d5a4b + d6e6cd9 commit 9c5558c
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 116 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Fixed

- Fix blocking `paginate: skip` and `hold` in v2.5.1 inline SVG mode ([#372](https://github.com/marp-team/marpit/pull/372))

## v2.5.1 - 2023-09-09

### Added
Expand Down
4 changes: 2 additions & 2 deletions src/markdown/directives/apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ function _apply(md, opts = {}) {
const tokensForPaginationTotal = []

for (const token of state.tokens) {
const { marpitDirectives, marpitSlideElement } = token.meta || {}
const { marpitDirectives } = token.meta || {}

if (marpitSlideElement === 1) {
if (token.type === 'marpit_slide_open') {
// `skip` and `hold` disable increment of the page number
if (
!(
Expand Down
253 changes: 139 additions & 114 deletions test/markdown/directives/apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,50 @@ import MarkdownIt from 'markdown-it'
import { comment } from '../../../src/markdown/comment'
import applyDirectives from '../../../src/markdown/directives/apply'
import parseDirectives from '../../../src/markdown/directives/parse'
import { inlineSVG } from '../../../src/markdown/inline_svg'
import { slide } from '../../../src/markdown/slide'

describe('Marpit directives apply plugin', () => {
const themeSetStub = new Map()
const themeSetStub = Object.assign(new Map(), {
getThemeProp: () => 256,
})
themeSetStub.set('test_theme', true)

const md = (...args) => {
const md = (marpitInstanceOpts, ...args) => {
const customDirectives = { global: {}, local: {} }
const instance = new MarkdownIt('commonmark')

instance.marpit = {
customDirectives,
options: { looseYAML: false },
themeSet: themeSetStub,
...marpitInstanceOpts,
}

return instance
.use(comment)
.use(slide)
.use(parseDirectives)
.use(applyDirectives, ...args)
.use(inlineSVG)
}

const mdForTest = (...args) =>
md(...args).use((mdInstance) => {
mdInstance.core.ruler.before(
'marpit_directives_apply',
'marpit_directives_apply_test',
(state) => {
state.tokens.forEach((token) => {
if (token.meta && token.meta.marpitDirectives) {
// Internal directive
token.meta.marpitDirectives.unknownDir = 'directive'
}
})
},
)
})
const mdTestPlugin = (mdInstance) => {
mdInstance.core.ruler.before(
'marpit_directives_apply',
'marpit_directives_apply_test',
(state) => {
state.tokens.forEach((token) => {
if (token.meta && token.meta.marpitDirectives) {
// Internal directive
token.meta.marpitDirectives.unknownDir = 'directive'
}
})
},
)
}

const mdForTest = (...args) => md({}, ...args).use(mdTestPlugin)

const basicDirs = dedent`
---
Expand Down Expand Up @@ -239,141 +245,160 @@ describe('Marpit directives apply plugin', () => {
})

describe('Paginate with skipped slides', () => {
it('applies data-marpit-pagination attribute with a _paginate: hold slide', () => {
const paginateDirs = dedent`
---
paginate: true
_paginate:
---
for (const inlineSVGmode of [true, false]) {
const mdWithSVG = () =>
md({ inlineSVGOptions: { enabled: inlineSVGmode } }).use(mdTestPlugin)

# Slide 1
context(
`with inlineSVG mode as ${inlineSVGmode ? 'true' : 'false'}`,
() => {
it('applies data-marpit-pagination attribute with a _paginate: hold slide', () => {
const paginateDirs = dedent`
---
paginate: true
_paginate:
---
- Page is counted (1 of 2)
- Pagination is not rendered
# Slide 1
---
- Page is counted (1 of 2)
- Pagination is not rendered
<!--
_paginate: hold
-->
---
## Slide 2
<!--
_paginate: hold
-->
- Page is not counted
- Pagination is rendered (1 of 2)
## Slide 2
---
- Page is not counted
- Pagination is rendered (1 of 2)
## Slide 3
---
- Page is counted
- Pagination is rendered (2 of 2)
`
## Slide 3
const $ = load(mdForTest().render(paginateDirs))
const sections = $('section')
- Page is counted
- Pagination is rendered (2 of 2)
`

expect(sections.eq(0).data('marpit-pagination')).toBeUndefined()
expect(sections.eq(0).data('marpit-pagination-total')).toBeUndefined()
expect(sections.eq(1).data('marpit-pagination')).toBe(1)
expect(sections.eq(1).data('marpit-pagination-total')).toBe(2)
expect(sections.eq(2).data('marpit-pagination')).toBe(2)
expect(sections.eq(2).data('marpit-pagination-total')).toBe(2)
})
const $ = load(mdWithSVG().render(paginateDirs))
console.log($.html())
const sections = $('section')

it('applies data-marpit-pagination attribute with a _paginate: skip slide', () => {
const paginateDirs = dedent`
---
paginate: true
_paginate:
---
expect(sections.eq(0).data('marpit-pagination')).toBeUndefined()
expect(
sections.eq(0).data('marpit-pagination-total'),
).toBeUndefined()
expect(sections.eq(1).data('marpit-pagination')).toBe(1)
expect(sections.eq(1).data('marpit-pagination-total')).toBe(2)
expect(sections.eq(2).data('marpit-pagination')).toBe(2)
expect(sections.eq(2).data('marpit-pagination-total')).toBe(2)
})

# Slide 1
it('applies data-marpit-pagination attribute with a _paginate: skip slide', () => {
const paginateDirs = dedent`
---
paginate: true
_paginate:
---
- Page is counted (1 of 2)
- Pagination is not rendered
# Slide 1
---
- Page is counted (1 of 2)
- Pagination is not rendered
<!--
_paginate: skip
-->
---
## Slide 2
<!--
_paginate: skip
-->
- Page is not counted
- Pagination is not rendered (1 of 2)
## Slide 2
---
- Page is not counted
- Pagination is not rendered (1 of 2)
## Slide 3
---
- Page is counted
- Pagination is rendered (2 of 2)
`
## Slide 3
const $ = load(mdForTest().render(paginateDirs))
const sections = $('section')
- Page is counted
- Pagination is rendered (2 of 2)
`

expect(sections.eq(0).data('marpit-pagination')).toBeUndefined()
expect(sections.eq(0).data('marpit-pagination-total')).toBeUndefined()
expect(sections.eq(1).data('marpit-pagination')).toBeUndefined()
expect(sections.eq(1).data('marpit-pagination-total')).toBeUndefined()
expect(sections.eq(2).data('marpit-pagination')).toBe(2)
expect(sections.eq(2).data('marpit-pagination-total')).toBe(2)
})
const $ = load(mdWithSVG().render(paginateDirs))
const sections = $('section')

context('when paginate: hold is applied from beginning', () => {
const paginateDirs = dedent`
---
paginate: hold
---
expect(sections.eq(0).data('marpit-pagination')).toBeUndefined()
expect(
sections.eq(0).data('marpit-pagination-total'),
).toBeUndefined()
expect(sections.eq(1).data('marpit-pagination')).toBeUndefined()
expect(
sections.eq(1).data('marpit-pagination-total'),
).toBeUndefined()
expect(sections.eq(2).data('marpit-pagination')).toBe(2)
expect(sections.eq(2).data('marpit-pagination-total')).toBe(2)
})

# Slide 1
context('when paginate: hold is applied from beginning', () => {
const paginateDirs = dedent`
---
paginate: hold
---
- Page is not incremented (1 of 3)
- Pagination rendered
# Slide 1
---
- Page is not incremented (1 of 3)
- Pagination rendered
## Slide 2
---
- Page is not incremented (1 of 3)
- Pagination rendered
## Slide 2
---
- Page is not incremented (1 of 3)
- Pagination rendered
<!-- paginate: true -->
---
## Slide 3
<!-- paginate: true -->
- Page is incremented (2 of 3)
- Pagination rendered
## Slide 3
---
- Page is incremented (2 of 3)
- Pagination rendered
<!-- paginate: false -->
---
## Slide 4
<!-- paginate: false -->
- Page is incremented (3 of 3)
- Pagination is not rendered
`
## Slide 4
it('applies data-marpit-pagination and data-marpit-pagination-total attribute correctly', () => {
const $ = load(mdForTest().render(paginateDirs))
const sections = $('section')

expect(sections.eq(0).data('marpit-pagination')).toBe(1)
expect(sections.eq(0).data('marpit-pagination-total')).toBe(3)
expect(sections.eq(1).data('marpit-pagination')).toBe(1)
expect(sections.eq(1).data('marpit-pagination-total')).toBe(3)
expect(sections.eq(2).data('marpit-pagination')).toBe(2)
expect(sections.eq(2).data('marpit-pagination-total')).toBe(3)
expect(sections.eq(3).data('marpit-pagination')).toBeUndefined()
expect(sections.eq(3).data('marpit-pagination-total')).toBeUndefined()
})
})
- Page is incremented (3 of 3)
- Pagination is not rendered
`

it('applies data-marpit-pagination and data-marpit-pagination-total attribute correctly', () => {
const $ = load(mdWithSVG().render(paginateDirs))
const sections = $('section')

expect(sections.eq(0).data('marpit-pagination')).toBe(1)
expect(sections.eq(0).data('marpit-pagination-total')).toBe(3)
expect(sections.eq(1).data('marpit-pagination')).toBe(1)
expect(sections.eq(1).data('marpit-pagination-total')).toBe(3)
expect(sections.eq(2).data('marpit-pagination')).toBe(2)
expect(sections.eq(2).data('marpit-pagination-total')).toBe(3)
expect(sections.eq(3).data('marpit-pagination')).toBeUndefined()
expect(
sections.eq(3).data('marpit-pagination-total'),
).toBeUndefined()
})
})
},
)
}
})
})
})

0 comments on commit 9c5558c

Please sign in to comment.