@clarionstack/notesia 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +108 -9
- package/bin/notesia.mjs +8 -2
- package/package.json +1 -1
- package/src/commands.mjs +261 -11
- package/src/import.mjs +27 -71
- package/src/io.mjs +88 -0
- package/src/mcp.mjs +603 -0
package/src/import.mjs
CHANGED
|
@@ -8,13 +8,12 @@
|
|
|
8
8
|
|
|
9
9
|
import fs from 'node:fs/promises';
|
|
10
10
|
import path from 'node:path';
|
|
11
|
-
import readline from 'node:readline';
|
|
12
11
|
import { requestBridge } from './client.mjs';
|
|
13
12
|
|
|
14
13
|
const NOTE_CHAR_LIMIT = 100_000;
|
|
15
14
|
const BRIDGE_MAX_REQUEST_BYTES = 1024 * 1024;
|
|
16
15
|
const BRIDGE_TARGET_REQUEST_BYTES = Math.floor(BRIDGE_MAX_REQUEST_BYTES * 0.9);
|
|
17
|
-
const DEFAULT_EXTENSIONS = ['md', 'txt', 'pdf', 'docx', 'json'];
|
|
16
|
+
const DEFAULT_EXTENSIONS = ['md', 'txt', 'html', 'htm', 'pdf', 'docx', 'json'];
|
|
18
17
|
const JSON_MAX_DEPTH = 12;
|
|
19
18
|
const JSON_MAX_ENTRIES = 5_000;
|
|
20
19
|
const JSON_MAX_ARRAY_ITEMS = 100;
|
|
@@ -179,15 +178,6 @@ function titleFromPath(filePath, relativePath) {
|
|
|
179
178
|
return stem;
|
|
180
179
|
}
|
|
181
180
|
|
|
182
|
-
function stripHtml(html) {
|
|
183
|
-
return html
|
|
184
|
-
.replace(/<br\s*\/?>/gi, '\n')
|
|
185
|
-
.replace(/<[^>]+>/g, '')
|
|
186
|
-
.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
|
|
187
|
-
.replace(/"/g, '"').replace(/'/g, "'").replace(/ /g, ' ')
|
|
188
|
-
.trim();
|
|
189
|
-
}
|
|
190
|
-
|
|
191
181
|
function decodeXmlEntities(value) {
|
|
192
182
|
if (!value) return '';
|
|
193
183
|
return value
|
|
@@ -458,13 +448,17 @@ async function parseFile(absolutePath, extension, relativePath) {
|
|
|
458
448
|
try {
|
|
459
449
|
if (extension === 'md' || extension === 'txt') {
|
|
460
450
|
const body = await fs.readFile(absolutePath, 'utf8');
|
|
461
|
-
return {
|
|
451
|
+
return {
|
|
452
|
+
title: defaultTitle,
|
|
453
|
+
body,
|
|
454
|
+
contentFormat: extension === 'md' ? 'markdown' : 'text',
|
|
455
|
+
};
|
|
462
456
|
}
|
|
463
457
|
if (extension === 'html' || extension === 'htm') {
|
|
464
458
|
const raw = await fs.readFile(absolutePath, 'utf8');
|
|
465
459
|
const titleMatch = raw.match(/<title[^>]*>([\s\S]*?)<\/title>/i);
|
|
466
460
|
const htmlTitle = titleMatch ? titleMatch[1].replace(/\s+/g, ' ').trim() : null;
|
|
467
|
-
return { title: htmlTitle || defaultTitle, body:
|
|
461
|
+
return { title: htmlTitle || defaultTitle, body: raw, contentFormat: 'html' };
|
|
468
462
|
}
|
|
469
463
|
if (extension === 'json') {
|
|
470
464
|
const sourcePath = relativePath || path.basename(absolutePath);
|
|
@@ -634,6 +628,7 @@ function normalizeImportItem(item) {
|
|
|
634
628
|
return {
|
|
635
629
|
title: item.title,
|
|
636
630
|
content: item._parseError ? '' : item.content,
|
|
631
|
+
content_format: item.content_format,
|
|
637
632
|
source_path: item.source_path,
|
|
638
633
|
_parseError: item._parseError || null,
|
|
639
634
|
};
|
|
@@ -735,26 +730,6 @@ function printResult(result, opts) {
|
|
|
735
730
|
}
|
|
736
731
|
}
|
|
737
732
|
|
|
738
|
-
// ─── Interactive prompt ──────────────────────────────────────────────────────
|
|
739
|
-
|
|
740
|
-
function createInterface() {
|
|
741
|
-
return readline.createInterface({
|
|
742
|
-
input: process.stdin,
|
|
743
|
-
output: process.stdout,
|
|
744
|
-
});
|
|
745
|
-
}
|
|
746
|
-
|
|
747
|
-
async function promptYesNo(question) {
|
|
748
|
-
return new Promise((resolve) => {
|
|
749
|
-
const rl = createInterface();
|
|
750
|
-
rl.question(question, (answer) => {
|
|
751
|
-
rl.close();
|
|
752
|
-
const normalized = answer.trim().toLowerCase();
|
|
753
|
-
resolve(normalized === 'y' || normalized === 'yes');
|
|
754
|
-
});
|
|
755
|
-
});
|
|
756
|
-
}
|
|
757
|
-
|
|
758
733
|
// ─── Main entry point ────────────────────────────────────────────────────────
|
|
759
734
|
|
|
760
735
|
export async function runImport(argv, deps = {}) {
|
|
@@ -784,13 +759,26 @@ export async function runImport(argv, deps = {}) {
|
|
|
784
759
|
|
|
785
760
|
if (parsed.error && !parsed.body) {
|
|
786
761
|
if (opts.verbose) process.stderr.write(` [failed] ${file.relativePath} — ${parsed.error}\n`);
|
|
787
|
-
items.push({
|
|
762
|
+
items.push({
|
|
763
|
+
title: parsed.title,
|
|
764
|
+
content: '',
|
|
765
|
+
content_format: parsed.contentFormat,
|
|
766
|
+
source_path: file.relativePath,
|
|
767
|
+
_parseError: parsed.error,
|
|
768
|
+
});
|
|
788
769
|
continue;
|
|
789
770
|
}
|
|
790
771
|
|
|
791
772
|
const chunks = splitContent(parsed.title, parsed.body);
|
|
792
|
-
for (const chunk of chunks) {
|
|
793
|
-
items.push({
|
|
773
|
+
for (const [chunkIndex, chunk] of chunks.entries()) {
|
|
774
|
+
items.push({
|
|
775
|
+
title: chunk.title,
|
|
776
|
+
content: chunk.content,
|
|
777
|
+
content_format: parsed.contentFormat,
|
|
778
|
+
source_path: chunkIndex === 0
|
|
779
|
+
? file.relativePath
|
|
780
|
+
: `${file.relativePath}#part-${chunkIndex + 1}`,
|
|
781
|
+
});
|
|
794
782
|
}
|
|
795
783
|
|
|
796
784
|
if (opts.verbose) {
|
|
@@ -809,11 +797,10 @@ export async function runImport(argv, deps = {}) {
|
|
|
809
797
|
failed: 0,
|
|
810
798
|
items: [],
|
|
811
799
|
};
|
|
812
|
-
let createMissingFolder = false;
|
|
813
800
|
|
|
814
801
|
for (let batchIndex = 0; batchIndex < itemBatches.length; batchIndex += 1) {
|
|
815
802
|
const batchItems = itemBatches[batchIndex];
|
|
816
|
-
|
|
803
|
+
const payload = buildBulkImportPayload(batchItems, opts, opts.createFolder);
|
|
817
804
|
|
|
818
805
|
if (opts.verbose) {
|
|
819
806
|
const estimatedBytes = estimateBridgeRequestBytes(payload);
|
|
@@ -826,8 +813,8 @@ export async function runImport(argv, deps = {}) {
|
|
|
826
813
|
let response;
|
|
827
814
|
try {
|
|
828
815
|
response = await bridge({ op: 'bulk_import', payload }, { timeoutMs });
|
|
829
|
-
process.stderr.write(` [debug] bridge response ok: ${response?.ok}, hasData: ${!!response?.data}\n`);
|
|
830
816
|
if (opts.verbose || !response?.ok) {
|
|
817
|
+
process.stderr.write(` [debug] bridge response ok: ${response?.ok}, hasData: ${!!response?.data}\n`);
|
|
831
818
|
process.stderr.write(` [debug] full response: ${JSON.stringify(response)}\n`);
|
|
832
819
|
}
|
|
833
820
|
} catch (err) {
|
|
@@ -842,39 +829,8 @@ export async function runImport(argv, deps = {}) {
|
|
|
842
829
|
|
|
843
830
|
let bridgeError = getBridgeError(response);
|
|
844
831
|
|
|
845
|
-
// Folder prompt flow happens once, then the same batch is retried with create_folder_if_missing=true.
|
|
846
|
-
if (bridgeError?.code === 'FOLDER_NOT_FOUND' && opts.folder && opts.createFolder && !createMissingFolder) {
|
|
847
|
-
const folderName = bridgeError?.details?.folderName || opts.folder;
|
|
848
|
-
process.stdout.write(`\nFolder "${folderName}" does not exist in Notesia.\n`);
|
|
849
|
-
const shouldCreate = await promptYesNo('Do you want to create it and continue import? [y/N] ');
|
|
850
|
-
if (!shouldCreate) {
|
|
851
|
-
process.stderr.write('Import cancelled.\n');
|
|
852
|
-
process.exit(1);
|
|
853
|
-
}
|
|
854
|
-
|
|
855
|
-
createMissingFolder = true;
|
|
856
|
-
payload = buildBulkImportPayload(batchItems, opts, true);
|
|
857
|
-
try {
|
|
858
|
-
response = await bridge({ op: 'bulk_import', payload }, { timeoutMs });
|
|
859
|
-
process.stderr.write(` [debug] bridge response ok: ${response?.ok}, hasData: ${!!response?.data}\n`);
|
|
860
|
-
if (opts.verbose || !response?.ok) {
|
|
861
|
-
process.stderr.write(` [debug] full response: ${JSON.stringify(response)}\n`);
|
|
862
|
-
}
|
|
863
|
-
} catch (err) {
|
|
864
|
-
const code = err.code || 'BRIDGE_ERROR';
|
|
865
|
-
if (code === 'NOTESIA_APP_NOT_RUNNING') {
|
|
866
|
-
process.stderr.write('NOTESIA_APP_NOT_RUNNING\n');
|
|
867
|
-
process.exit(1);
|
|
868
|
-
}
|
|
869
|
-
process.stderr.write(`[${code}] ${err.message}\n`);
|
|
870
|
-
process.exit(1);
|
|
871
|
-
}
|
|
872
|
-
|
|
873
|
-
bridgeError = getBridgeError(response);
|
|
874
|
-
}
|
|
875
|
-
|
|
876
832
|
if (bridgeError) {
|
|
877
|
-
if (bridgeError.code === 'FOLDER_NOT_FOUND' && opts.folder
|
|
833
|
+
if (bridgeError.code === 'FOLDER_NOT_FOUND' && opts.folder) {
|
|
878
834
|
process.stderr.write(`[${bridgeError.code}] ${bridgeError.message}\n`);
|
|
879
835
|
process.exit(1);
|
|
880
836
|
}
|
package/src/io.mjs
CHANGED
|
@@ -47,6 +47,18 @@ export function printRetrieveResult(result, {
|
|
|
47
47
|
writeStdout(String(result?.content ?? ''));
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
+
export function printOpenResult(result, {
|
|
51
|
+
format = 'text',
|
|
52
|
+
writeStdout = defaultWriteStdout,
|
|
53
|
+
} = {}) {
|
|
54
|
+
if (format === 'json') {
|
|
55
|
+
writeStdout(`${JSON.stringify(result ?? {}, null, 2)}\n`);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const title = result?.title || 'Untitled';
|
|
59
|
+
writeStdout(`Opened: ${title} (id: ${result?.id ?? 'unknown'})\n`);
|
|
60
|
+
}
|
|
61
|
+
|
|
50
62
|
export function printMutationResult(result, {
|
|
51
63
|
format = 'text',
|
|
52
64
|
writeStdout = defaultWriteStdout,
|
|
@@ -153,6 +165,32 @@ export function printAuthStatusResult(result, {
|
|
|
153
165
|
writeStdout(`${lines.join('\n')}\n`);
|
|
154
166
|
}
|
|
155
167
|
|
|
168
|
+
export function printTagsResult(result, {
|
|
169
|
+
format = 'table',
|
|
170
|
+
writeStdout = defaultWriteStdout,
|
|
171
|
+
} = {}) {
|
|
172
|
+
if (format === 'json') {
|
|
173
|
+
writeStdout(`${JSON.stringify(result ?? { tags: [], total: 0 }, null, 2)}\n`);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
const tags = Array.isArray(result?.tags) ? result.tags : [];
|
|
177
|
+
if (tags.length === 0) {
|
|
178
|
+
writeStdout('No tags found.\n');
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
const headers = ['ID', 'NAME', 'NOTES'];
|
|
182
|
+
const rows = tags.map((t) => [
|
|
183
|
+
formatCell(t.id),
|
|
184
|
+
formatCell(t.name),
|
|
185
|
+
formatCell(t.note_count),
|
|
186
|
+
]);
|
|
187
|
+
const widths = headers.map((h, i) => Math.max(h.length, ...rows.map((r) => r[i].length)));
|
|
188
|
+
const renderRow = (cells) => cells.map((c, i) => c.padEnd(widths[i])).join(' ').trimEnd();
|
|
189
|
+
const lines = [renderRow(headers), widths.map((w) => '-'.repeat(w)).join(' '), ...rows.map(renderRow)];
|
|
190
|
+
writeStdout(`${lines.join('\n')}\n`);
|
|
191
|
+
if (result?.total != null) writeStdout(`Total: ${result.total}\n`);
|
|
192
|
+
}
|
|
193
|
+
|
|
156
194
|
export function printKbResult(op, result, {
|
|
157
195
|
format = 'text',
|
|
158
196
|
writeStdout = defaultWriteStdout,
|
|
@@ -224,3 +262,53 @@ export function printKbResult(op, result, {
|
|
|
224
262
|
// Fallback for unknown kb ops
|
|
225
263
|
writeStdout(`${JSON.stringify(result ?? {}, null, 2)}\n`);
|
|
226
264
|
}
|
|
265
|
+
|
|
266
|
+
export function printFolderListResult(result, {
|
|
267
|
+
format = 'table',
|
|
268
|
+
writeStdout = defaultWriteStdout,
|
|
269
|
+
} = {}) {
|
|
270
|
+
if (format === 'json') {
|
|
271
|
+
writeStdout(`${JSON.stringify(result ?? { folders: [], total: 0, root_note_count: 0 }, null, 2)}\n`);
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const folders = Array.isArray(result?.folders) ? result.folders : [];
|
|
276
|
+
if (folders.length === 0) {
|
|
277
|
+
writeStdout('No folders found.\n');
|
|
278
|
+
if (typeof result?.root_note_count === 'number') {
|
|
279
|
+
writeStdout(`Root (unfoldered) notes: ${result.root_note_count}\n`);
|
|
280
|
+
}
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const headers = ['ID', 'PARENT_ID', 'NAME', 'NOTES', 'SYSTEM'];
|
|
285
|
+
const rows = folders.map((f) => [
|
|
286
|
+
formatCell(f.id),
|
|
287
|
+
formatCell(f.parent_id ?? '-'),
|
|
288
|
+
truncate(formatCell(f.name), 40),
|
|
289
|
+
formatCell(f.note_count ?? 0),
|
|
290
|
+
f.is_system ? 'yes' : 'no',
|
|
291
|
+
]);
|
|
292
|
+
const widths = headers.map((h, i) => Math.max(h.length, ...rows.map((r) => r[i].length)));
|
|
293
|
+
const renderRow = (cells) => cells.map((c, i) => c.padEnd(widths[i])).join(' ').trimEnd();
|
|
294
|
+
const lines = [renderRow(headers), widths.map((w) => '-'.repeat(w)).join(' '), ...rows.map(renderRow)];
|
|
295
|
+
writeStdout(`${lines.join('\n')}\n`);
|
|
296
|
+
if (typeof result?.root_note_count === 'number') {
|
|
297
|
+
writeStdout(`Root (unfoldered) notes: ${result.root_note_count}\n`);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
export function printFolderMutationResult(result, {
|
|
302
|
+
format = 'text',
|
|
303
|
+
writeStdout = defaultWriteStdout,
|
|
304
|
+
} = {}) {
|
|
305
|
+
if (format === 'json') {
|
|
306
|
+
writeStdout(`${JSON.stringify(result ?? { ok: true }, null, 2)}\n`);
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
if (result?.folder) {
|
|
310
|
+
writeStdout(`ok (folder id: ${result.folder.id}, name: ${result.folder.name})\n`);
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
writeStdout('ok\n');
|
|
314
|
+
}
|