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

add notes export option #429

Merged
merged 5 commits into from
Feb 23, 2022
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ If you want to prevent looking up a configuration file, you can pass `--no-confi
| `jpegQuality` | number | `--jpeg-quality` | Setting JPEG image quality (`85` by default) |
| `keywords` | string \| string[] | `--keywords` | Define keywords for the slide deck (Accepts comma-separated string and array of string) |
| `lang` | string | | Define the language of converted HTML |
| `notes` | boolean | `--notes` | Convert slide deck into just the text notes |
| `ogImage` | string | `--og-image` | Define [Open Graph] image URL |
| `options` | object | | The base options for the constructor of engine |
| `output` | string | `--output` `-o` | Output file path (or directory when input-dir is passed) |
Expand Down
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ interface IMarpCLIArguments {
inputDir?: string
jpegQuality?: number
keywords?: string
notes?: boolean
ogImage?: string
output?: string | false
pdf?: boolean
Expand Down Expand Up @@ -169,6 +170,7 @@ export class MarpCLIConfig {
// CLI options
if (this.args.pdf || this.conf.pdf) return ConvertType.pdf
if (this.args.pptx || this.conf.pptx) return ConvertType.pptx
if (this.args.notes || this.conf.notes) return ConvertType.notes

const image =
this.args.images ||
Expand All @@ -188,6 +190,7 @@ export class MarpCLIConfig {
if (lowerOutput.endsWith('.pptx')) return ConvertType.pptx
if (lowerOutput.endsWith('.jpg') || lowerOutput.endsWith('.jpeg'))
return ConvertType.jpeg
if (lowerOutput.endsWith('.txt')) return ConvertType.notes

// Prefer PDF than HTML if enabled presenter notes for PDF
if (this.args.pdfNotes || this.conf.pdfNotes) return ConvertType.pdf
Expand Down
20 changes: 19 additions & 1 deletion src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export enum ConvertType {
png = 'png',
pptx = 'pptx',
jpeg = 'jpg',
notes = 'notes',
}

export const mimeTypes = {
Expand Down Expand Up @@ -191,7 +192,6 @@ export class Converter {

if (!opts.onlyScanning) {
const files: File[] = []

switch (this.options.type) {
case ConvertType.pdf:
template = await useTemplate(true)
Expand All @@ -217,6 +217,10 @@ export class Converter {
})
)
break
case ConvertType.notes:
template = await useTemplate(false)
files.push(await this.convertFileToNotes(template, file))
break
default:
template = await useTemplate()
files.push(this.convertFileToHTML(template, file))
Expand Down Expand Up @@ -253,6 +257,20 @@ export class Converter {
return ret
}

private convertFileToNotes(tpl: TemplateResult, file: File): File {
const ret = file.convert(this.options.output, { extension: 'txt' })
const comments = tpl.rendered.comments
if (comments.flat().length === 0) {
warn(`${file.relativePath()} contains no notes.`)
ret.buffer = Buffer.from('')
} else {
ret.buffer = Buffer.from(
comments.map((c) => c.join('\n\n')).join('\n\n---\n\n')
)
}
return ret
}

private async convertFileToPDF(
tpl: TemplateResult,
file: File
Expand Down
14 changes: 10 additions & 4 deletions src/marp-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,25 @@ export const marpCli = async (
type: 'boolean',
},
pdf: {
conflicts: ['image', 'images', 'pptx'],
conflicts: ['image', 'images', 'pptx', 'notes'],
describe: 'Convert slide deck into PDF',
group: OptionGroup.Converter,
type: 'boolean',
},
notes: {
conflicts: ['image', 'images', 'pptx', 'pdf'],
describe: 'Convert slide deck notes into TXT',
group: OptionGroup.Converter,
type: 'boolean',
},
pptx: {
conflicts: ['pdf', 'image', 'images'],
conflicts: ['pdf', 'image', 'images', 'notes'],
describe: 'Convert slide deck into PowerPoint document',
group: OptionGroup.Converter,
type: 'boolean',
},
image: {
conflicts: ['pdf', 'images', 'pptx'],
conflicts: ['pdf', 'images', 'pptx', 'notes'],
describe: 'Convert the first slide page into an image file',
group: OptionGroup.Converter,
choices: ['png', 'jpeg'],
Expand All @@ -141,7 +147,7 @@ export const marpCli = async (
type: 'string',
},
images: {
conflicts: ['pdf', 'image', 'pptx'],
conflicts: ['pdf', 'image', 'pptx', 'notes'],
describe: 'Convert slide deck into multiple image files',
group: OptionGroup.Converter,
choices: ['png', 'jpeg'],
Expand Down
40 changes: 40 additions & 0 deletions test/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,46 @@ transition:
expect(ret.newFile.type).toBe(FileType.StandardIO)
})

it('converts markdown file to text and save to specified path when output is defined', async () => {
const notesInstance = (opts: Partial<ConverterOption> = {}) =>
instance({ ...opts, type: ConvertType.notes })
const write = (<any>fs).__mockWriteFile()
const output = './specified.txt'
const ret = await (<any>notesInstance({ output })).convertFile(
new File(threePath),
{ type: 'notes' }
)
const notes: Buffer = write.mock.calls[0][1]

expect(write).toHaveBeenCalled()
expect(write.mock.calls[0][0]).toBe('./specified.txt')
expect(notes.toString()).toBe('presenter note')
expect(ret.newFile?.path).toBe('./specified.txt')
expect(ret.newFile?.buffer).toBe(notes)
})

it('converts markdown file to text and save to specified path when output is defined but no notes exist', async () => {
const warn = jest.spyOn(console, 'warn').mockImplementation()
const notesInstance = (opts: Partial<ConverterOption> = {}) =>
instance({ ...opts, type: ConvertType.notes })
const write = (<any>fs).__mockWriteFile()
const output = './specified.txt'
const ret = await (<any>notesInstance({ output })).convertFile(
new File(onePath),
{ type: 'notes' }
)
const notes: Buffer = write.mock.calls[0][1]

expect(warn).toHaveBeenCalledWith(
expect.stringContaining('contains no notes')
)
expect(write).toHaveBeenCalled()
expect(write.mock.calls[0][0]).toBe('./specified.txt')
expect(notes.toString()).toBe('')
expect(ret.newFile?.path).toBe('./specified.txt')
expect(ret.newFile?.buffer).toBe(notes)
})

describe('when convert type is PDF', () => {
const pdfInstance = (opts: Partial<ConverterOption> = {}) =>
instance({ ...opts, type: ConvertType.pdf })
Expand Down