Skip to content

Commit

Permalink
Mark bespoke template as not-printable due to incorrect Chrome rendering
Browse files Browse the repository at this point in the history
Original report: marp-team/marp-vscode#175

If tried using not-printable template in Puppeteer-required conversion,
Marp CLI automatically fallbacks into bare template.
  • Loading branch information
yhatt committed Oct 17, 2020
1 parent d945125 commit 19788c5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
33 changes: 28 additions & 5 deletions src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import metaPlugin from './engine/meta-plugin'
import { error } from './error'
import { File, FileType } from './file'
import templates, {
bare,
Template,
TemplateMeta,
TemplateOption,
Expand Down Expand Up @@ -94,7 +95,13 @@ export class Converter {
return template
}

async convert(markdown: string, file?: File): Promise<TemplateResult> {
async convert(
markdown: string,
file?: File,
{
fallbackToPrintableTemplate = false,
}: { fallbackToPrintableTemplate?: boolean } = {}
): Promise<TemplateResult> {
const { lang, globalDirectives, type } = this.options
const isFile = (f: File | undefined): f is File =>
!!f && f.type === FileType.File
Expand All @@ -114,7 +121,10 @@ export class Converter {
}
}

return await this.template({
let template = this.template
if (fallbackToPrintableTemplate && !template.printable) template = bare

return await template({
...(this.options.templateOption || {}),
lang,
base:
Expand Down Expand Up @@ -142,24 +152,32 @@ export class Converter {
file: File,
opts: ConvertFileOption = {}
): Promise<ConvertResult> {
const template = await (async (): Promise<TemplateResult> => {
let template: TemplateResult

const useTemplate = async (
fallbackToPrintableTemplate?: boolean
): Promise<TemplateResult> => {
try {
silence(!!opts.onlyScanning)
return await this.convert((await file.load()).toString(), file)
return await this.convert((await file.load()).toString(), file, {
fallbackToPrintableTemplate,
})
} finally {
silence(false)
}
})()
}

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

switch (this.options.type) {
case ConvertType.pdf:
template = await useTemplate(true)
files.push(await this.convertFileToPDF(template, file))
break
case ConvertType.png:
case ConvertType.jpeg:
template = await useTemplate(true)
files.push(
...(await this.convertFileToImage(template, file, {
pages: this.options.pages,
Expand All @@ -169,9 +187,11 @@ export class Converter {
)
break
case ConvertType.pptx:
template = await useTemplate(true)
files.push(await this.convertFileToPPTX(template, file))
break
default:
template = await useTemplate()
files.push(this.convertFileToHTML(template, file))
}

Expand All @@ -182,6 +202,9 @@ export class Converter {

// #convertFile must return a single file to serve in server
return { file, template, newFile: files[0] }
} else {
// Try conversion with specific template to scan using resources
template = await useTemplate()
}

return { file, template }
Expand Down
11 changes: 9 additions & 2 deletions src/templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ export interface TemplateResult {
result: string
}

export type Template<T = TemplateOption> = (
export type Template<T = TemplateOption> = ((
locals: TemplateCoreOption & T
) => Promise<TemplateResult>
) => Promise<TemplateResult>) & {
printable?: boolean
}

export const bare: Template<TemplateBareOption> = async (opts) => {
const rendered = opts.renderer({
Expand All @@ -70,6 +72,8 @@ export const bare: Template<TemplateBareOption> = async (opts) => {
}
}

Object.defineProperty(bare, 'printable', { value: true })

export const bespoke: Template<TemplateBespokeOption> = async (opts) => {
const rendered = opts.renderer({
container: new Element('div', { id: 'p' }),
Expand All @@ -93,6 +97,9 @@ export const bespoke: Template<TemplateBespokeOption> = async (opts) => {
}
}

// Sometimes bespoke template cannot render background images since Chrome 85
Object.defineProperty(bespoke, 'printable', { value: false })

async function libJs(fn: string) {
return (await readFile(path.resolve(__dirname, fn))).toString()
}
Expand Down

0 comments on commit 19788c5

Please sign in to comment.