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

Feature predefined variables and environment variables in configPath #654

Closed
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
47 changes: 47 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,53 @@ export class MarkdownPreviewEnhancedConfig implements MarkdownEngineConfig {
);

this.configPath = (config.get<string>("configPath") || "").trim();
// replace predefined variables in config path
{
// ... workspaceFolder
if (this.configPath.includes("${workspaceFolder}")) {
if (vscode.workspace.workspaceFolders !== undefined) {
// determine workspace folder
// let workspaceFolder = vscode.workspace.workspaceFolders[0].uri.path ;
let workspaceFolder = vscode.workspace.workspaceFolders[0].uri.fsPath;

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

// log
//vscode.window.showInformationMessage(
// `Resolved config (workspace folder): ${this.configPath}`,
//);
} else {
vscode.window.showErrorMessage(
"Working folder not found, open a folder an try again",
);
}
}
}

// replace environment variables in config path
{
// 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
let resolvedConfigPath = this.configPath.replace(
/%([A-Z_]+[A-Z0-9_]*)%|\$([A-Z_]+[A-Z0-9_]*)|\${([A-Z0-9_]*)}/gi,
(_, windows, bash1, bash2) => process.env[windows || bash1 || bash2],
);
// check if something changed
if (resolvedConfigPath != this.configPath) {
// update config path
this.configPath = resolvedConfigPath;

// log
//vscode.window.showInformationMessage(
// `Resolved config (environment variables): ${this.configPath}`,
//);
}
}

this.usePandocParser = config.get<boolean>("usePandocParser");
this.breakOnSingleNewLine = config.get<boolean>("breakOnSingleNewLine");
this.enableTypographer = config.get<boolean>("enableTypographer");
Expand Down