From 4d39cc1b72a96b808b1d8ea07da6adaa5bc0033f Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Thu, 6 Sep 2018 12:50:43 +0900 Subject: [PATCH 1/4] Update Marpit header/footer plugin: Use plain tokens instead of rendered --- src/markdown/header_and_footer.js | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/markdown/header_and_footer.js b/src/markdown/header_and_footer.js index 821e82e6..dc03bc63 100644 --- a/src/markdown/header_and_footer.js +++ b/src/markdown/header_and_footer.js @@ -1,5 +1,4 @@ /** @module */ -import Token from 'markdown-it/lib/token' import wrapTokens from '../helpers/wrap_tokens' /** @@ -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 From 5bf5f790626e363551e9cce7045f3685579b92e1 Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Thu, 6 Sep 2018 13:47:35 +0900 Subject: [PATCH 2/4] Validate header and footer directive whether it is defined as a string --- src/markdown/directives/directives.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/markdown/directives/directives.js b/src/markdown/directives/directives.js index 52f895c3..c86e0760 100644 --- a/src/markdown/directives/directives.js +++ b/src/markdown/directives/directives.js @@ -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' } From a50f558ff0b51cad61b3ec983eeccf5cd4fbaf55 Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Thu, 6 Sep 2018 13:51:50 +0900 Subject: [PATCH 3/4] Add test about validation of header and footer directives --- test/markdown/header_and_footer.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/markdown/header_and_footer.js b/test/markdown/header_and_footer.js index b95a59f4..8ac595ac 100644 --- a/test/markdown/header_and_footer.js +++ b/test/markdown/header_and_footer.js @@ -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('')) + expect($('header')).toHaveLength(0) + }) }) describe('Footer local directive', () => { @@ -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('')) + expect($('footer')).toHaveLength(0) + }) }) }) From 6ecfc259310cb36dee66120bcdb4256953aa175f Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Thu, 6 Sep 2018 16:16:24 +0900 Subject: [PATCH 4/4] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index abc92cac..a8d5376d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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