Skip to content

Commit

Permalink
Merge pull request #7 from marp-team/html-option
Browse files Browse the repository at this point in the history
Add html option
  • Loading branch information
yhatt committed Aug 30, 2018
2 parents bd6d29e + 0ca1578 commit 0e31eaa
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 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]

### Added

- Add `--html` option ([#7](https://github.com/marp-team/marp-cli/pull/7))

## v0.0.5 - 2018-08-29

### Added
Expand Down
15 changes: 11 additions & 4 deletions src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export enum ConvertType {

export interface ConverterOption {
engine: typeof Marpit
html?: boolean
lang: string
options: MarpitOptions
output?: string
Expand Down Expand Up @@ -96,14 +97,20 @@ export class Converter {
}

private generateEngine(mergeOptions: MarpitOptions) {
const engine = new this.options.engine({
...this.options.options,
...mergeOptions,
})
const { html, options } = this.options
const opts: any = { ...options, ...mergeOptions }

// for marp-core
if (html !== undefined) opts.html = !!html

const engine = new this.options.engine(opts)

if (typeof engine.render !== 'function')
error('Specified engine has not implemented render() method.')

// for Marpit engine
engine.markdown.set({ html: !!html })

return engine
}

Expand Down
9 changes: 8 additions & 1 deletion src/marp-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { name, version } from '../package.json'
enum OptionGroup {
Basic = 'Basic Options:',
Converter = 'Converter Options:',
Marp = 'Marp / Marpit Options:',
}

const usage = `
Expand Down Expand Up @@ -60,9 +61,14 @@ export default async function(argv: string[] = []): Promise<number> {
choices: Object.keys(templates),
type: 'string',
},
html: {
describe: 'Enable or disable HTML tag',
group: OptionGroup.Marp,
type: 'boolean',
},
theme: {
describe: 'Override theme',
group: OptionGroup.Converter,
group: OptionGroup.Marp,
type: 'string',
},
})
Expand All @@ -77,6 +83,7 @@ export default async function(argv: string[] = []): Promise<number> {
// Initialize converter
const converter = new Converter({
engine: Marp,
html: args.html,
lang: (await osLocale()).replace(/[_@]/g, '-'),
options: {},
output: args.output,
Expand Down
19 changes: 14 additions & 5 deletions test/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ describe('Converter', () => {
})

describe('#convert', () => {
const md = '# <i>Hello!</i>'

it('returns the result of template', async () => {
const options = { html: true }
const readyScript = '<b>ready</b>'
const md = '# <i>Hello!</i>'
const result = await instance({ options, readyScript }).convert(md)

expect(result.result).toMatch(/^<!DOCTYPE html>[\s\S]+<\/html>$/)
Expand All @@ -64,24 +65,32 @@ describe('Converter', () => {
})

it('throws CLIError when selected engine is not implemented render() method', () => {
const subject = instance({ engine: function _() {} }).convert('')
const subject = instance({ engine: function _() {} }).convert(md)
expect(subject).rejects.toBeInstanceOf(CLIError)
})

it('throws CLIError when selected template is not found', () => {
const subject = instance({ template: 'not-found' }).convert('')
const subject = instance({ template: 'not-found' }).convert(md)
expect(subject).rejects.toBeInstanceOf(CLIError)
})

it('settings lang attribute of <html> by lang option', async () => {
const { result } = await instance({ lang: 'zh' }).convert('')
const { result } = await instance({ lang: 'zh' }).convert(md)
expect(result).toContain('<html lang="zh">')
})

it("overrides theme by converter's theme option", async () => {
const { rendered } = await instance({ theme: 'gaia' }).convert('')
const { rendered } = await instance({ theme: 'gaia' }).convert(md)
expect(rendered.css).toContain('@theme gaia')
})

it("overrides html option by converter's html option", async () => {
const enabled = (await instance({ html: true }).convert(md)).rendered
expect(enabled.html).toContain('<i>Hello!</i>')

const disabled = (await instance({ html: false }).convert(md)).rendered
expect(disabled.html).toContain('&lt;i&gt;Hello!&lt;/i&gt;')
})
})

describe('#convertFile', () => {
Expand Down

0 comments on commit 0e31eaa

Please sign in to comment.