Skip to content

Commit

Permalink
Merge pull request #151 from marp-team/heading-divider-map
Browse files Browse the repository at this point in the history
Update slide plugin and heading divider plugin to apply the correct mapped line of slides
  • Loading branch information
yhatt authored Apr 8, 2019
2 parents ad7564b + 250d2aa commit 0304146
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 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

- Update slide plugin and heading divider plugin to apply the correct mapped line of slides ([#151](https://github.com/marp-team/marpit/pull/151))

## v0.9.1 - 2019-04-05

### Added
Expand Down
1 change: 1 addition & 0 deletions src/markdown/heading_divider.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function headingDivider(md) {
if (token && splitFunc(token) && newTokens.some(t => !t.hidden)) {
const hr = new state.Token('hr', '', 0)
hr.hidden = true
hr.map = token.map

newTokens.push(hr)
}
Expand Down
11 changes: 8 additions & 3 deletions src/markdown/slide.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ function slide(md, opts = {}) {

state.tokens = split(state.tokens, t => t.type === 'hr', true).reduce(
(arr, slideTokens, idx) => {
const hrFirst = slideTokens[0] && slideTokens[0].type === 'hr'
const firstHr =
slideTokens[0] && slideTokens[0].type === 'hr'
? slideTokens[0]
: undefined

const mapTarget = firstHr || slideTokens.find(t => t.map)

return [
...arr,
Expand All @@ -52,14 +57,14 @@ function slide(md, opts = {}) {
open: {
block: true,
meta: { marpitSlide: idx, marpitSlideElement: 1 },
map: hrFirst ? slideTokens[0].map : undefined,
map: mapTarget ? mapTarget.map : [0, 1],
},
close: {
block: true,
meta: { marpitSlide: idx, marpitSlideElement: -1 },
},
},
slideTokens.slice(hrFirst ? 1 : 0)
slideTokens.slice(firstHr ? 1 : 0)
),
]
},
Expand Down
12 changes: 12 additions & 0 deletions test/markdown/heading_divider.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ describe('Marpit heading divider plugin', () => {
const $ = cheerio.load(mdWithSlide(marpitStub(4)).render(markdownText))
expect($('section')).toHaveLength(4)
})

it('maps corresponded line of slide to heading', () => {
const tokens = mdWithSlide(marpitStub(4)).parse(markdownText)
const [first, second, third, fourth] = tokens.filter(
t => t.type === 'marpit_slide_open'
)

expect(first.map).toStrictEqual([0, 1])
expect(second.map).toStrictEqual([1, 2])
expect(third.map).toStrictEqual([2, 3])
expect(fourth.map).toStrictEqual([3, 4])
})
})

context('with invalid headingDivider option', () => {
Expand Down
21 changes: 16 additions & 5 deletions test/markdown/slide.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('Marpit slide plugin', () => {

return instance.use(slide, ...args)
}
const multiMd = '# foo\n\n---\n\n## bar'

context('with default options', () => {
const markdown = md()
Expand All @@ -19,12 +20,24 @@ describe('Marpit slide plugin', () => {
expect($('section#1')).toHaveLength(1)

// Multi page
const $multi = cheerio.load(markdown.render('# foo\n\n---\n\n## bar'))
const $multi = cheerio.load(markdown.render(multiMd))
expect($multi('section')).toHaveLength(2)
expect($multi('section#1 > h1').text()).toBe('foo')
expect($multi('section#2 > h2').text()).toBe('bar')
})

it('maps corresponded line of slide', () => {
const [open] = markdown.parse('')
expect(open.map).toStrictEqual([0, 1])

const [first, second] = markdown
.parse(multiMd)
.filter(t => t.type === 'marpit_slide_open')

expect(first.map).toStrictEqual([0, 1])
expect(second.map).toStrictEqual([2, 3])
})

it('ignores in #renderInline', () => {
const $ = cheerio.load(md().renderInline(''))
expect($('section')).toHaveLength(0)
Expand All @@ -38,7 +51,7 @@ describe('Marpit slide plugin', () => {
const $ = cheerio.load(markdown.render(''))
expect($('section.page#1[tabindex=-1]')).toHaveLength(1)

const $multi = cheerio.load(markdown.render('# foo\n\n---\n\n## bar'))
const $multi = cheerio.load(markdown.render(multiMd))
expect($multi('section.page#1[tabindex=-1] > h1').text()).toBe('foo')
expect($multi('section.page#2[tabindex=-1] > h2').text()).toBe('bar')
})
Expand All @@ -61,9 +74,7 @@ describe('Marpit slide plugin', () => {
const $ = cheerio.load(markdown(i => `page${i + 1}`).render(''))
expect($('section#page1')).toHaveLength(1)

const $multi = cheerio.load(
markdown(i => (i + 1) * 2).render('# foo\n\n---\n\n## bar')
)
const $multi = cheerio.load(markdown(i => (i + 1) * 2).render(multiMd))
expect($multi('section#2 > h1').text()).toBe('foo')
expect($multi('section#4 > h2').text()).toBe('bar')
})
Expand Down

0 comments on commit 0304146

Please sign in to comment.