Skip to content

Commit

Permalink
Merge pull request #62 from marp-team/regression-of-size-directive
Browse files Browse the repository at this point in the history
Fix regression of not working size global directive
  • Loading branch information
yhatt authored Aug 1, 2019
2 parents 44e7c37 + ac8d9cc commit 8f951e4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 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

- Fix regression of not working `size` global directive ([#61](https://github.com/marp-team/marp-vscode/issues/61), [#62](https://github.com/marp-team/marp-vscode/pull/62))

## v0.8.0 - 2019-07-29

### Added
Expand Down
20 changes: 20 additions & 0 deletions src/extension.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs'
import path from 'path'
import { Marp } from '@marp-team/marp-core'
import axios from 'axios'
import cheerio from 'cheerio'
import dedent from 'dedent'
Expand Down Expand Up @@ -87,6 +88,25 @@ describe('#extendMarkdownIt', () => {
})

describe('Plugins', () => {
describe('Custom theme', () => {
const marpCore = (markdown: string = ''): Marp => {
const { extendMarkdownIt, marpVscode } = extension()
const md = new markdownIt()

extendMarkdownIt(md).render(marpMd(markdown))
return md[marpVscode]
}

it('prevents override built-in theme', () => {
expect(() => marpCore().themeSet.add('/* @theme default */')).toThrow()
expect(() => marpCore().themeSet.add('/* @theme gaia */')).toThrow()
expect(() => marpCore().themeSet.add('/* @theme uncover */')).toThrow()
})

it('works size global directive correctly', () =>
expect(() => marpCore('<!-- size: 4:3 -->')).not.toThrow())
})

describe('Line number', () => {
const markdown = dedent`
---
Expand Down
3 changes: 2 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import themes from './themes'

const frontMatterRegex = /^-{3,}\s*([^]*?)^\s*-{3}/m
const marpDirectiveRegex = /^marp\s*:\s*true\s*$/m
const marpVscode = Symbol('marp-vscode')
const shouldRefreshConfs = [
'markdown.marp.breaks',
'markdown.marp.enableHtml',
Expand All @@ -24,6 +23,8 @@ const detectMarpFromFrontMatter = (markdown: string): boolean => {
return !!(m && m.index === 0 && marpDirectiveRegex.exec(m[0].trim()))
}

export const marpVscode = Symbol('marp-vscode')

export function extendMarkdownIt(md: any) {
const { parse, renderer } = md
const { render } = renderer
Expand Down
19 changes: 17 additions & 2 deletions src/plugins/custom-theme.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
export class CustomThemeError extends Error {}

export default function marpVSCodeCustomTheme({ marpit }) {
export default function marpVSCodeCustomTheme(instance) {
const { marpit, parse } = instance
const registeredThemes = [...marpit.themeSet.themes()].map(t => t.name)
const { addTheme } = marpit.themeSet

// `size` global directive support in Marp Core may override an instance of
// default themes. Even if default themes were overridden, we should not throw
// error while parsing Markdown by markdown-it.
let whileParsing = false

marpit.themeSet.addTheme = theme => {
if (registeredThemes.includes(theme.name)) {
if (!whileParsing && registeredThemes.includes(theme.name)) {
throw new CustomThemeError(
`Custom theme cannot override "${theme.name}" built-in theme.`
)
} else {
return addTheme.call(marpit.themeSet, theme)
}
}

instance.parse = (...args) => {
try {
whileParsing = true
return parse.apply(instance, args)
} finally {
whileParsing = false
}
}
}

0 comments on commit 8f951e4

Please sign in to comment.