Skip to content

Commit

Permalink
Merge pull request #123 from marp-team/collect-inline-svg-tokens
Browse files Browse the repository at this point in the history
Include inline SVG elements when rendered with htmlAsArray env
  • Loading branch information
yhatt authored Jan 25, 2019
2 parents 8cc85ee + 78e2de8 commit d739880
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 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

- Include inline SVG elements when rendered with `htmlAsArray` env ([#123](https://github.com/marp-team/marpit/pull/123))

### Changed

- Small update for README and docs ([#122](https://github.com/marp-team/marpit/pull/122))
Expand Down
17 changes: 6 additions & 11 deletions src/markdown/collect.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function collect(md, marpit) {
marpit.lastSlideTokens = []

let currentPage
let pageIdx = -1

const collectComment = token => {
if (
Expand All @@ -33,21 +34,15 @@ function collect(md, marpit) {
currentPage >= 0 && marpit.lastSlideTokens[currentPage] !== undefined

for (const token of state.tokens) {
if (
token.type === 'marpit_slide_open' &&
token.meta &&
token.meta.marpitSlide !== undefined
) {
currentPage = token.meta.marpitSlide
if (token.meta && token.meta.marpitSlideElement === 1) {
pageIdx += 1
currentPage = pageIdx

if (
currentPage >= 0 &&
marpit.lastSlideTokens[currentPage] === undefined
) {
if (marpit.lastSlideTokens[currentPage] === undefined) {
marpit.lastSlideTokens[currentPage] = [token]
marpit.lastComments[currentPage] = []
}
} else if (token.type === 'marpit_slide_close') {
} else if (token.meta && token.meta.marpitSlideElement === -1) {
if (collectable()) marpit.lastSlideTokens[currentPage].push(token)
currentPage = undefined
} else {
Expand Down
30 changes: 27 additions & 3 deletions test/markdown/collect.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ import MarkdownIt from 'markdown-it'
import applyDirectives from '../../src/markdown/directives/apply'
import collect from '../../src/markdown/collect'
import comment from '../../src/markdown/comment'
import inlineSVG from '../../src/markdown/inline_svg'
import parseDirectives from '../../src/markdown/directives/parse'
import slide from '../../src/markdown/slide'
import { ThemeSet } from '../../src/index'

describe('Marpit collect plugin', () => {
const themeSet = new Map()
themeSet.set('default', true)
const themeSet = new ThemeSet()

const marpitStub = () => ({
const marpitStub = (svg = false) => ({
themeSet,
lastGlobalDirectives: {},
options: { inlineSVG: svg },
})

const md = marpitInstance =>
Expand All @@ -22,6 +24,7 @@ describe('Marpit collect plugin', () => {
.use(parseDirectives, { themeSet: marpitInstance.themeSet })
.use(applyDirectives)
.use(collect, marpitInstance)
.use(inlineSVG, marpitInstance)

const text = dedent`
---
Expand Down Expand Up @@ -141,4 +144,25 @@ describe('Marpit collect plugin', () => {
})
}
)

context('with inline SVG mode', () => {
it('includes inline SVG tokens in collected result', () => {
const marpit = marpitStub()
const marpitSVG = marpitStub(true)

md(marpit).render(text)
md(marpitSVG).render(text)

expect(marpitSVG.lastComments).toStrictEqual(marpit.lastComments)
expect(marpitSVG.lastSlideTokens).toHaveLength(4)

for (const tokens of marpitSVG.lastSlideTokens) {
const [open] = tokens
const [close] = tokens.slice(-1)

expect(open.type).toBe('marpit_inline_svg_open')
expect(close.type).toBe('marpit_inline_svg_close')
}
})
})
})
10 changes: 10 additions & 0 deletions test/marpit.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ describe('Marpit', () => {
expect(countDecl(ret.root, '--theme-defined')).toBe(1)
})
})

context('when passed htmlAsArray env', () => {
it('outputs HTML including inline SVG as array', () => {
const { html } = instance(true).render('# Hi', { htmlAsArray: true })
expect(html).toHaveLength(1)

const $ = cheerio.load(html[0], { lowerCaseTags: false })
expect($('svg > foreignObject')).toHaveLength(1)
})
})
})

context('with backgroundSyntax option', () => {
Expand Down

0 comments on commit d739880

Please sign in to comment.