Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve rendering header and footer to use inline tokens #66

Merged
merged 4 commits into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})
})