From f780ec877ac15f3b11af1d4bc2336e0f4ed600f4 Mon Sep 17 00:00:00 2001 From: Charles Zhang Date: Sat, 8 Feb 2020 17:17:32 -0800 Subject: [PATCH 01/10] Add an additional config option "customConfig" Defaults to false. --- package.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/package.json b/package.json index 6cc1094..323d138 100644 --- a/package.json +++ b/package.json @@ -126,6 +126,11 @@ "type": "boolean", "default": true, "description": "Shows editor toolbar button to Markdown document, for accessing quick pick of Marp commands." + }, + "markdown.marp.customConfig": { + "type": "boolean", + "default": false, + "description": "Use Marp CLI config files in workspace folders, if they exist." } } }, From 5401ecb2e8db981ff307568f9ffe3659281999aa Mon Sep 17 00:00:00 2001 From: Charles Zhang Date: Sat, 8 Feb 2020 17:20:33 -0800 Subject: [PATCH 02/10] Skeleton function load an exisiting config Just copying the `createConfigFile()` function for now, because we want it to return the same output. --- src/marp-cli.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/marp-cli.ts b/src/marp-cli.ts index cd9c3d8..422ffa3 100644 --- a/src/marp-cli.ts +++ b/src/marp-cli.ts @@ -77,6 +77,26 @@ export async function createConfigFile( } } +export async function loadConfigFile( + target: TextDocument +): Promise { + const tmpFileName = `.marp-vscode-cli-conf-${nanoid()}.json` + const tmpPath = path.join(tmpdir(), tmpFileName) + const cliOpts = await marpCoreOptionForCLI(target) + + await promiseWriteFile(tmpPath, JSON.stringify(cliOpts)) + + return { + path: tmpPath, + cleanup: async () => { + await Promise.all([ + promiseUnlink(tmpPath), + ...cliOpts.vscode.themeFiles.map((w: WorkFile) => w.cleanup()), + ]) + }, + } +} + export default async function runMarpCli(...opts: string[]): Promise { const argv = ['--no-stdin', ...opts] console.info(`Execute Marp CLI: ${argv.join(' ')}`) From c4161074ecbf2de5b6eaf58fa630aef4749d4b10 Mon Sep 17 00:00:00 2001 From: Charles Zhang Date: Sat, 8 Feb 2020 17:29:16 -0800 Subject: [PATCH 03/10] Add custom config to `doExport()` Add a try-except-finally block. In the try block, check if the `customConfig` option is specified and load the default config file (still working on this). Else, call `createConfigFile()`) If there is an error (e.g., no file, invalid config file), revert to creating the Config File. --- src/commands/export.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/commands/export.ts b/src/commands/export.ts index 591b47a..722f914 100644 --- a/src/commands/export.ts +++ b/src/commands/export.ts @@ -37,8 +37,22 @@ export const doExport = async (uri: Uri, document: TextDocument) => { const input = await createWorkFile(document) try { - const conf = await createConfigFile(document) - + /*Check options to determine whether or not to use a custom config*/ + try { + if (const marpConfiguration().get('customConfig')){ + const conf = await loadConfigFile(XXX) + } else { + const conf = await createConfigFile(document) + } + } catch (e) { + /*${workspace().path}*/ + console.error( + `Failed to load custom config file from "XXX". (${e.message})` + ) + } finally { + const conf = await createConfigFile(document) + } + try { await marpCli('-c', conf.path, input.path, '-o', uri.fsPath) env.openExternal(uri) From 202751141392952d55072320468c64361d9f36e1 Mon Sep 17 00:00:00 2001 From: Charles Zhang Date: Sat, 8 Feb 2020 17:44:12 -0800 Subject: [PATCH 04/10] Updated logic --- src/commands/export.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/commands/export.ts b/src/commands/export.ts index 722f914..572517d 100644 --- a/src/commands/export.ts +++ b/src/commands/export.ts @@ -40,14 +40,14 @@ export const doExport = async (uri: Uri, document: TextDocument) => { /*Check options to determine whether or not to use a custom config*/ try { if (const marpConfiguration().get('customConfig')){ - const conf = await loadConfigFile(XXX) + const conf = await loadConfigFile(document) } else { const conf = await createConfigFile(document) } } catch (e) { /*${workspace().path}*/ console.error( - `Failed to load custom config file from "XXX". (${e.message})` + `Error loading custom configurations; falling back on default config. (${e.message})` ) } finally { const conf = await createConfigFile(document) From a10dde2be64ad80256ae8f79c619961ace64625b Mon Sep 17 00:00:00 2001 From: Charles Zhang Date: Sat, 8 Feb 2020 18:04:00 -0800 Subject: [PATCH 05/10] Proposed implementation Added comments to propose an implementation, but my typescript is inadequate. --- src/marp-cli.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/marp-cli.ts b/src/marp-cli.ts index 422ffa3..02192f0 100644 --- a/src/marp-cli.ts +++ b/src/marp-cli.ts @@ -84,6 +84,20 @@ export async function loadConfigFile( const tmpPath = path.join(tmpdir(), tmpFileName) const cliOpts = await marpCoreOptionForCLI(target) + /* Find config file (needs a bit more logic to check for the other + * types of config files) + */ + const documentWorkspace = workspace.getWorkspaceFolder(doc.uri) + const configFileName = path.join(documentWorkspace.uri.fsPath, '.marprc') + + /* Proposed implementation: + * + * 1. Load config file as a dictionary (JSON object?), e.g. `tmpConfig` + * 2. Iterate over keys in `tmpConfig` and insert (or replace) the key:value in `cliOpts` + * + * Advantage here is that the user only has to replace the options that they care to modify. + * This is useful because specifying where you should look for themes is potentially painful. + */ await promiseWriteFile(tmpPath, JSON.stringify(cliOpts)) return { From c3b6507e091d1698ae23e9368f4652602a13b2f9 Mon Sep 17 00:00:00 2001 From: Charles Zhang Date: Sat, 8 Feb 2020 18:20:26 -0800 Subject: [PATCH 06/10] Additional detail in help message. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 323d138..72c0e51 100644 --- a/package.json +++ b/package.json @@ -130,7 +130,7 @@ "markdown.marp.customConfig": { "type": "boolean", "default": false, - "description": "Use Marp CLI config files in workspace folders, if they exist." + "description": "Enable using a custom Marp CLI config file for exporting (does not affect preview in VS Code). Custom config file must be placed in the root of the workspace." } } }, From 395aa2a316e80b718a03244b15704eec4920469c Mon Sep 17 00:00:00 2001 From: Charles Zhang Date: Sat, 8 Feb 2020 23:32:27 -0800 Subject: [PATCH 07/10] Delete load_config_file() function --- src/marp-cli.ts | 34 ---------------------------------- 1 file changed, 34 deletions(-) diff --git a/src/marp-cli.ts b/src/marp-cli.ts index 02192f0..cd9c3d8 100644 --- a/src/marp-cli.ts +++ b/src/marp-cli.ts @@ -77,40 +77,6 @@ export async function createConfigFile( } } -export async function loadConfigFile( - target: TextDocument -): Promise { - const tmpFileName = `.marp-vscode-cli-conf-${nanoid()}.json` - const tmpPath = path.join(tmpdir(), tmpFileName) - const cliOpts = await marpCoreOptionForCLI(target) - - /* Find config file (needs a bit more logic to check for the other - * types of config files) - */ - const documentWorkspace = workspace.getWorkspaceFolder(doc.uri) - const configFileName = path.join(documentWorkspace.uri.fsPath, '.marprc') - - /* Proposed implementation: - * - * 1. Load config file as a dictionary (JSON object?), e.g. `tmpConfig` - * 2. Iterate over keys in `tmpConfig` and insert (or replace) the key:value in `cliOpts` - * - * Advantage here is that the user only has to replace the options that they care to modify. - * This is useful because specifying where you should look for themes is potentially painful. - */ - await promiseWriteFile(tmpPath, JSON.stringify(cliOpts)) - - return { - path: tmpPath, - cleanup: async () => { - await Promise.all([ - promiseUnlink(tmpPath), - ...cliOpts.vscode.themeFiles.map((w: WorkFile) => w.cleanup()), - ]) - }, - } -} - export default async function runMarpCli(...opts: string[]): Promise { const argv = ['--no-stdin', ...opts] console.info(`Execute Marp CLI: ${argv.join(' ')}`) From e7389de7e54b408d7e2f5800461767cc22530199 Mon Sep 17 00:00:00 2001 From: Charles Zhang Date: Sun, 9 Feb 2020 00:42:55 -0800 Subject: [PATCH 08/10] Alternative way to find config file Allow Marp CLI to find file by changing the process directory to the directory of the markdown file. --- src/commands/export.ts | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/commands/export.ts b/src/commands/export.ts index 572517d..8ed5374 100644 --- a/src/commands/export.ts +++ b/src/commands/export.ts @@ -37,25 +37,23 @@ export const doExport = async (uri: Uri, document: TextDocument) => { const input = await createWorkFile(document) try { - /*Check options to determine whether or not to use a custom config*/ + + const conf = await createConfigFile(document) + try { - if (const marpConfiguration().get('customConfig')){ - const conf = await loadConfigFile(document) + if (!const marpConfiguration().get('customConfig')){ + await marpCli('-c', conf.path, input.path, '-o', uri.fsPath) } else { - const conf = await createConfigFile(document) + /* If we are using the custom config, set the working directory + * allow Marp CLI to search the document's workspace for a + * config file. + */ + const currentDir = path.cwd() + process.chdir(path.dirname(document.uri.fsPath)) + await marpCli(input.path, '-o', uri.fsPath) + process.chdir(currentDir) } - } catch (e) { - /*${workspace().path}*/ - console.error( - `Error loading custom configurations; falling back on default config. (${e.message})` - ) - } finally { - const conf = await createConfigFile(document) - } - - try { - await marpCli('-c', conf.path, input.path, '-o', uri.fsPath) - env.openExternal(uri) + env.openExternal(uri) } finally { conf.cleanup() } From 29708f6e5a3c4e2c599707cc5866b7d5f0e5f215 Mon Sep 17 00:00:00 2001 From: Charles Zhang Date: Sun, 9 Feb 2020 00:44:45 -0800 Subject: [PATCH 09/10] Clean up whitespace --- src/commands/export.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/commands/export.ts b/src/commands/export.ts index 8ed5374..8fb44e0 100644 --- a/src/commands/export.ts +++ b/src/commands/export.ts @@ -37,9 +37,8 @@ export const doExport = async (uri: Uri, document: TextDocument) => { const input = await createWorkFile(document) try { - const conf = await createConfigFile(document) - + try { if (!const marpConfiguration().get('customConfig')){ await marpCli('-c', conf.path, input.path, '-o', uri.fsPath) From 711cf62a900ca3dae3e77b800edfe245cbd2e2d1 Mon Sep 17 00:00:00 2001 From: Charles Zhang Date: Sun, 9 Feb 2020 00:56:06 -0800 Subject: [PATCH 10/10] Typos --- src/commands/export.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/export.ts b/src/commands/export.ts index 8fb44e0..075230d 100644 --- a/src/commands/export.ts +++ b/src/commands/export.ts @@ -40,7 +40,7 @@ export const doExport = async (uri: Uri, document: TextDocument) => { const conf = await createConfigFile(document) try { - if (!const marpConfiguration().get('customConfig')){ + if (!marpConfiguration().get('customConfig')) { await marpCli('-c', conf.path, input.path, '-o', uri.fsPath) } else { /* If we are using the custom config, set the working directory