123 lines
2.8 KiB
JavaScript
123 lines
2.8 KiB
JavaScript
// Notes Parser Utilities
|
|
// Handles parsing and formatting of structured notes
|
|
|
|
(function() {
|
|
'use strict';
|
|
|
|
const NotesParser = {
|
|
|
|
// Parse raw notes string into structured object
|
|
parse(noteString) {
|
|
if (!noteString) {
|
|
return {
|
|
path: '',
|
|
userNotes: '',
|
|
autoTags: []
|
|
};
|
|
}
|
|
|
|
try {
|
|
const parsed = JSON.parse(noteString);
|
|
|
|
// Validate structure
|
|
if (typeof parsed === 'object' && parsed !== null) {
|
|
return {
|
|
path: parsed.path || '',
|
|
userNotes: parsed.userNotes || '',
|
|
autoTags: parsed.autoTags || []
|
|
};
|
|
}
|
|
} catch (e) {
|
|
// Invalid JSON, treat as plain notes
|
|
}
|
|
|
|
return {
|
|
path: '',
|
|
userNotes: noteString,
|
|
autoTags: []
|
|
};
|
|
},
|
|
|
|
// Format structured notes for storage
|
|
format(notesObject) {
|
|
if (!notesObject) {
|
|
return '';
|
|
}
|
|
|
|
const notes = {
|
|
path: notesObject.path || '',
|
|
userNotes: notesObject.userNotes || '',
|
|
autoTags: notesObject.autoTags || []
|
|
};
|
|
|
|
return JSON.stringify(notes);
|
|
},
|
|
|
|
// Extract just the user notes (for display)
|
|
extractUserNotes(noteString) {
|
|
const parsed = this.parse(noteString);
|
|
return parsed.userNotes;
|
|
},
|
|
|
|
// Extract just the path (for sync operations)
|
|
extractPath(noteString) {
|
|
const parsed = this.parse(noteString);
|
|
return parsed.path;
|
|
},
|
|
|
|
// Extract auto-generated tags
|
|
extractAutoTags(noteString) {
|
|
const parsed = this.parse(noteString);
|
|
return parsed.autoTags || [];
|
|
},
|
|
|
|
// Generate tags from path
|
|
generateTagsFromPath(path) {
|
|
if (!path) {
|
|
return [];
|
|
}
|
|
|
|
// Split by slash and filter empty
|
|
const folders = path.split('/').filter(f => f);
|
|
|
|
return folders.map(folder => ({
|
|
name: folder.trim()
|
|
}));
|
|
},
|
|
|
|
// Merge two notes objects
|
|
merge(notes1, notes2) {
|
|
const parsed1 = this.parse(notes1);
|
|
const parsed2 = this.parse(notes2);
|
|
|
|
return {
|
|
path: parsed1.path || parsed2.path,
|
|
userNotes: parsed1.userNotes || parsed2.userNotes,
|
|
autoTags: [...(parsed1.autoTags || []), ...(parsed2.autoTags || [])]
|
|
.filter((v, i, a) => a.indexOf(v) === i) // Remove duplicates
|
|
};
|
|
},
|
|
|
|
// Validate notes structure
|
|
validate(notesObject) {
|
|
if (!notesObject || typeof notesObject !== 'object') {
|
|
return false;
|
|
}
|
|
|
|
return !!notesObject.path || !!notesObject.userNotes;
|
|
},
|
|
|
|
// Create minimal notes object
|
|
createEmpty() {
|
|
return {
|
|
path: '',
|
|
userNotes: '',
|
|
autoTags: []
|
|
};
|
|
}
|
|
};
|
|
|
|
// Export for use in other scripts
|
|
window.NotesParser = NotesParser;
|
|
|
|
})(); |