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 missing local file error message #200

Merged
merged 2 commits into from
Jan 27, 2020
Merged
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
35 changes: 28 additions & 7 deletions src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,17 +364,29 @@ export class Converter {
try {
const browser = await Converter.runBrowser()
const page = await browser.newPage()
const tracker = this.trackFailedLocalFileAccess(page)
const {
missingFileSet: missingTracker,
failedFileSet: failedTracker,
} = this.trackFailedLocalFileAccess(page)

try {
return await process(page, uri)
} finally {
if (tracker.size > 0) {
if (missingTracker.size > 0) {
warn(
`${missingTracker.size > 1 ? 'Some of t' : 'T'}he local file${
missingTracker.size > 1 ? 's are' : ' is'
} missing and will be ignored. Make sure the file path${
missingTracker.size > 1 ? 's are' : ' is'
} correct.`
)
}
if (failedTracker.size > 0) {
warn(
`Marp CLI has detected accessing to local file${
tracker.size > 1 ? 's' : ''
failedTracker.size > 1 ? 's' : ''
}. ${
tracker.size > 1 ? 'They are' : 'That is'
failedTracker.size > 1 ? 'They are' : 'That is'
} blocked by security reason. Instead we recommend using assets uploaded to online. (Or you can use ${chalk.yellow(
'--allow-local-files'
)} option if you are understood of security risk)`
Expand All @@ -387,17 +399,26 @@ export class Converter {
}
}

private trackFailedLocalFileAccess(page: puppeteer.Page): Set<string> {
private trackFailedLocalFileAccess(
page: puppeteer.Page
): { missingFileSet: Set<string>; failedFileSet: Set<string> } {
const missingFileSet = new Set<string>()
const failedFileSet = new Set<string>()

page.on('requestfailed', (req: puppeteer.Request) => {
try {
const url = new URL(req.url())
if (url.protocol === 'file:') failedFileSet.add(url.href)
if (url.protocol === 'file:') {
if (req.failure()?.errorText === 'net::ERR_FILE_NOT_FOUND') {
missingFileSet.add(url.href)
} else {
failedFileSet.add(url.href)
}
}
} catch (e) {}
})

return failedFileSet
return { missingFileSet, failedFileSet }
}

static async closeBrowser() {
Expand Down