Skip to content
Merged
Show file tree
Hide file tree
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
13 changes: 10 additions & 3 deletions apps/obsidian/src/components/NodeTypeModal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { App, Editor, SuggestModal, TFile, Notice } from "obsidian";
import { DiscourseNode } from "../types";
import { getDiscourseNodeFormatExpression } from "../utils/getDiscourseNodeFormatExpression";
import { DiscourseNode } from "~/types";
import { getDiscourseNodeFormatExpression } from "~/utils/getDiscourseNodeFormatExpression";
import { checkInvalidChars } from "~/utils/validateNodeType";

export class NodeTypeModal extends SuggestModal<DiscourseNode> {
constructor(
Expand All @@ -25,6 +26,7 @@ export class NodeTypeModal extends SuggestModal<DiscourseNode> {
renderSuggestion(nodeType: DiscourseNode, el: HTMLElement) {
el.createEl("div", { text: nodeType.name });
}

async createDiscourseNode(
title: string,
nodeType: DiscourseNode,
Expand Down Expand Up @@ -67,7 +69,12 @@ export class NodeTypeModal extends SuggestModal<DiscourseNode> {
nodeFormat[1]?.replace(/\\/g, "") +
selectedText +
nodeFormat[2]?.replace(/\\/g, "");
if (!nodeFormat) return;

const isFilenameValid = checkInvalidChars(formattedNodeName);
if (!isFilenameValid.isValid) {
new Notice(`${isFilenameValid.error}`, 5000);
return;
}

const newFile = await this.createDiscourseNode(formattedNodeName, nodeType);
if (newFile) {
Expand Down
2 changes: 1 addition & 1 deletion apps/obsidian/src/components/NodeTypeSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ const NodeTypeSettings = () => {
/>
<input
type="text"
placeholder="Format (e.g., [CLM] - {content})"
placeholder="Format (e.g., CLM - {content})"
value={nodeType.format}
onChange={(e) =>
handleNodeTypeChange(index, "format", e.target.value)
Expand Down
38 changes: 29 additions & 9 deletions apps/obsidian/src/utils/validateNodeType.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { DiscourseNode } from "~/types";

type ValidationResult = {
isValid: boolean;
error?: string;
};

export function validateNodeFormat(
format: string,
nodeTypes: DiscourseNode[],
): {
isValid: boolean;
error?: string;
} {
): ValidationResult {
if (!format) {
return {
isValid: false,
Expand All @@ -28,17 +30,35 @@ export function validateNodeFormat(
};
}

const { isValid, error } = validateFormatUniqueness(nodeTypes);
if (!isValid) {
return { isValid: false, error };
const invalidCharsResult = checkInvalidChars(format);
if (!invalidCharsResult.isValid) {
return invalidCharsResult;
}

const uniquenessResult = validateFormatUniqueness(nodeTypes);
if (!uniquenessResult.isValid) {
return uniquenessResult;
}

return { isValid: true };
}

export const checkInvalidChars = (format: string): ValidationResult => {
const INVALID_FILENAME_CHARS_REGEX = /[#^\[\]|]/;
const invalidCharMatch = format.match(INVALID_FILENAME_CHARS_REGEX);
if (invalidCharMatch) {
return {
isValid: false,
error: `Node contains invalid character: ${invalidCharMatch[0]}. Characters #, ^, [, ], | cannot be used in filenames.`,
};
}

return { isValid: true };
};

const validateFormatUniqueness = (
nodeTypes: DiscourseNode[],
): { isValid: boolean; error?: string } => {
): ValidationResult => {
const isDuplicate =
new Set(nodeTypes.map((nodeType) => nodeType.format)).size !==
nodeTypes.length;
Expand All @@ -53,7 +73,7 @@ const validateFormatUniqueness = (
export const validateNodeName = (
name: string,
nodeTypes: DiscourseNode[],
): { isValid: boolean; error?: string } => {
): ValidationResult => {
if (!name || name.trim() === "") {
return { isValid: false, error: "Name is required" };
}
Expand Down