mac csc link
This commit is contained in:
@@ -1,25 +0,0 @@
|
||||
export default class BaseNotesAdapter {
|
||||
constructor(config = {}) {
|
||||
this.config = config
|
||||
}
|
||||
|
||||
async init() {
|
||||
throw new Error('init() not implemented')
|
||||
}
|
||||
|
||||
async getAll() {
|
||||
throw new Error('getAll() not implemented')
|
||||
}
|
||||
|
||||
async create(note) {
|
||||
throw new Error('create() not implemented')
|
||||
}
|
||||
|
||||
async update(note) {
|
||||
throw new Error('update() not implemented')
|
||||
}
|
||||
|
||||
async delete(id) {
|
||||
throw new Error('delete() not implemented')
|
||||
}
|
||||
}
|
||||
@@ -2,13 +2,34 @@ import fs from 'fs/promises'
|
||||
import path from 'path'
|
||||
import { app } from 'electron'
|
||||
|
||||
const USER_DATA_STRING = '__DEFAULT_USER_DATA__'
|
||||
|
||||
export default class PluginConfig {
|
||||
constructor(defaultPlugin) {
|
||||
this.defaultPlugin = defaultPlugin
|
||||
|
||||
this.configPath = path.join(app.getPath('userData'), 'config.json')
|
||||
}
|
||||
|
||||
// Helper to replace placeholders with dynamic values, recursively
|
||||
_resolveDefaults(config) {
|
||||
if (Array.isArray(config)) {
|
||||
return config.map((item) => this._resolveDefaults(item))
|
||||
} else if (config && typeof config === 'object') {
|
||||
const resolved = {}
|
||||
for (const [key, value] of Object.entries(config)) {
|
||||
resolved[key] = this._resolveDefaults(value)
|
||||
}
|
||||
return resolved
|
||||
} else if (
|
||||
typeof config === 'string' &&
|
||||
config.includes(USER_DATA_STRING)
|
||||
) {
|
||||
return config.replace(USER_DATA_STRING, app.getPath('userData'))
|
||||
} else {
|
||||
return config
|
||||
}
|
||||
}
|
||||
|
||||
async load() {
|
||||
let parsed
|
||||
|
||||
@@ -32,6 +53,9 @@ export default class PluginConfig {
|
||||
}
|
||||
|
||||
await this.write(parsed)
|
||||
} else {
|
||||
// Ensure any "__DEFAULT_USER_DATA__" values are resolved on load
|
||||
parsed.adapterConfig = this._resolveDefaults(parsed.adapterConfig)
|
||||
}
|
||||
|
||||
return parsed
|
||||
@@ -43,9 +67,15 @@ export default class PluginConfig {
|
||||
// Ensure directory exists
|
||||
await fs.mkdir(dir, { recursive: true })
|
||||
|
||||
// Resolve defaults before writing
|
||||
const resolvedConfig = {
|
||||
...configObject,
|
||||
adapterConfig: this._resolveDefaults(configObject.adapterConfig),
|
||||
}
|
||||
|
||||
await fs.writeFile(
|
||||
this.configPath,
|
||||
JSON.stringify(configObject, null, 2),
|
||||
JSON.stringify(resolvedConfig, null, 2),
|
||||
'utf8',
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
|
||||
import { app, shell, BrowserWindow, ipcMain } from 'electron'
|
||||
import filesystemPlugin from 'takerofnotes-plugin-filesystem'
|
||||
import PluginRegistry from './core/PluginRegistry.js'
|
||||
import filesystemPlugin from './plugins/filesystem'
|
||||
import PluginConfig from './core/PluginConfig.js'
|
||||
import NotesAPI from './core/NotesAPI.js'
|
||||
import { join } from 'path'
|
||||
@@ -84,7 +84,8 @@ app.whenReady().then(async () => {
|
||||
const registry = new PluginRegistry()
|
||||
|
||||
// Register built-in plugins
|
||||
registry.register(filesystemPlugin)
|
||||
// TODO figure out why export is under default
|
||||
registry.register(filesystemPlugin.default)
|
||||
|
||||
// Pull plugin config
|
||||
const config = await new PluginConfig(filesystemPlugin).load()
|
||||
|
||||
@@ -1,159 +0,0 @@
|
||||
import fs from 'fs/promises'
|
||||
import path from 'path'
|
||||
import { app } from 'electron'
|
||||
import matter from 'gray-matter'
|
||||
import { Index } from 'flexsearch'
|
||||
import crypto from 'crypto'
|
||||
|
||||
export default class NotesAPI {
|
||||
constructor() {
|
||||
this.notesDir = path.join(app.getPath('userData'), 'notes')
|
||||
this.notesCache = new Map()
|
||||
|
||||
this.index = new Index({
|
||||
tokenize: 'tolerant',
|
||||
resolution: 9,
|
||||
})
|
||||
}
|
||||
|
||||
async init() {
|
||||
await fs.mkdir(this.notesDir, { recursive: true })
|
||||
await this._loadAllNotes()
|
||||
}
|
||||
|
||||
async _loadAllNotes() {
|
||||
const files = await fs.readdir(this.notesDir)
|
||||
|
||||
for (const file of files) {
|
||||
if (!file.endsWith('.md')) continue
|
||||
|
||||
const fullPath = path.join(this.notesDir, file)
|
||||
const raw = await fs.readFile(fullPath, 'utf8')
|
||||
const parsed = matter(raw)
|
||||
|
||||
const note = {
|
||||
...parsed.data,
|
||||
content: parsed.content,
|
||||
}
|
||||
|
||||
this.notesCache.set(note.id, note)
|
||||
this.index.add(note.id, note.title + note.content)
|
||||
}
|
||||
}
|
||||
|
||||
async _writeNoteFile(note) {
|
||||
const filePath = path.join(this.notesDir, `${note.id}.md`)
|
||||
|
||||
const fileContent = matter.stringify(note.content, {
|
||||
id: note.id,
|
||||
title: note.title,
|
||||
category: note.category ?? null,
|
||||
createdAt: note.createdAt,
|
||||
updatedAt: note.updatedAt,
|
||||
})
|
||||
|
||||
await fs.writeFile(filePath, fileContent, 'utf8')
|
||||
}
|
||||
|
||||
/* -----------------------
|
||||
Public API
|
||||
------------------------*/
|
||||
getCategories() {
|
||||
const categories = new Set()
|
||||
|
||||
for (const note of this.notesCache.values()) {
|
||||
if (note.category) {
|
||||
categories.add(note.category)
|
||||
}
|
||||
}
|
||||
|
||||
return Array.from(categories).sort()
|
||||
}
|
||||
|
||||
getCategoryNotes(categoryName) {
|
||||
return Array.from(this.notesCache.values())
|
||||
.filter((n) => n.category === categoryName)
|
||||
.sort((a, b) => new Date(b.updatedAt) - new Date(a.updatedAt))
|
||||
}
|
||||
|
||||
getNote(id) {
|
||||
return this.notesCache.get(id) ?? null
|
||||
}
|
||||
|
||||
async createNote(metadata = {}, content = '') {
|
||||
const id = crypto.randomUUID()
|
||||
const now = new Date().toISOString()
|
||||
|
||||
const note = {
|
||||
id,
|
||||
title: metadata.title || 'Untitled',
|
||||
category: metadata.category || null,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
content,
|
||||
}
|
||||
|
||||
console.log(note)
|
||||
|
||||
this.notesCache.set(id, note)
|
||||
this.index.add(id, note.title + '\n' + content)
|
||||
|
||||
await this._writeNoteFile(note)
|
||||
|
||||
return note
|
||||
}
|
||||
|
||||
async deleteNote(id) {
|
||||
const filePath = path.join(this.notesDir, `${id}.md`)
|
||||
await fs.unlink(filePath)
|
||||
this.notesCache.delete(id)
|
||||
this.index.remove(id)
|
||||
}
|
||||
|
||||
async updateNote(id, content) {
|
||||
const note = this.notesCache.get(id)
|
||||
if (!note) throw new Error('Note not found')
|
||||
|
||||
note.content = content
|
||||
note.updatedAt = new Date().toISOString()
|
||||
|
||||
this.index.update(id, note.title + '\n' + content)
|
||||
|
||||
await this._writeNoteFile(note)
|
||||
|
||||
return note
|
||||
}
|
||||
|
||||
async updateNoteMetadata(id, updates = {}) {
|
||||
const note = this.notesCache.get(id)
|
||||
if (!note) throw new Error('Note not found')
|
||||
|
||||
const allowedFields = ['title', 'category']
|
||||
for (const key of Object.keys(updates)) {
|
||||
if (!allowedFields.includes(key)) {
|
||||
throw new Error(`Invalid metadata field: ${key}`)
|
||||
}
|
||||
}
|
||||
|
||||
if (updates.title !== undefined) {
|
||||
note.title = updates.title
|
||||
}
|
||||
|
||||
if (updates.category !== undefined) {
|
||||
note.category = updates.category
|
||||
}
|
||||
|
||||
note.updatedAt = new Date().toISOString()
|
||||
|
||||
this.index.update(id, note.title + '\n' + note.content)
|
||||
|
||||
await this._writeNoteFile(note)
|
||||
|
||||
return note
|
||||
}
|
||||
|
||||
search(query) {
|
||||
const ids = this.index.search(query)
|
||||
return ids.map((id) => this.notesCache.get(id))
|
||||
}
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
import BaseNotesAdapter from '../../core/BaseNotesAdapter.js'
|
||||
import matter from 'gray-matter'
|
||||
import fs from 'fs/promises'
|
||||
import path from 'path'
|
||||
|
||||
export default class FileSystemNotesAdapter extends BaseNotesAdapter {
|
||||
constructor(config) {
|
||||
super()
|
||||
|
||||
for (const field in config) {
|
||||
this[field] = config[field]
|
||||
}
|
||||
}
|
||||
|
||||
async init() {
|
||||
await fs.mkdir(this.notesDir, { recursive: true })
|
||||
}
|
||||
|
||||
async getAll() {
|
||||
const files = await fs.readdir(this.notesDir)
|
||||
const notes = []
|
||||
|
||||
for (const file of files) {
|
||||
if (!file.endsWith('.md')) continue
|
||||
|
||||
const fullPath = path.join(this.notesDir, file)
|
||||
const raw = await fs.readFile(fullPath, 'utf8')
|
||||
const parsed = matter(raw)
|
||||
|
||||
notes.push({
|
||||
...parsed.data,
|
||||
content: parsed.content,
|
||||
})
|
||||
}
|
||||
|
||||
return notes
|
||||
}
|
||||
|
||||
async create(note) {
|
||||
await this._write(note)
|
||||
}
|
||||
|
||||
async update(note) {
|
||||
await this._write(note)
|
||||
}
|
||||
|
||||
async delete(id) {
|
||||
const filePath = path.join(this.notesDir, `${id}.md`)
|
||||
await fs.unlink(filePath)
|
||||
}
|
||||
|
||||
async _write(note) {
|
||||
const filePath = path.join(this.notesDir, `${note.id}.md`)
|
||||
|
||||
const fileContent = matter.stringify(note.content, {
|
||||
id: note.id,
|
||||
title: note.title,
|
||||
category: note.category ?? null,
|
||||
createdAt: note.createdAt,
|
||||
updatedAt: note.updatedAt,
|
||||
})
|
||||
|
||||
await fs.writeFile(filePath, fileContent, 'utf8')
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
import path from 'path'
|
||||
import { app } from 'electron'
|
||||
import FileSystemAdapter from './FileSystemAdapter.js'
|
||||
|
||||
export default {
|
||||
id: 'filesystem',
|
||||
name: 'Local Filesystem',
|
||||
description: 'Stores notes as markdown files locally.',
|
||||
version: '1.0.0',
|
||||
|
||||
configSchema: [
|
||||
{
|
||||
key: 'notesDir',
|
||||
label: 'Notes Directory',
|
||||
type: 'directory',
|
||||
default: path.join(app.getPath('userData'), 'notes'),
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
|
||||
createAdapter(config) {
|
||||
return new FileSystemAdapter(config)
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user