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

Handle export into non-file scheme #269

Merged
merged 7 commits into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
55 changes: 14 additions & 41 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@
"@types/lodash.debounce": "^4.0.6",
"@types/markdown-it": "^12.0.3",
"@types/node-fetch": "^2.5.12",
"@types/tmp": "^0.2.1",
"@types/vscode": "~1.56.0",
"@typescript-eslint/eslint-plugin": "^4.28.4",
"@typescript-eslint/parser": "^4.28.4",
Expand Down Expand Up @@ -297,6 +298,7 @@
},
"dependencies": {
"@marp-team/marp-cli": "^1.2.0",
"@marp-team/marp-core": "^2.1.0"
"@marp-team/marp-core": "^2.1.0",
"tmp": "^0.2.1"
yhatt marked this conversation as resolved.
Show resolved Hide resolved
}
}
1 change: 1 addition & 0 deletions src/__mocks__/vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export const FileSystem = {
type: FileType.File,
})),
readFile: jest.fn().mockResolvedValue(new TextEncoder().encode('readFile')),
isWritableFileSystem: jest.fn((scheme: string) => scheme === 'file'),
}

export enum FileType {
Expand Down
46 changes: 43 additions & 3 deletions src/commands/export.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import fs from 'fs'
import path from 'path'
import { promisify } from 'util'
import { tmpName, TmpNameOptions } from 'tmp'
import {
commands,
env,
Expand All @@ -19,6 +22,9 @@ import {
WorkspaceProxyServer,
} from '../workspace-proxy-server'

const tmpNamePromise = promisify<TmpNameOptions, string>(tmpName)
const unlink = promisify(fs.unlink)

export enum Types {
html = 'html',
pdf = 'pdf',
Expand Down Expand Up @@ -92,16 +98,50 @@ export const doExport = async (uri: Uri, document: TextDocument) => {
const input = await createWorkFile(document)

try {
let outputPath = uri.fsPath
const outputToLocalFS = uri.scheme === 'file'

// NOTE: It may return `undefined` if VS Code does not know about the
// filesystem. In this case, Marp may be able to write to the output path.
if (workspace.fs.isWritableFileSystem(uri.scheme) === false) {
throw new Error(`Could not write to ${uri.scheme} file system.`)
}

if (!outputToLocalFS) {
outputPath = await tmpNamePromise({
postfix: path.extname(uri.path),
})
}

// Run Marp CLI
const conf = await createConfigFile(document, {
allowLocalFiles: !proxyServer,
})

try {
await marpCli(['-c', conf.path, input.path, '-o', uri.fsPath], {
await marpCli(['-c', conf.path, input.path, '-o', outputPath], {
baseUrl,
})
env.openExternal(uri)

if (outputToLocalFS) {
env.openExternal(uri)
} else {
try {
await workspace.fs.copy(Uri.file(outputPath), uri, {
overwrite: true,
})
} finally {
try {
await unlink(outputPath)
} catch (e) {
console.warn(e)
}
}

window.showInformationMessage(
`Marp slide deck was successfully exported to ${uri.toString()}.`
)
}
} finally {
conf.cleanup()
}
Expand Down Expand Up @@ -143,7 +183,7 @@ export const saveDialog = async (document: TextDocument) => {
await window.withProgress(
{
location: ProgressLocation.Notification,
title: `Exporting Marp slide deck to ${saveURI.path}...`,
title: `Exporting Marp slide deck to ${saveURI.toString()}...`,
},
() => doExport(saveURI, document)
)
Expand Down