Skip to content

Commit

Permalink
Feature predefined and environment variables in config (#655)
Browse files Browse the repository at this point in the history
* Support workspaceFolder variable in configPath

Adds support for predefined VS Code variable "${workspaceFolder}" to setting "configPath" in MPE.

This allows for configuring MPE per-workspace if needed.

fixes #625

* Support environment variables in configPath

setting "configPath" does not support environment variables.

Extended to support windows style %ENV_VAR% as well as bash style ${ENV_VAR} and $ENV_VAR environment variables

fixes #625

* Refactoring to allow any string to resolve variables

Implementation only applies to configPath and polutes the code.

Refactors that code and extracts it into a PathResolver class.

fixes #625
  • Loading branch information
DoItWithASmile authored Dec 31, 2022
1 parent eb20c2c commit 2f71342
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
RevealJsTheme,
} from "@shd101wyy/mume/out/src/markdown-engine-config";
import * as vscode from "vscode";
import { PathResolver } from "./utils/path-resolver";

export class MarkdownPreviewEnhancedConfig implements MarkdownEngineConfig {
public static getCurrentConfig() {
Expand Down Expand Up @@ -68,6 +69,8 @@ export class MarkdownPreviewEnhancedConfig implements MarkdownEngineConfig {
);

this.configPath = (config.get<string>("configPath") || "").trim();
this.configPath = PathResolver.resolvePath(this.configPath);

this.usePandocParser = config.get<boolean>("usePandocParser");
this.breakOnSingleNewLine = config.get<boolean>("breakOnSingleNewLine");
this.enableTypographer = config.get<boolean>("enableTypographer");
Expand Down
85 changes: 85 additions & 0 deletions src/utils/path-resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import * as vscode from "vscode";

export class PathResolver {
/**
* resolve placeholder in string
*/
public static resolvePath(path: string) {
let result = path;

// guard: empty
if (result.trim() == "") {
return path;
}

// resolve VS Code predefined variables
result = this.resolve_vscode_predefined_variables(result);

// resolve environment variables
result = this.resolve_environment_variable(result);

// done
return result;
}

/**
* resolve vscode predefined variables in string
*/
public static resolve_vscode_predefined_variables(path: string) {
let result = path;

// ... workspaceFolder
result = this.resolve_vscode_predefined_variable_workspaceFolder(result);

// done
return result;
}

/**
* resolve vscode predefined variables in string
*/
public static resolve_vscode_predefined_variable_workspaceFolder(
path: string,
) {
let result = path;

// guard: nothing to do
if (!path.includes("${workspaceFolder}")) return path;

// guard: error
if (vscode.workspace.workspaceFolders == undefined) {
vscode.window.showErrorMessage(
"Working folder not found, open a folder an try again",
);

return path;
}

// determine workspace folder
// let workspaceFolder = vscode.workspace.workspaceFolders[0].uri.path ;
let workspaceFolder = vscode.workspace.workspaceFolders[0].uri.fsPath;

// replace in configPath
result = path.replace("${workspaceFolder}", workspaceFolder);

// log
//vscode.window.showInformationMessage(
// `Resolved '${this}' to '${result}'`
//);

// done
return result;
}

/**
* resolve environment variables in string
*/
public static resolve_environment_variable(path: string) {
// try to replace environment variables for windows (%ENV_VAR%) and bash (${ENV_VAR} as well as $ENV_VAR)
// line as suggested in https://stackoverflow.com/questions/21363912/how-to-resolve-a-path-that-includes-an-environment-variable-in-nodejs
return path.replace(
/%([A-Z_]+[A-Z0-9_]*)%|\$([A-Z_]+[A-Z0-9_]*)|\${([A-Z0-9_]*)}/gi,
(_, windows, bash1, bash2) => process.env[windows || bash1 || bash2],
);
}
}

0 comments on commit 2f71342

Please sign in to comment.