Skip to content

Commit

Permalink
Merge pull request #66 from marp-team/header-footer-token
Browse files Browse the repository at this point in the history
Improve rendering header and footer to use inline tokens
  • Loading branch information
yhatt authored Sep 6, 2018
2 parents ea2a1c3 + 6ecfc25 commit 93f39af
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 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

- Improve rendering header and footer to use inline tokens ([#66](https://github.com/marp-team/marpit/pull/66))

## v0.0.14 - 2018-09-02

### Added
Expand Down
4 changes: 2 additions & 2 deletions src/markdown/directives/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ export const locals = {
return { color: value }
},
footer(value) {
return { footer: value }
return typeof value === 'string' ? { footer: value } : {}
},
header(value) {
return { header: value }
return typeof value === 'string' ? { header: value } : {}
},
paginate(value) {
return { paginate: (value || '').toLowerCase() === 'true' }
Expand Down
31 changes: 15 additions & 16 deletions src/markdown/header_and_footer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/** @module */
import Token from 'markdown-it/lib/token'
import wrapTokens from '../helpers/wrap_tokens'

/**
Expand All @@ -17,26 +16,26 @@ function headerAndFooter(md) {
state => {
if (state.inlineMode) return

const renderedInlines = new Map()
const getRendered = markdown => {
let rendered = renderedInlines.get(markdown)
const parsedInlines = new Map()
const getParsed = markdown => {
let parsed = parsedInlines.get(markdown)

if (!rendered) {
rendered = md.renderInline(markdown, state.env)
renderedInlines.set(markdown, rendered)
if (!parsed) {
parsed = md.parseInline(markdown, state.env)
delete parsed.map

parsedInlines.set(markdown, parsed)
}

return rendered
return parsed
}

const createMarginalTokens = (tag, markdown) => {
const token = new Token('html_block', '', 0)
token.content = getRendered(markdown)

return wrapTokens(`marpit_${tag}`, { tag, close: { block: true } }, [
token,
])
}
const createMarginalTokens = (tag, markdown) =>
wrapTokens(
`marpit_${tag}`,
{ tag, close: { block: true } },
getParsed(markdown)
)

let current

Expand Down
10 changes: 10 additions & 0 deletions test/markdown/header_and_footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ describe('Marpit header and footer plugin', () => {
expect(img.attr('src')).toBe('https://example.com/image.jpg')
})
})

it('ignores invalid directives like defined as object', () => {
const $ = cheerio.load(md().render('<!-- header: ["test"] -->'))
expect($('header')).toHaveLength(0)
})
})

describe('Footer local directive', () => {
Expand Down Expand Up @@ -92,5 +97,10 @@ describe('Marpit header and footer plugin', () => {
expect(img.attr('src')).toBe('https://example.com/image.jpg')
})
})

it('ignores invalid directives like defined as object', () => {
const $ = cheerio.load(md().render('<!-- footer: ["test"] -->'))
expect($('footer')).toHaveLength(0)
})
})
})

0 comments on commit 93f39af

Please sign in to comment.