Skip to content

Commit

Permalink
fix template parser erroring on no value
Browse files Browse the repository at this point in the history
related to #493
  • Loading branch information
MrBrax committed Apr 18, 2024
1 parent 583cf66 commit 17abb9e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
24 changes: 17 additions & 7 deletions client-vue/src/components/forms/SettingsForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
</li>
</ul>
<p class="template-preview">
{{ templatePreview(data, formData.config[key] as string) }}
{{ templatePreview(data, formData.config[key] as string || "(no data)") }}
</p>
</div>

Expand Down Expand Up @@ -340,14 +340,24 @@ function doValidateExternalURL() {
});
}
function templatePreview(data: SettingField, template: string): string {
function templatePreview(fieldData: SettingField, templateString: string): string {
// console.debug("templatePreview", data, template);
if (data.type !== "template") return "";
if (!data.replacements) return "";
const replaced_string = formatString(template, Object.fromEntries(Object.entries(data.replacements).map(([key, value]) => [key, value.display])));
if (data.context) {
if (fieldData.type !== "template") {
console.warn("templatePreview", "not a template", JSON.stringify(fieldData));
return "";
}
if (!fieldData.replacements) {
console.warn("templatePreview", "no replacements", JSON.stringify(fieldData));
return "";
}
if (!templateString) {
console.warn("templatePreview", "no template", JSON.stringify(fieldData));
return "";
}
const replaced_string = formatString(templateString, Object.fromEntries(Object.entries(fieldData.replacements).map(([key, value]) => [key, value.display])));
if (fieldData.context) {
// return data.context.replace(/{template}/g, replaced_string);
return formatString(data.context, Object.fromEntries(Object.entries(data.replacements).map(([key, value]) => [key, value.display]))).replace(
return formatString(fieldData.context, Object.fromEntries(Object.entries(fieldData.replacements).map(([key, value]) => [key, value.display]))).replace(
/{template}/g,
replaced_string,
);
Expand Down
1 change: 1 addition & 0 deletions common/ServerConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,7 @@ export const settingsFields = createSettingsFields({
group: "Exporter",
text: "Default directory",
type: "template",
default: "",
replacements: ExporterFilenameFields,
},
"exporter.default.host": {
Expand Down

0 comments on commit 17abb9e

Please sign in to comment.