From ab8f32c82127d849c6aa358afd107cd0eddbc9d2 Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Thu, 10 Oct 2019 09:54:06 +0900 Subject: [PATCH 1/3] Mark well-known magic comments as parsed token Marpit collect plugin won't collect comments that have marpitCommentParsed meta. So comment plugin detects well-known magic comments and marks as parsed. --- src/markdown/comment.js | 28 ++++++- src/markdown/directives/parse.js | 30 ++++---- test/markdown/collect.js | 127 ++++++++++++++++++++++--------- 3 files changed, 130 insertions(+), 55 deletions(-) diff --git a/src/markdown/comment.js b/src/markdown/comment.js index 333fa883..21f203e2 100644 --- a/src/markdown/comment.js +++ b/src/markdown/comment.js @@ -6,6 +6,22 @@ const commentMatcher = // +const magicCommentMatchers = [ + // Prettier + /^prettier-ignore(-(start|end))?$/, + + // markdownlint + /^markdownlint-((disable|enable).*|capture|restore)$/, + + // remark-lint (remark-message-control) + /^lint (disable|enable|ignore).*$/, +] + +export function markAsParsed(token, kind) { + token.meta = token.meta || {} + token.meta.marpitCommentParsed = kind +} + /** * Marpit comment plugin. * @@ -19,9 +35,15 @@ function comment(md) { const parse = (token, content) => { const parsed = yaml(content, !!md.marpit.options.looseYAML) - token.meta = { - ...(token.meta || {}), - marpitParsedDirectives: parsed === false ? {} : parsed, + token.meta = token.meta || {} + token.meta.marpitParsedDirectives = parsed === false ? {} : parsed + + // Mark well-known magic comments as parsed comment + for (const magicCommentMatcher of magicCommentMatchers) { + if (magicCommentMatcher.test(content.trim())) { + markAsParsed(token, 'well-known-magic-comment') + break + } } } diff --git a/src/markdown/directives/parse.js b/src/markdown/directives/parse.js index f978d8f2..29d8c685 100644 --- a/src/markdown/directives/parse.js +++ b/src/markdown/directives/parse.js @@ -2,16 +2,12 @@ import MarkdownItFrontMatter from 'markdown-it-front-matter' import yaml from './yaml' import * as directives from './directives' +import { markAsParsed } from '../comment' import marpitPlugin from '../marpit_plugin' -const isComment = token => +const isDirectiveComment = token => token.type === 'marpit_comment' && token.meta.marpitParsedDirectives -const markAsParsed = token => { - token.meta = token.meta || {} - token.meta.marpitCommentParsed = 'directive' -} - /** * Parse Marpit directives and store result to the slide token meta. * @@ -101,14 +97,17 @@ function parse(md, opts = {}) { for (const token of state.tokens) { if ( - isComment(token) && + isDirectiveComment(token) && applyDirectives(token.meta.marpitParsedDirectives) ) { - markAsParsed(token) + markAsParsed(token, 'directive') } else if (token.type === 'inline') { for (const t of token.children) { - if (isComment(t) && applyDirectives(t.meta.marpitParsedDirectives)) - markAsParsed(t) + if ( + isDirectiveComment(t) && + applyDirectives(t.meta.marpitParsedDirectives) + ) + markAsParsed(t, 'directive') } } } @@ -190,14 +189,17 @@ function parse(md, opts = {}) { cursor.spot = {} } else if ( - isComment(token) && + isDirectiveComment(token) && applyDirectives(token.meta.marpitParsedDirectives) ) { - markAsParsed(token) + markAsParsed(token, 'directive') } else if (token.type === 'inline') { for (const t of token.children) { - if (isComment(t) && applyDirectives(t.meta.marpitParsedDirectives)) - markAsParsed(t) + if ( + isDirectiveComment(t) && + applyDirectives(t.meta.marpitParsedDirectives) + ) + markAsParsed(t, 'directive') } } } diff --git a/test/markdown/collect.js b/test/markdown/collect.js index 78db0712..cc3a5530 100644 --- a/test/markdown/collect.js +++ b/test/markdown/collect.js @@ -2,7 +2,7 @@ import dedent from 'dedent' 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 comment, { markAsParsed } from '../../src/markdown/comment' import inlineSVG from '../../src/markdown/inline_svg' import parseDirectives from '../../src/markdown/directives/parse' import slide from '../../src/markdown/slide' @@ -109,46 +109,97 @@ describe('Marpit collect plugin', () => { expect(lastComments[3]).toStrictEqual(['inline comment']) }) - context( - 'when comment token is marked marpitCommentParsed meta in other plugin', - () => { - it('ignores collecting comment', () => { - const marpit = marpitStub() - - md(marpit) - .use(mdIt => { - mdIt.core.ruler.before( - 'marpit_slide', - 'marpit_test_inject', - state => { - const markParsed = token => { - token.meta = { - ...(token.meta || {}), - marpitCommentParsed: 'test', - } - } + context('when comment token is marked as parsed by #markAsParsed', () => { + it('ignores collecting comment', () => { + const marpit = marpitStub() - for (const token of state.tokens) { - if (token.content === 'This is comment') { - markParsed(token) - } else if (token.type === 'inline') { - for (const t of token.children) - if (t.content === 'inline comment') markParsed(t) - } + md(marpit) + .use(mdIt => { + mdIt.core.ruler.before( + 'marpit_slide', + 'marpit_test_inject', + state => { + for (const token of state.tokens) { + if (token.content === 'This is comment') { + markAsParsed(token, 'test') + } else if (token.type === 'inline') { + for (const t of token.children) + if (t.content === 'inline comment') markAsParsed(t, 'test') } } - ) - }) - .render(text) - - const { lastComments } = marpit - - expect(lastComments[0]).toHaveLength(1) - expect(lastComments[0]).not.toContain('This is comment') - expect(lastComments[3]).toHaveLength(0) - }) - } - ) + } + ) + }) + .render(text) + + const { lastComments } = marpit + + expect(lastComments[0]).toHaveLength(1) + expect(lastComments[0]).not.toContain('This is comment') + expect(lastComments[3]).toHaveLength(0) + }) + + it('ignores comments for well-known linters and formatters by default', () => { + const comment = markdown => { + const marpit = marpitStub() + md(marpit).render(markdown) + + return marpit.lastComments[0] + } + + expect(comment('')).toHaveLength(1) + + // Prettier + expect(comment('')).toHaveLength(0) + expect( + comment(dedent` + + + + `) + ).toHaveLength(1) + + // markdownlint + expect( + comment(dedent` + + deliberate space * in * emphasis + + `) + ).toHaveLength(0) + expect( + comment(dedent` + + + any violations you want + + `) + ).toHaveLength(0) + + // remark-lint (remark-message-control) + expect(comment('')).toHaveLength(0) + expect(comment('')).toHaveLength(0) + expect( + comment(dedent` + # Hello + + + + ## Hello + + + `) + ).toHaveLength(0) + expect( + comment(dedent` + + + * **foo** + * __bar__ + `) + ).toHaveLength(0) + }) + }) context('with inline SVG mode', () => { it('includes inline SVG tokens in collected result', () => { From 025e5fb0d579d2efe805938061dce1cca7a2cdaa Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Thu, 10 Oct 2019 09:58:21 +0900 Subject: [PATCH 2/3] Fix ESLint --- test/markdown/collect.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/markdown/collect.js b/test/markdown/collect.js index cc3a5530..fac07e45 100644 --- a/test/markdown/collect.js +++ b/test/markdown/collect.js @@ -140,19 +140,19 @@ describe('Marpit collect plugin', () => { }) it('ignores comments for well-known linters and formatters by default', () => { - const comment = markdown => { + const comments = markdown => { const marpit = marpitStub() md(marpit).render(markdown) return marpit.lastComments[0] } - expect(comment('')).toHaveLength(1) + expect(comments('')).toHaveLength(1) // Prettier - expect(comment('')).toHaveLength(0) + expect(comments('')).toHaveLength(0) expect( - comment(dedent` + comments(dedent` @@ -161,14 +161,14 @@ describe('Marpit collect plugin', () => { // markdownlint expect( - comment(dedent` + comments(dedent` deliberate space * in * emphasis `) ).toHaveLength(0) expect( - comment(dedent` + comments(dedent` any violations you want @@ -177,10 +177,10 @@ describe('Marpit collect plugin', () => { ).toHaveLength(0) // remark-lint (remark-message-control) - expect(comment('')).toHaveLength(0) - expect(comment('')).toHaveLength(0) + expect(comments('')).toHaveLength(0) + expect(comments('')).toHaveLength(0) expect( - comment(dedent` + comments(dedent` # Hello @@ -191,7 +191,7 @@ describe('Marpit collect plugin', () => { `) ).toHaveLength(0) expect( - comment(dedent` + comments(dedent` * **foo** From 7b69eb76e3d52ff1c19dacb71145a16691b45ed3 Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Thu, 10 Oct 2019 10:41:47 +0900 Subject: [PATCH 3/3] [ci skip] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d091f9e5..05a3b423 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Changed +- Ignore well-known magic comments in collected comments ([#191](https://github.com/marp-team/marpit/issues/191), [#199](https://github.com/marp-team/marpit/pull/199)) - Upgrade dependent packages to the latest version ([#196](https://github.com/marp-team/marpit/pull/196)) ## v1.4.0 - 2019-09-12