From 9c495ca30306dca94b6df8b9b3a57d2478917839 Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Mon, 8 Apr 2019 10:26:02 +0900 Subject: [PATCH 1/5] Update heading divider plugin to apply map prop --- src/markdown/heading_divider.js | 1 + test/markdown/heading_divider.js | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/markdown/heading_divider.js b/src/markdown/heading_divider.js index 78ddfc86..aee0fba4 100644 --- a/src/markdown/heading_divider.js +++ b/src/markdown/heading_divider.js @@ -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) } diff --git a/test/markdown/heading_divider.js b/test/markdown/heading_divider.js index a851efec..1e7a999c 100644 --- a/test/markdown/heading_divider.js +++ b/test/markdown/heading_divider.js @@ -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', () => { From 445c952a066052f511d8622469e5dc01e8766d67 Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Mon, 8 Apr 2019 14:55:15 +0900 Subject: [PATCH 2/5] Fix resolution logic of target token for mapping --- src/markdown/slide.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/markdown/slide.js b/src/markdown/slide.js index 3003b217..d23e4571 100644 --- a/src/markdown/slide.js +++ b/src/markdown/slide.js @@ -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, @@ -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 : undefined, }, close: { block: true, meta: { marpitSlide: idx, marpitSlideElement: -1 }, }, }, - slideTokens.slice(hrFirst ? 1 : 0) + slideTokens.slice(firstHr ? 1 : 0) ), ] }, From 260c6f9af88a5a18926bab4e8f14d70ff2d62f5c Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Mon, 8 Apr 2019 15:03:13 +0900 Subject: [PATCH 3/5] Add unit test for slide plugin about mapped line to slide --- test/markdown/slide.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/test/markdown/slide.js b/test/markdown/slide.js index b8557251..0c61a9db 100644 --- a/test/markdown/slide.js +++ b/test/markdown/slide.js @@ -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() @@ -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) @@ -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') }) @@ -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') }) From c62a68ac6b1f46932fcaf4e496ab0fadb453c482 Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Mon, 8 Apr 2019 15:24:26 +0900 Subject: [PATCH 4/5] Update slide plugin to map to first line as fallback --- src/markdown/slide.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/markdown/slide.js b/src/markdown/slide.js index d23e4571..9a163097 100644 --- a/src/markdown/slide.js +++ b/src/markdown/slide.js @@ -57,7 +57,7 @@ function slide(md, opts = {}) { open: { block: true, meta: { marpitSlide: idx, marpitSlideElement: 1 }, - map: mapTarget ? mapTarget.map : undefined, + map: mapTarget ? mapTarget.map : [0, 1], }, close: { block: true, From 250d2aab5f25103706447309215b31ccbecb9105 Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Mon, 8 Apr 2019 15:37:22 +0900 Subject: [PATCH 5/5] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f6645c5..808a886e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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