Skip to content

Commit

Permalink
Merge pull request #104 from marp-team/dollar-prefixed-global-directives
Browse files Browse the repository at this point in the history
Add dollarPrefixForGlobalDirectives option
  • Loading branch information
yhatt authored Sep 12, 2019
2 parents 395fce5 + 8352fc7 commit 27a5775
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Added

- Add `minifyCSS` option ([#103](https://github.com/marp-team/marp-core/pull/103))
- Add `dollarPrefixForGlobalDirectives` option _(not for users)_ ([#104](https://github.com/marp-team/marp-core/pull/104))

### Changed

Expand Down
11 changes: 11 additions & 0 deletions src/dollar/dollar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import marpitPlugin from '@marp-team/marpit/lib/markdown/marpit_plugin'
import { Marp } from '../marp'

// TODO: Remove dollar prefix support after making be known deprecation to user of GUI tools like Marp for VS Code
export const markdown = marpitPlugin(({ marpit: marp }: { marpit: Marp }) => {
if (!marp.options.dollarPrefixForGlobalDirectives) return

marp.customDirectives.global.$theme = v => ({ theme: v })
marp.customDirectives.global.$style = v => ({ style: v })
marp.customDirectives.global.$headingDivider = v => ({ headingDivider: v })
})
10 changes: 8 additions & 2 deletions src/marp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import postcssMinifySelectors from 'postcss-minify-selectors'
import postcssNormalizeWhitespace from 'postcss-normalize-whitespace'
import { version } from 'katex/package.json'
import browser from './browser'
import * as dollarPlugin from './dollar/dollar'
import * as emojiPlugin from './emoji/emoji'
import * as fittingPlugin from './fitting/fitting'
import * as htmlPlugin from './html/html'
Expand All @@ -27,6 +28,9 @@ export interface MarpOptions extends MarpitOptions {
markdown?: object
math?: mathPlugin.MathOptions
minifyCSS?: boolean

/** @deprecated Dollar prefix for global directives is removed feature in Marpit framework, and Marp Core does not recommend too. Please use only for keeping compatibility in limited cases. */
dollarPrefixForGlobalDirectives?: boolean
}

const marpObservedSymbol = Symbol('marpObserved')
Expand All @@ -47,12 +51,13 @@ export class Marp extends Marpit {
}

constructor(opts: MarpOptions = {}) {
super(<MarpOptions>{
super({
inlineSVG: true,
looseYAML: true,
math: true,
minifyCSS: true,
html: Marp.html,
dollarPrefixForGlobalDirectives: false,
...opts,
markdown: [
'commonmark',
Expand All @@ -70,7 +75,7 @@ export class Marp extends Marpit {
unicode: 'twemoji',
...(opts.emoji || {}),
},
})
} as MarpOptions)

this.markdown.enable(['table', 'linkify'])

Expand All @@ -93,6 +98,7 @@ export class Marp extends Marpit {
.use(mathPlugin.markdown, flag => (this.renderedMath = flag))
.use(fittingPlugin.markdown)
.use(sizePlugin.markdown)
.use(dollarPlugin.markdown)
}

highlighter(code: string, lang: string): string {
Expand Down
46 changes: 46 additions & 0 deletions test/marp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,52 @@ describe('Marp', () => {
})
})

describe('dollarPrefixForGlobalDirectives option', () => {
it('cannot use dollar prefix for global directives by default', () => {
const instance = marp({ minifyCSS: false })

// $theme
expect(instance.render('<!-- $theme: gaia -->').css).not.toContain(
'@theme gaia'
)

// $style
expect(
instance.render('<!-- $style: "h6 { color: orange }" -->').css
).not.toContain('color: orange')

// $headingDivider
expect(
instance.render('<!-- $headingDivider: 1 -->\n\n# 1\n# 2', {
htmlAsArray: true,
}).html
).toHaveLength(1)
})

context('with true', () => {
it('can use dollar prefix as aliases for global directives', () => {
const instance = marp({
dollarPrefixForGlobalDirectives: true,
minifyCSS: false,
})

expect(instance.render('<!-- $theme: gaia -->').css).toContain(
'@theme gaia'
)

expect(
instance.render('<!-- $style: "h6 { color: orange }" -->').css
).toContain('color: orange')

expect(
instance.render('<!-- $headingDivider: 1 -->\n\n# 1\n# 2', {
htmlAsArray: true,
}).html
).toHaveLength(2)
})
})
})

describe('size global directive', () => {
it('defines size custom global directive', () =>
expect(marp().customDirectives.global.size).toBeTruthy())
Expand Down

0 comments on commit 27a5775

Please sign in to comment.