Skip to content

Commit

Permalink
Make previewable the in-memory huge content
Browse files Browse the repository at this point in the history
As same as #551, the preview window may fail to open data URI due to the
limitation of the display length.. If passed data URI to the preview
window, Marp CLI try to convert to Blob URL and open it instead.
  • Loading branch information
yhatt committed Sep 24, 2023
1 parent ff4a7bd commit 7f72e6b
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { nanoid } from 'nanoid'
import type { Page, Browser, Target } from 'puppeteer-core'
import TypedEmitter from 'typed-emitter'
import { ConvertType, mimeTypes } from './converter'
import { error } from './error'
import { CLIError, error } from './error'
import { File, FileType } from './file'
import {
generatePuppeteerDataDirPath,
Expand Down Expand Up @@ -93,7 +93,24 @@ export class Preview extends (EventEmitter as new () => TypedEmitter<Preview.Eve
/* c8 ignore stop */
},
load: async (uri: string) => {
await page.goto(uri, { timeout: 0, waitUntil: 'domcontentloaded' })
if (uri.startsWith('data:')) {
// A data URI with a huge size may fail opening with a browser due to the limitation of URL length.
// If received a data URI, try to open it with a converted Blob URL.
await Promise.all([
page.waitForNavigation({
timeout: 5000,
waitUntil: 'domcontentloaded',
}),
page.evaluate(async (uri) => {
const res = await fetch(uri, { cache: 'no-cache' })
const blob = await res.blob()
location.href = URL.createObjectURL(blob)
}, uri),
])
} else {
await page.goto(uri, { timeout: 0, waitUntil: 'domcontentloaded' })
}

await page
.target()
.createCDPSession()
Expand Down Expand Up @@ -126,13 +143,12 @@ export class Preview extends (EventEmitter as new () => TypedEmitter<Preview.Eve
pptr.on('targetcreated', idMatcher)

// Open new window with specific identifier
;(async () => {
for (const page of await pptr.pages()) {
await page.evaluate(
`window.open('about:blank?__marp_cli_id=${id}', '', 'width=${this.options.width},height=${this.options.height}')`
)
break
}
void (async () => {
const [page] = await pptr.pages()

await page.evaluate(
`window.open('about:blank?__marp_cli_id=${id}', '', 'width=${this.options.width},height=${this.options.height}')`
)
})()
})
)
Expand Down

0 comments on commit 7f72e6b

Please sign in to comment.