From b08073b1e6250790a595d919aec4547428ab8bfc Mon Sep 17 00:00:00 2001 From: Ross Keenan Date: Fri, 28 Jan 2022 07:30:50 +0200 Subject: [PATCH] fix(Threading): :bug: Disallow illegal filename characters in threadingTemplate (fix #288) --- main.js | 21 +++++++++++++++++++-- src/Settings/ThreadingSettings.ts | 21 ++++++++++++++++++--- src/constants.ts | 14 +++++++++++++- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/main.js b/main.js index 764ab7dc..5cd4c2f1 100644 --- a/main.js +++ b/main.js @@ -9649,6 +9649,17 @@ const BC_FIELDS_INFO = [ }, ]; const BC_ALTS = BC_FIELDS_INFO.filter((f) => f.alt).map((f) => f.field); +const ILLEGAL_FILENAME_CHARS = [ + "\\", + "/", + ":", + "*", + "?", + '"', + "<", + ">", + "|", +]; const DEFAULT_SETTINGS = { addDendronNotes: false, aliasesInIndex: false, @@ -9667,7 +9678,7 @@ const DEFAULT_SETTINGS = { enableRelationSuggestor: false, fieldSuggestor: true, filterImpliedSiblingsOfDifferentTypes: false, - jugglLayout: 'hierarchy', + jugglLayout: "hierarchy", limitWriteBCCheckboxes: [], CHECKBOX_STATES_OVERWRITTEN: false, gridDots: false, @@ -37214,7 +37225,13 @@ function addThreadingSettings(plugin, cmdsDetails) { .addText((text) => { text.setValue(settings.threadingTemplate); text.inputEl.onblur = async () => { - settings.threadingTemplate = text.getValue(); + const value = text.getValue(); + if (ILLEGAL_FILENAME_CHARS.some((char) => value.includes(char))) { + new obsidian.Notice(`File name cannot contain any of these characters: ${ILLEGAL_FILENAME_CHARS.join(" ")}`); + text.setValue(settings.threadingTemplate); + return; + } + settings.threadingTemplate = value; await plugin.saveSettings(); }; }); diff --git a/src/Settings/ThreadingSettings.ts b/src/Settings/ThreadingSettings.ts index 967fe7ac..07ad48dd 100644 --- a/src/Settings/ThreadingSettings.ts +++ b/src/Settings/ThreadingSettings.ts @@ -1,5 +1,10 @@ -import { Setting } from "obsidian"; -import { ARROW_DIRECTIONS, DEFAULT_SETTINGS, DIRECTIONS } from "../constants"; +import { Notice, Setting } from "obsidian"; +import { + ARROW_DIRECTIONS, + DEFAULT_SETTINGS, + DIRECTIONS, + ILLEGAL_FILENAME_CHARS, +} from "../constants"; import type BCPlugin from "../main"; import { fragWithHTML, subDetails } from "./BreadcrumbsSettingTab"; @@ -40,7 +45,17 @@ export function addThreadingSettings( .addText((text) => { text.setValue(settings.threadingTemplate); text.inputEl.onblur = async () => { - settings.threadingTemplate = text.getValue(); + const value = text.getValue(); + if (ILLEGAL_FILENAME_CHARS.some((char) => value.includes(char))) { + new Notice( + `File name cannot contain any of these characters: ${ILLEGAL_FILENAME_CHARS.join( + " " + )}` + ); + text.setValue(settings.threadingTemplate); + return; + } + settings.threadingTemplate = value; await plugin.saveSettings(); }; }); diff --git a/src/constants.ts b/src/constants.ts index 26b3eeaf..3806f45d 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -237,6 +237,18 @@ export const BC_FIELDS_INFO = [ export const BC_ALTS = BC_FIELDS_INFO.filter((f) => f.alt).map((f) => f.field); +export const ILLEGAL_FILENAME_CHARS = [ + "\\", + "/", + ":", + "*", + "?", + '"', + "<", + ">", + "|", +]; + export const DEFAULT_SETTINGS: BCSettings = { addDendronNotes: false, aliasesInIndex: false, @@ -255,7 +267,7 @@ export const DEFAULT_SETTINGS: BCSettings = { enableRelationSuggestor: false, fieldSuggestor: true, filterImpliedSiblingsOfDifferentTypes: false, - jugglLayout: 'hierarchy', + jugglLayout: "hierarchy", limitWriteBCCheckboxes: [], CHECKBOX_STATES_OVERWRITTEN: false, gridDots: false,