Skip to content
Open
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
4 changes: 2 additions & 2 deletions src/core/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { compileTemplate } from '../utils/template-compiler';
import { initializeIcons, getPropertyTypeIcon } from '../icons/icons';
import { findMatchingTemplate, initializeTriggers } from '../utils/triggers';
import { getLocalStorage, setLocalStorage, loadSettings, generalSettings, Settings } from '../utils/storage-utils';
import { escapeHtml, unescapeValue } from '../utils/string-utils';
import { escapeHtml } from '../utils/string-utils';
import { loadTemplates, createDefaultTemplate } from '../managers/template-manager';
import browser from '../utils/browser-polyfill';
import { addBrowserClassToHtml, detectBrowser } from '../utils/browser-detection';
Expand Down Expand Up @@ -745,7 +745,7 @@ async function initializeTemplateFields(currentTabId: number, template: Template
const [compiledPropertyValues, formattedNoteName, formattedPath, formattedContent] = await Promise.all([
// Compile all property values in parallel
Promise.all(template.properties.map(property =>
memoizedCompileTemplate(currentTabId!, unescapeValue(property.value), variables, currentUrl)
memoizedCompileTemplate(currentTabId!, property.value, variables, currentUrl)
)),
// Compile note name
memoizedCompileTemplate(currentTabId!, template.noteNameFormat, variables, currentUrl),
Expand Down
3 changes: 1 addition & 2 deletions src/managers/property-types-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { createElementWithClass, createElementWithHTML } from '../utils/dom-util
import { initializeIcons, getPropertyTypeIcon } from '../icons/icons';
import { templates } from './template-manager';
import { refreshPropertyNameSuggestions } from './template-ui';
import { unescapeValue } from '../utils/string-utils';
import { showImportModal } from '../utils/import-modal';
import { saveFile } from '../utils/file-utils';
import { getMessage } from '../utils/i18n';
Expand Down Expand Up @@ -105,7 +104,7 @@ function createPropertyTypeListItem(propertyType: PropertyType, usageCount: numb

const defaultValueInput = createElementWithHTML('input', '', {
type: 'text',
value: unescapeValue(propertyType.defaultValue || ''),
value: propertyType.defaultValue || '',
class: 'property-default-value',
placeholder: 'Default value'
}) as HTMLInputElement;
Expand Down
39 changes: 39 additions & 0 deletions src/managers/template-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import browser from '../utils/browser-polyfill';
import { generalSettings } from '../utils/storage-utils';
import { addPropertyType } from './property-types-manager';
import { getMessage } from '../utils/i18n';
import { SCHEMA_VERSION } from '../utils/import-export';

export let templates: Template[] = [];
export let editingTemplateIndex = -1;
Expand Down Expand Up @@ -55,6 +56,20 @@ export async function loadTemplates(): Promise<Template[]> {
await saveTemplateSettings();
}

let shouldSaveAfterPatch = false;
// Apply patch to any template with different schemaVersion
templates = templates.map((template) => {
if(!template.schemaVersion || template.schemaVersion !== SCHEMA_VERSION) {
shouldSaveAfterPatch = true;
return patchAndUpdateTemplateVersion(template);
}
return template;
});

if(shouldSaveAfterPatch) {
await saveTemplateSettings();
}

// After loading templates, update global property types
await updateGlobalPropertyTypes(templates);

Expand Down Expand Up @@ -112,6 +127,7 @@ async function prepareTemplateForSave(template: Template): Promise<[string[], st

export function createDefaultTemplate(): Template {
return {
schemaVersion: SCHEMA_VERSION,
id: Date.now().toString() + Math.random().toString(36).slice(2, 11),
name: getMessage('defaultTemplateName'),
behavior: 'create',
Expand Down Expand Up @@ -269,3 +285,26 @@ export async function cleanupTemplateStorage(): Promise<void> {
await loadTemplates();
console.log('Template storage cleaned up and rebuilt');
}

export function patchAndUpdateTemplateVersion(template: Template): Template {
console.log(`Patching template "${template.name}" from schema version ${template.schemaVersion}`);

if(!template.schemaVersion || template.schemaVersion === "0.1.0") {
template.properties = patchBackslashBeforeDoublequotesInProperties(template.properties);
}

template.schemaVersion = SCHEMA_VERSION;

return template;
}

// Fix https://github.com/obsidianmd/obsidian-clipper/issues/598
export function patchBackslashBeforeDoublequotesInProperties(properties: Property[]): Property[] {
if(!properties.length) return properties;

for(const property of properties) {
property.value = property.value.replace(/\\"/g, '"');
}

return properties;
}
7 changes: 4 additions & 3 deletions src/managers/template-ui.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Template, Property } from '../types/types';
import { deleteTemplate, templates, editingTemplateIndex, saveTemplateSettings, setEditingTemplateIndex, loadTemplates } from './template-manager';
import { initializeIcons, getPropertyTypeIcon } from '../icons/icons';
import { escapeValue, unescapeValue } from '../utils/string-utils';
import { generalSettings } from '../utils/storage-utils';
import { updateUrl } from '../utils/routing';
import { handleDragStart, handleDragOver, handleDrop, handleDragEnd } from '../utils/drag-and-drop';
Expand All @@ -11,6 +10,7 @@ import { showSettingsSection } from './settings-section-ui';
import { updatePropertyType } from './property-types-manager';
import { getMessage } from '../utils/i18n';
import { parse, validateVariables, validateFilters } from '../utils/parser';
import { SCHEMA_VERSION } from '../utils/import-export';
let hasUnsavedChanges = false;

export function resetUnsavedChanges(): void {
Expand Down Expand Up @@ -142,6 +142,7 @@ export function showTemplateEditor(template: Template | null): void {
if (!template) {
const newTemplateName = getUniqueTemplateName(getMessage('newTemplate'));
editingTemplate = {
schemaVersion: SCHEMA_VERSION,
id: Date.now().toString() + Math.random().toString(36).slice(2, 11),
name: newTemplateName,
behavior: 'create',
Expand Down Expand Up @@ -359,7 +360,7 @@ export function addPropertyToEditor(name: string = '', value: string = '', id: s
type: 'text',
class: 'property-value',
id: `${propertyId}-value`,
value: unescapeValue(value),
value: value,
placeholder: getMessage('propertyValue')
}) as HTMLInputElement;
propertyRow.appendChild(valueInput);
Expand Down Expand Up @@ -533,7 +534,7 @@ export function updateTemplateFromForm(): void {
return {
id: (prop as HTMLElement).dataset.id || Date.now().toString() + Math.random().toString(36).slice(2, 11),
name: nameInput.value,
value: escapeValue(valueInput.value),
value: valueInput.value,
type: typeSelect.getAttribute('data-value') || 'text'
};
}).filter(prop => prop.name.trim() !== ''); // Filter out properties with empty names
Expand Down
1 change: 1 addition & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface Template {
schemaVersion: string;
id: string;
name: string;
behavior: 'create' | 'append-specific' | 'append-daily' | 'prepend-specific' | 'prepend-daily' | 'overwrite';
Expand Down
9 changes: 7 additions & 2 deletions src/utils/import-export.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Template } from '../types/types';
import { templates, saveTemplateSettings, editingTemplateIndex, loadTemplates } from '../managers/template-manager';
import { templates, saveTemplateSettings, editingTemplateIndex, loadTemplates, patchAndUpdateTemplateVersion } from '../managers/template-manager';
import { showTemplateEditor, updateTemplateList } from '../managers/template-ui';
import { sanitizeFileName } from './string-utils';
import { generalSettings, loadSettings } from '../utils/storage-utils';
Expand All @@ -12,7 +12,7 @@ import { copyToClipboardWithFeedback } from './clipboard-utils';
import { compressToUTF16, decompressFromUTF16 } from 'lz-string';
import { getMessage } from './i18n';

const SCHEMA_VERSION = '0.1.0';
export const SCHEMA_VERSION = '0.1.1';

// Add these type definitions at the top
interface StorageData {
Expand Down Expand Up @@ -195,6 +195,11 @@ async function processImportedTemplate(importedTemplate: Partial<Template>): Pro
throw new Error('Invalid template file');
}

// Apply patch before futher processing
if(!importedTemplate.schemaVersion || importedTemplate.schemaVersion !== SCHEMA_VERSION) {
importedTemplate = patchAndUpdateTemplateVersion(importedTemplate as Template);
}

importedTemplate.id = Date.now().toString() + Math.random().toString(36).slice(2, 9);

// Process property types
Expand Down