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

Recognize theme CSS in input directory #28

Merged
merged 5 commits into from
Oct 5, 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 @@ -7,6 +7,10 @@
- Add server mode provided by `--server` (`-s`) option ([#27](https://github.com/marp-team/marp-cli/pull/27))
- Add fonts for internationalization to Docker image ([#26](https://github.com/marp-team/marp-cli/pull/26))

### Changed

- Recognize theme CSS in input directory specified by `--input-dir` (`-I`) option ([#28](https://github.com/marp-team/marp-cli/pull/28))

## v0.0.10 - 2018-09-20

### Added
Expand Down
8 changes: 6 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class MarpCLIConfig {
return resolveEngine(['@marp-team/marp-core', Marp])
})()

const inputDir = await this.inputDir()
const output =
this.args.output ||
(this.conf.output
Expand All @@ -87,7 +88,10 @@ export class MarpCLIConfig {
).map(f => path.resolve(path.dirname(this.confPath!), f))
: [])

const themeSet = await ThemeSet.initialize(themeSetPathes, initialThemes)
const themeSet = await ThemeSet.initialize(
(inputDir ? [inputDir] : []).concat(themeSetPathes),
initialThemes
)

if (
themeSet.themes.size <= initialThemes.length &&
Expand All @@ -96,6 +100,7 @@ export class MarpCLIConfig {
warn('Not found additional theme CSS files.')

return {
inputDir,
output,
server,
themeSet,
Expand All @@ -106,7 +111,6 @@ export class MarpCLIConfig {
) || false,
engine: engine.klass,
html: this.pickDefined(this.args.html, this.conf.html),
inputDir: await this.inputDir(),
lang: this.conf.lang || (await osLocale()).replace(/[_@]/g, '-'),
options: this.conf.options || {},
readyScript: engine.browserScript
Expand Down
2 changes: 1 addition & 1 deletion src/marp-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default async function(argv: string[] = []): Promise<number> {
},
'input-dir': {
alias: 'I',
describe: 'The base input directory to find markdown',
describe: 'The base directory to find markdown and theme CSS',
group: OptionGroup.Basic,
type: 'string',
},
Expand Down
10 changes: 8 additions & 2 deletions src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Marpit } from '@marp-team/marpit'
import fs from 'fs'
import globby from 'globby'
import path from 'path'
import { warn } from './cli'

const themeExtensions = ['*.css']

Expand Down Expand Up @@ -95,8 +96,13 @@ export class ThemeSet {

registerTo(engine: Marpit) {
for (const theme of this.themes.values()) {
const engineTheme = engine.themeSet.add(theme.css)
theme.name = engineTheme.name
try {
const engineTheme = engine.themeSet.add(theme.css)
theme.name = engineTheme.name
} catch (e) {
const fn = path.relative(process.cwd(), theme.filename)
warn(`Cannot register theme CSS: ${fn} (${e.message})`)
}
}
}

Expand Down
Empty file added test/_files/themes/empty
Empty file.
10 changes: 9 additions & 1 deletion test/marp-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ describe('Marp CLI', () => {
writeFile.mock.calls.forEach(([fn]) => expect(fn).toMatch(/\.html$/))
})

it('allows using theme css in specified dir', async () => {
jest.spyOn(cli, 'info').mockImplementation()
expect(await marpCli(['--input-dir', files, '--theme', 'a'])).toBe(0)

for (const [, buffer] of writeFile.mock.calls)
expect(buffer.toString()).toContain('/* @theme a */')
})

it('prints error and return error code with invalid option(s)', async () => {
const err = jest.spyOn(console, 'error').mockImplementation()
jest.spyOn(console, 'warn').mockImplementation()
Expand Down Expand Up @@ -163,7 +171,7 @@ describe('Marp CLI', () => {
)
expect(serverStart).toBeCalledTimes(1)
expect(Watcher.watch).toHaveBeenCalledWith(
[files],
expect.arrayContaining([files]),
expect.objectContaining({
mode: Watcher.WatchMode.Notify,
})
Expand Down
17 changes: 17 additions & 0 deletions test/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,22 @@ describe('ThemeSet', () => {
expect(names).toContain('b')
expect(names).toContain('c')
})

context('when registered theme is not compatible to Marpit engine', () => {
const emptyCSS = path.resolve(__dirname, '_files/themes/empty')

it('outputs warning and ignores registration', async () => {
const warn = jest.spyOn(console, 'warn').mockImplementation()
const themeSet = await ThemeSet.initialize([emptyCSS])
const marpit = new Marpit()

themeSet.registerTo(marpit)

expect(marpit.themeSet.size).toBe(0)
expect(warn).toBeCalledWith(
expect.stringContaining('Cannot register theme CSS')
)
})
})
})
})