@skillre/dsh-plugin-sticky-wall 0.6.0 → 0.8.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.en.md +38 -15
- package/README.md +39 -18
- package/UNINSTALL.md +22 -7
- package/compatibility.json +83 -4
- package/lib/client.js +1174 -369
- package/lib/config.d.ts +18 -0
- package/lib/config.js +27 -0
- package/lib/frontmatter.d.ts +81 -0
- package/lib/frontmatter.js +440 -0
- package/lib/index.d.ts +9 -4
- package/lib/index.js +153 -36
- package/lib/layout.d.ts +27 -5
- package/lib/layout.js +82 -25
- package/lib/migrate.d.ts +53 -0
- package/lib/migrate.js +156 -0
- package/lib/store.d.ts +123 -68
- package/lib/store.js +305 -96
- package/lib/transport.d.ts +7 -4
- package/lib/transport.js +25 -4
- package/lib/types.d.ts +117 -7
- package/lib/types.js +38 -5
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +3 -3
- package/docs/PREVIEW.md +0 -39
package/lib/config.d.ts
CHANGED
|
@@ -9,6 +9,15 @@
|
|
|
9
9
|
import Schema from '@deepseek-ai/schemastery';
|
|
10
10
|
/** Effective plugin configuration after schema resolution. */
|
|
11
11
|
export interface WallConfig {
|
|
12
|
+
/**
|
|
13
|
+
* Directory holding one `.md` file per note.
|
|
14
|
+
*
|
|
15
|
+
* Empty means "follow the DSH home": `$DSH_HOME/sticky-notes`, falling back to
|
|
16
|
+
* `~/.dsh/sticky-notes`. Following the harness home is what keeps an isolated
|
|
17
|
+
* profile's notes inside that profile instead of in the user's real
|
|
18
|
+
* installation, and it is also what makes the wall span workspaces.
|
|
19
|
+
*/
|
|
20
|
+
notesDir: string;
|
|
12
21
|
/** Reject writes whose note count would exceed this cap. */
|
|
13
22
|
maxNotes: number;
|
|
14
23
|
/** Register the `sticky_wall` model tool so an agent can capture notes. */
|
|
@@ -20,3 +29,12 @@ export interface WallConfig {
|
|
|
20
29
|
export declare const Config: Schema<WallConfig>;
|
|
21
30
|
/** Resolve raw row config, keeping the declared defaults authoritative. */
|
|
22
31
|
export declare function resolveConfig(raw?: unknown): WallConfig;
|
|
32
|
+
/**
|
|
33
|
+
* Resolve the notes directory for one deployment.
|
|
34
|
+
*
|
|
35
|
+
* Order: an explicit `notesDir` wins; otherwise the directory follows
|
|
36
|
+
* `DSH_HOME` when the harness sets it, and only then falls back to `~/.dsh`.
|
|
37
|
+
* This mirrors the fleet's other data-owning plugins, and it is the reason an
|
|
38
|
+
* isolated test profile cannot read or clobber the user's real notes.
|
|
39
|
+
*/
|
|
40
|
+
export declare function resolveNotesDir(configured: string, env: NodeJS.ProcessEnv, home: string): string;
|
package/lib/config.js
CHANGED
|
@@ -7,8 +7,10 @@
|
|
|
7
7
|
* corrupting a wall silently.
|
|
8
8
|
*/
|
|
9
9
|
import Schema from '@deepseek-ai/schemastery';
|
|
10
|
+
import { NOTES_DIR_NAME } from './types.js';
|
|
10
11
|
/** The schema DSH resolves a `skillre-sticky-wall` row's config through. */
|
|
11
12
|
export const Config = Schema.object({
|
|
13
|
+
notesDir: Schema.string().default(''),
|
|
12
14
|
maxNotes: Schema.number().min(1).max(5000).step(1).default(400),
|
|
13
15
|
enableAgentTool: Schema.boolean().default(true),
|
|
14
16
|
seedWelcomeNote: Schema.boolean().default(true),
|
|
@@ -19,3 +21,28 @@ export function resolveConfig(raw = {}) {
|
|
|
19
21
|
// carries the full shape; the cast states that instead of re-declaring it.
|
|
20
22
|
return Config(raw);
|
|
21
23
|
}
|
|
24
|
+
/** Expand a leading `~/` against the user's home directory. */
|
|
25
|
+
function expandHome(configured, home) {
|
|
26
|
+
if (configured === '~')
|
|
27
|
+
return home;
|
|
28
|
+
if (configured.startsWith('~/'))
|
|
29
|
+
return `${home}/${configured.slice(2)}`;
|
|
30
|
+
return configured;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Resolve the notes directory for one deployment.
|
|
34
|
+
*
|
|
35
|
+
* Order: an explicit `notesDir` wins; otherwise the directory follows
|
|
36
|
+
* `DSH_HOME` when the harness sets it, and only then falls back to `~/.dsh`.
|
|
37
|
+
* This mirrors the fleet's other data-owning plugins, and it is the reason an
|
|
38
|
+
* isolated test profile cannot read or clobber the user's real notes.
|
|
39
|
+
*/
|
|
40
|
+
export function resolveNotesDir(configured, env, home) {
|
|
41
|
+
const value = configured.trim();
|
|
42
|
+
if (value !== '')
|
|
43
|
+
return expandHome(value, home).replace(/\/+$/, '');
|
|
44
|
+
const dshHome = env['DSH_HOME']?.trim();
|
|
45
|
+
if (dshHome !== undefined && dshHome !== '')
|
|
46
|
+
return `${dshHome.replace(/\/+$/, '')}/${NOTES_DIR_NAME}`;
|
|
47
|
+
return `${home.replace(/\/+$/, '')}/.dsh/${NOTES_DIR_NAME}`;
|
|
48
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `.md` file format: YAML frontmatter plus a free-text body.
|
|
3
|
+
*
|
|
4
|
+
* A note is a real file the user owns. The frontmatter carries the structured
|
|
5
|
+
* attributes (id, colour, placement, timestamps); everything after the closing
|
|
6
|
+
* delimiter is the body, which is ordinary Markdown that a person or a model can
|
|
7
|
+
* edit with any tool.
|
|
8
|
+
*
|
|
9
|
+
* Three properties matter more than completeness here:
|
|
10
|
+
*
|
|
11
|
+
* 1. **Never throw on bad input.** `parse` always returns a document. A broken
|
|
12
|
+
* YAML block becomes a note with default attributes and a `metaError`, and the
|
|
13
|
+
* raw text is kept as the body rather than discarded. A wall must not lose a
|
|
14
|
+
* note because one line of YAML is malformed — that is the fail-soft contract
|
|
15
|
+
* the storage domain used to give us for free.
|
|
16
|
+
*
|
|
17
|
+
* 2. **The body is never the frontmatter.** The body is exactly the text after
|
|
18
|
+
* the closing delimiter. Nothing here normalizes, trims or reflows it: the
|
|
19
|
+
* card height estimate walks this text line by line, and a stray trim would
|
|
20
|
+
* change every card's geometry. It is also what keeps a round-trip lossless.
|
|
21
|
+
*
|
|
22
|
+
* 3. **Unknown keys survive.** A newer build, or the user's own hand-written
|
|
23
|
+
* `due:` field, is preserved in `extra` and written back. Silently dropping it
|
|
24
|
+
* would destroy data the plugin did not create.
|
|
25
|
+
*
|
|
26
|
+
* The parser deliberately implements a SMALL YAML subset — scalars, inline lists,
|
|
27
|
+
* booleans, numbers and quoted strings — because that is the entire surface the
|
|
28
|
+
* frontmatter uses. A full YAML engine would add weight and, worse, would accept
|
|
29
|
+
* constructs this module cannot faithfully re-serialize.
|
|
30
|
+
*/
|
|
31
|
+
import { type Note } from './types.js';
|
|
32
|
+
/** One parsed note file: the note plus anything the round-trip must preserve. */
|
|
33
|
+
export interface ParsedNoteFile {
|
|
34
|
+
note: Note;
|
|
35
|
+
/** True when the file had a frontmatter block, readable or not. */
|
|
36
|
+
hadFrontmatter: boolean;
|
|
37
|
+
}
|
|
38
|
+
/** Everything `parse` needs to know about the file it is reading. */
|
|
39
|
+
export interface ParseOptions {
|
|
40
|
+
/** Fallback identity when the frontmatter has no `id` (the basename). */
|
|
41
|
+
fallbackId: string;
|
|
42
|
+
/** Fallback timestamps, normally the file's mtime. */
|
|
43
|
+
fallbackTime: string;
|
|
44
|
+
}
|
|
45
|
+
/** Normalize a timestamp to ISO-8601; unparseable input falls back. */
|
|
46
|
+
export declare function normalizeTime(raw: unknown, fallback: string): string;
|
|
47
|
+
/** Build a local ISO timestamp (with offset) for a fresh note. */
|
|
48
|
+
export declare function nowIso(at?: Date): string;
|
|
49
|
+
/** Locate the frontmatter block; returns `null` when the file has none. */
|
|
50
|
+
export declare function findFrontmatter(text: string): {
|
|
51
|
+
raw: string;
|
|
52
|
+
body: string;
|
|
53
|
+
} | null;
|
|
54
|
+
/**
|
|
55
|
+
* Parse a frontmatter block into its key/value pairs, last writer winning.
|
|
56
|
+
*
|
|
57
|
+
* `malformed` collects top-level lines that are not `key: value`, so the caller
|
|
58
|
+
* can flag the note while still keeping every value it did understand.
|
|
59
|
+
*/
|
|
60
|
+
export declare function parseFrontmatter(raw: string, malformed?: string[]): Map<string, string>;
|
|
61
|
+
/**
|
|
62
|
+
* Parse one note file.
|
|
63
|
+
*
|
|
64
|
+
* Never throws. A frontmatter block that cannot be read yields a note with
|
|
65
|
+
* defaults, `metaError` set, and the WHOLE FILE as the body, so nothing typed is
|
|
66
|
+
* lost and the wall still shows the card.
|
|
67
|
+
*/
|
|
68
|
+
export declare function parseNoteFile(text: string, options: ParseOptions): ParsedNoteFile;
|
|
69
|
+
/**
|
|
70
|
+
* Serialize one note back to file text.
|
|
71
|
+
*
|
|
72
|
+
* The output is stable: the same note always produces the same bytes, so a
|
|
73
|
+
* no-op save cannot churn the file's mtime and trigger a spurious refresh.
|
|
74
|
+
*/
|
|
75
|
+
export declare function serializeNoteFile(note: Note): string;
|
|
76
|
+
/** True when a file name is a plausible note id, so a path cannot escape. */
|
|
77
|
+
export declare function isNoteId(value: string): boolean;
|
|
78
|
+
/** The `.md` basename for a note id. */
|
|
79
|
+
export declare function fileNameOf(id: string): string;
|
|
80
|
+
/** Recover a note id from a file's basename; `null` when it is not one of ours. */
|
|
81
|
+
export declare function idOfFile(name: string): string | null;
|
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `.md` file format: YAML frontmatter plus a free-text body.
|
|
3
|
+
*
|
|
4
|
+
* A note is a real file the user owns. The frontmatter carries the structured
|
|
5
|
+
* attributes (id, colour, placement, timestamps); everything after the closing
|
|
6
|
+
* delimiter is the body, which is ordinary Markdown that a person or a model can
|
|
7
|
+
* edit with any tool.
|
|
8
|
+
*
|
|
9
|
+
* Three properties matter more than completeness here:
|
|
10
|
+
*
|
|
11
|
+
* 1. **Never throw on bad input.** `parse` always returns a document. A broken
|
|
12
|
+
* YAML block becomes a note with default attributes and a `metaError`, and the
|
|
13
|
+
* raw text is kept as the body rather than discarded. A wall must not lose a
|
|
14
|
+
* note because one line of YAML is malformed — that is the fail-soft contract
|
|
15
|
+
* the storage domain used to give us for free.
|
|
16
|
+
*
|
|
17
|
+
* 2. **The body is never the frontmatter.** The body is exactly the text after
|
|
18
|
+
* the closing delimiter. Nothing here normalizes, trims or reflows it: the
|
|
19
|
+
* card height estimate walks this text line by line, and a stray trim would
|
|
20
|
+
* change every card's geometry. It is also what keeps a round-trip lossless.
|
|
21
|
+
*
|
|
22
|
+
* 3. **Unknown keys survive.** A newer build, or the user's own hand-written
|
|
23
|
+
* `due:` field, is preserved in `extra` and written back. Silently dropping it
|
|
24
|
+
* would destroy data the plugin did not create.
|
|
25
|
+
*
|
|
26
|
+
* The parser deliberately implements a SMALL YAML subset — scalars, inline lists,
|
|
27
|
+
* booleans, numbers and quoted strings — because that is the entire surface the
|
|
28
|
+
* frontmatter uses. A full YAML engine would add weight and, worse, would accept
|
|
29
|
+
* constructs this module cannot faithfully re-serialize.
|
|
30
|
+
*/
|
|
31
|
+
import { COLORS, FRONTMATTER_SCHEMA, LIMITS, WIDTHS, } from './types.js';
|
|
32
|
+
/** Frontmatter keys this build understands and re-serializes itself. */
|
|
33
|
+
const KNOWN_KEYS = new Set([
|
|
34
|
+
'id',
|
|
35
|
+
'schema',
|
|
36
|
+
'title',
|
|
37
|
+
'color',
|
|
38
|
+
'tags',
|
|
39
|
+
'pinned',
|
|
40
|
+
'archived',
|
|
41
|
+
'width',
|
|
42
|
+
'x',
|
|
43
|
+
'y',
|
|
44
|
+
'rotate',
|
|
45
|
+
'created',
|
|
46
|
+
'updated',
|
|
47
|
+
]);
|
|
48
|
+
/** The opening/closing delimiter. `---` opens; either form closes. */
|
|
49
|
+
const DELIMITER = '---';
|
|
50
|
+
/** Zero-pad a number to two digits. */
|
|
51
|
+
function pad(value) {
|
|
52
|
+
return String(value).padStart(2, '0');
|
|
53
|
+
}
|
|
54
|
+
/** Normalize a timestamp to ISO-8601; unparseable input falls back. */
|
|
55
|
+
export function normalizeTime(raw, fallback) {
|
|
56
|
+
if (typeof raw === 'string' && raw.trim() !== '') {
|
|
57
|
+
const trimmed = raw.trim();
|
|
58
|
+
// An unquoted YAML timestamp parses as a Date; keep the authored string when
|
|
59
|
+
// it is already a valid instant so timezone information is not lost.
|
|
60
|
+
const stamp = new Date(trimmed);
|
|
61
|
+
if (!Number.isNaN(stamp.getTime()))
|
|
62
|
+
return trimmed;
|
|
63
|
+
}
|
|
64
|
+
return fallback;
|
|
65
|
+
}
|
|
66
|
+
/** Build a local ISO timestamp (with offset) for a fresh note. */
|
|
67
|
+
export function nowIso(at = new Date()) {
|
|
68
|
+
const offset = -at.getTimezoneOffset();
|
|
69
|
+
const sign = offset >= 0 ? '+' : '-';
|
|
70
|
+
const absolute = Math.abs(offset);
|
|
71
|
+
return (`${String(at.getFullYear())}-${pad(at.getMonth() + 1)}-${pad(at.getDate())}` +
|
|
72
|
+
`T${pad(at.getHours())}:${pad(at.getMinutes())}:${pad(at.getSeconds())}` +
|
|
73
|
+
`.${String(at.getMilliseconds()).padStart(3, '0')}` +
|
|
74
|
+
`${sign}${pad(Math.floor(absolute / 60))}:${pad(absolute % 60)}`);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Strip a surrounding pair of quotes, if present.
|
|
78
|
+
*
|
|
79
|
+
* Both double and single quotes are accepted. A double-quoted scalar also has its
|
|
80
|
+
* backslash escapes unescaped, because {@link quote} writes them that way and the
|
|
81
|
+
* pair must round-trip.
|
|
82
|
+
*/
|
|
83
|
+
function unquote(raw) {
|
|
84
|
+
const text = raw.trim();
|
|
85
|
+
if (text.length >= 2 && text.startsWith('"') && text.endsWith('"')) {
|
|
86
|
+
const inner = text.slice(1, -1);
|
|
87
|
+
try {
|
|
88
|
+
return JSON.parse(`"${inner}"`);
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return inner;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (text.length >= 2 && text.startsWith("'") && text.endsWith("'")) {
|
|
95
|
+
// YAML single quotes escape an inner quote by doubling it.
|
|
96
|
+
return text.slice(1, -1).replace(/''/g, "'");
|
|
97
|
+
}
|
|
98
|
+
return text;
|
|
99
|
+
}
|
|
100
|
+
/** Read one scalar as a boolean, accepting the YAML spellings. */
|
|
101
|
+
function asBoolean(raw) {
|
|
102
|
+
if (typeof raw !== 'string')
|
|
103
|
+
return undefined;
|
|
104
|
+
const value = raw.trim().toLowerCase();
|
|
105
|
+
if (value === 'true' || value === 'yes' || value === 'on')
|
|
106
|
+
return true;
|
|
107
|
+
if (value === 'false' || value === 'no' || value === 'off')
|
|
108
|
+
return false;
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
/** Read one scalar as a finite number, or `undefined`. */
|
|
112
|
+
function asNumber(raw) {
|
|
113
|
+
if (typeof raw !== 'string' || raw.trim() === '')
|
|
114
|
+
return undefined;
|
|
115
|
+
const value = Number(raw.trim());
|
|
116
|
+
return Number.isFinite(value) ? value : undefined;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Split an inline YAML list (`[a, b, c]`) into its items.
|
|
120
|
+
*
|
|
121
|
+
* A bare scalar is treated as a one-item list, which is what a hand-written
|
|
122
|
+
* `tags: work` means. An empty `[]` is an empty list.
|
|
123
|
+
*/
|
|
124
|
+
function parseList(raw) {
|
|
125
|
+
const text = raw.trim();
|
|
126
|
+
if (text === '')
|
|
127
|
+
return [];
|
|
128
|
+
if (text.startsWith('[') && text.endsWith(']')) {
|
|
129
|
+
const inner = text.slice(1, -1).trim();
|
|
130
|
+
if (inner === '')
|
|
131
|
+
return [];
|
|
132
|
+
return splitInline(inner).map((item) => unquote(item)).filter((item) => item !== '');
|
|
133
|
+
}
|
|
134
|
+
const single = unquote(text);
|
|
135
|
+
return single === '' ? [] : [single];
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Split a comma-separated inline list, honouring quotes.
|
|
139
|
+
*
|
|
140
|
+
* A naive `split(',')` breaks `tags: ["a,b", c]`, so the scan tracks quote state.
|
|
141
|
+
*/
|
|
142
|
+
function splitInline(text) {
|
|
143
|
+
const items = [];
|
|
144
|
+
let current = '';
|
|
145
|
+
let quote = null;
|
|
146
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
147
|
+
const char = text[index] ?? '';
|
|
148
|
+
if (quote !== null) {
|
|
149
|
+
current += char;
|
|
150
|
+
if (char === quote)
|
|
151
|
+
quote = null;
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (char === '"' || char === "'") {
|
|
155
|
+
quote = char;
|
|
156
|
+
current += char;
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
if (char === ',') {
|
|
160
|
+
items.push(current);
|
|
161
|
+
current = '';
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
current += char;
|
|
165
|
+
}
|
|
166
|
+
if (current.trim() !== '')
|
|
167
|
+
items.push(current);
|
|
168
|
+
return items;
|
|
169
|
+
}
|
|
170
|
+
/** Locate the frontmatter block; returns `null` when the file has none. */
|
|
171
|
+
export function findFrontmatter(text) {
|
|
172
|
+
// Only the FIRST line may open the block. A `---` further down is a horizontal
|
|
173
|
+
// rule in the body, and treating it as a delimiter would swallow real content.
|
|
174
|
+
const firstBreak = text.indexOf('\n');
|
|
175
|
+
const firstLine = (firstBreak === -1 ? text : text.slice(0, firstBreak)).replace(/\r$/, '').trim();
|
|
176
|
+
if (firstLine !== DELIMITER)
|
|
177
|
+
return null;
|
|
178
|
+
if (firstBreak === -1)
|
|
179
|
+
return null;
|
|
180
|
+
const rest = text.slice(firstBreak + 1);
|
|
181
|
+
const lines = rest.split('\n');
|
|
182
|
+
let offset = 0;
|
|
183
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
184
|
+
const line = (lines[index] ?? '').replace(/\r$/, '');
|
|
185
|
+
const trimmed = line.trim();
|
|
186
|
+
// An opening delimiter with no closing one is not frontmatter: the user is
|
|
187
|
+
// writing a document that starts with a rule. Fall back to plain body.
|
|
188
|
+
if (trimmed === DELIMITER || trimmed === '...') {
|
|
189
|
+
const bodyStart = offset + line.length + 1;
|
|
190
|
+
return { raw: rest.slice(0, offset), body: rest.slice(bodyStart) };
|
|
191
|
+
}
|
|
192
|
+
offset += line.length + 1;
|
|
193
|
+
}
|
|
194
|
+
return null;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Parse a frontmatter block into its key/value pairs, last writer winning.
|
|
198
|
+
*
|
|
199
|
+
* `malformed` collects top-level lines that are not `key: value`, so the caller
|
|
200
|
+
* can flag the note while still keeping every value it did understand.
|
|
201
|
+
*/
|
|
202
|
+
export function parseFrontmatter(raw, malformed = []) {
|
|
203
|
+
const entries = new Map();
|
|
204
|
+
const lines = raw.replace(/\r\n?/g, '\n').split('\n');
|
|
205
|
+
let currentKey = null;
|
|
206
|
+
let currentValue = '';
|
|
207
|
+
const flush = () => {
|
|
208
|
+
if (currentKey !== null)
|
|
209
|
+
entries.set(currentKey, currentValue.trim());
|
|
210
|
+
currentKey = null;
|
|
211
|
+
currentValue = '';
|
|
212
|
+
};
|
|
213
|
+
for (const line of lines) {
|
|
214
|
+
if (line.trim() === '' || line.trimStart().startsWith('#'))
|
|
215
|
+
continue;
|
|
216
|
+
// An indented line continues the previous key's value (a folded or block
|
|
217
|
+
// scalar). The subset does not interpret YAML block syntax, but it must not
|
|
218
|
+
// silently attribute a continuation to the wrong key.
|
|
219
|
+
if (/^\s/.test(line) && currentKey !== null) {
|
|
220
|
+
currentValue += ` ${line.trim()}`;
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
const match = line.match(/^([A-Za-z0-9_-]+)\s*:\s*(.*)$/);
|
|
224
|
+
if (match === null) {
|
|
225
|
+
// A top-level line that is neither `key: value` nor a continuation is
|
|
226
|
+
// genuinely malformed for this subset. Record it so the caller can flag the
|
|
227
|
+
// note instead of pretending the block was fully understood.
|
|
228
|
+
flush();
|
|
229
|
+
malformed.push(line.trim());
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
flush();
|
|
233
|
+
currentKey = match[1] ?? '';
|
|
234
|
+
currentValue = match[2] ?? '';
|
|
235
|
+
}
|
|
236
|
+
flush();
|
|
237
|
+
return entries;
|
|
238
|
+
}
|
|
239
|
+
/** Normalize a tag list to the stored shape. */
|
|
240
|
+
function normalizeTags(raw) {
|
|
241
|
+
const seen = new Set();
|
|
242
|
+
for (const entry of raw) {
|
|
243
|
+
const tag = entry.trim().toLowerCase().replace(/\s+/g, ' ').slice(0, LIMITS.tag);
|
|
244
|
+
if (tag === '' || seen.has(tag))
|
|
245
|
+
continue;
|
|
246
|
+
seen.add(tag);
|
|
247
|
+
if (seen.size >= LIMITS.tags)
|
|
248
|
+
break;
|
|
249
|
+
}
|
|
250
|
+
return [...seen];
|
|
251
|
+
}
|
|
252
|
+
/** Accept only a declared pastel key. */
|
|
253
|
+
function normalizeColor(raw) {
|
|
254
|
+
const value = (raw ?? '').trim();
|
|
255
|
+
return COLORS.find((color) => color === value) ?? 'yellow';
|
|
256
|
+
}
|
|
257
|
+
/** Accept only a declared width preset. */
|
|
258
|
+
function normalizeWidth(raw) {
|
|
259
|
+
const value = (raw ?? '').trim();
|
|
260
|
+
return WIDTHS.find((width) => width === value) ?? 'md';
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Rebuild the layout from the four flat keys.
|
|
264
|
+
*
|
|
265
|
+
* All four absent means "never placed" — the Client derives a preview position
|
|
266
|
+
* from the id. Any one present means the note HAS been placed, so the rest fall
|
|
267
|
+
* back to defaults rather than producing a half-valid placement.
|
|
268
|
+
*/
|
|
269
|
+
function readLayout(entries) {
|
|
270
|
+
const x = entries.has('x') ? asNumber(entries.get('x')) : undefined;
|
|
271
|
+
const y = entries.has('y') ? asNumber(entries.get('y')) : undefined;
|
|
272
|
+
const rotate = entries.has('rotate') ? asNumber(entries.get('rotate')) : undefined;
|
|
273
|
+
const width = entries.has('width') ? entries.get('width') : undefined;
|
|
274
|
+
if (x === undefined && y === undefined && rotate === undefined && width === undefined)
|
|
275
|
+
return null;
|
|
276
|
+
return {
|
|
277
|
+
x: x ?? 0,
|
|
278
|
+
y: y ?? 0,
|
|
279
|
+
rotate: rotate ?? 0,
|
|
280
|
+
width: normalizeWidth(width),
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Parse one note file.
|
|
285
|
+
*
|
|
286
|
+
* Never throws. A frontmatter block that cannot be read yields a note with
|
|
287
|
+
* defaults, `metaError` set, and the WHOLE FILE as the body, so nothing typed is
|
|
288
|
+
* lost and the wall still shows the card.
|
|
289
|
+
*/
|
|
290
|
+
export function parseNoteFile(text, options) {
|
|
291
|
+
const source = text.replace(/\r\n?/g, '\n');
|
|
292
|
+
const block = findFrontmatter(source);
|
|
293
|
+
const fallback = options.fallbackTime;
|
|
294
|
+
if (block === null) {
|
|
295
|
+
// Either genuinely no frontmatter, or an unterminated opening delimiter that
|
|
296
|
+
// is really body text. Both land here, and both keep the full text.
|
|
297
|
+
return {
|
|
298
|
+
hadFrontmatter: false,
|
|
299
|
+
note: {
|
|
300
|
+
id: options.fallbackId,
|
|
301
|
+
title: '',
|
|
302
|
+
body: source,
|
|
303
|
+
tags: [],
|
|
304
|
+
color: 'yellow',
|
|
305
|
+
pinned: false,
|
|
306
|
+
archived: false,
|
|
307
|
+
createdAt: fallback,
|
|
308
|
+
updatedAt: fallback,
|
|
309
|
+
layout: null,
|
|
310
|
+
},
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
const malformed = [];
|
|
314
|
+
const entries = parseFrontmatter(block.raw, malformed);
|
|
315
|
+
const id = unquote(entries.get('id') ?? '').trim();
|
|
316
|
+
const title = unquote(entries.get('title') ?? '').slice(0, LIMITS.title);
|
|
317
|
+
const extra = {};
|
|
318
|
+
for (const [key, value] of entries) {
|
|
319
|
+
if (!KNOWN_KEYS.has(key))
|
|
320
|
+
extra[key] = value;
|
|
321
|
+
}
|
|
322
|
+
const note = {
|
|
323
|
+
// An explicit id wins; the filename is the fallback so a hand-created file
|
|
324
|
+
// with no frontmatter still becomes a working note rather than a duplicate.
|
|
325
|
+
id: id === '' ? options.fallbackId : id,
|
|
326
|
+
title,
|
|
327
|
+
// The body is exactly what follows the delimiter, minus the one newline that
|
|
328
|
+
// separates the closing delimiter line from it. Nothing else is trimmed: the
|
|
329
|
+
// height estimate and the round-trip both depend on byte fidelity, so the
|
|
330
|
+
// separator is removed by exactly one newline and never by a `trim()`.
|
|
331
|
+
body: stripLeadingBreak(block.body),
|
|
332
|
+
tags: normalizeTags(parseList(entries.get('tags') ?? '')),
|
|
333
|
+
color: normalizeColor(entries.get('color')),
|
|
334
|
+
pinned: asBoolean(entries.get('pinned')) === true,
|
|
335
|
+
archived: asBoolean(entries.get('archived')) === true,
|
|
336
|
+
createdAt: normalizeTime(unquote(entries.get('created') ?? ''), fallback),
|
|
337
|
+
updatedAt: normalizeTime(unquote(entries.get('updated') ?? ''), fallback),
|
|
338
|
+
layout: readLayout(entries),
|
|
339
|
+
};
|
|
340
|
+
const parsed = { note, hadFrontmatter: true };
|
|
341
|
+
const schema = asNumber(entries.get('schema'));
|
|
342
|
+
if (schema !== undefined && schema > FRONTMATTER_SCHEMA) {
|
|
343
|
+
// A newer build wrote this file. Parse what is understood, keep the rest, and
|
|
344
|
+
// say so — never rewrite over a format this build does not fully know.
|
|
345
|
+
note.metaError = `newer-schema:${String(schema)}`;
|
|
346
|
+
}
|
|
347
|
+
else if (malformed.length > 0) {
|
|
348
|
+
// The block parsed only partially: every value it did yield is kept, but the
|
|
349
|
+
// wall flags the card so a mangled frontmatter is visible instead of silently
|
|
350
|
+
// dropping attributes the user expected.
|
|
351
|
+
note.metaError = 'malformed-frontmatter';
|
|
352
|
+
}
|
|
353
|
+
if (Object.keys(extra).length > 0)
|
|
354
|
+
parsed.note.extra = extra;
|
|
355
|
+
return parsed;
|
|
356
|
+
}
|
|
357
|
+
/** Quote and escape a scalar so the parser reads back exactly this string. */
|
|
358
|
+
function quote(value) {
|
|
359
|
+
// A JSON string is a valid YAML double-quoted scalar, and its escapes are the
|
|
360
|
+
// ones `unquote` reverses. Cheap, exact, and it survives any punctuation.
|
|
361
|
+
return JSON.stringify(value);
|
|
362
|
+
}
|
|
363
|
+
/** Serialize a scalar plain when it is unambiguous, quoted otherwise. */
|
|
364
|
+
function scalar(value) {
|
|
365
|
+
if (value !== '' && !/^[\s"'[\]{#&*!|>%@`,:?\-]/.test(value) && !/\s$/.test(value) && !/^(true|false|null|yes|no|on|off|~)$/i.test(value) && !/^[+-]?\d/.test(value)) {
|
|
366
|
+
return value;
|
|
367
|
+
}
|
|
368
|
+
return quote(value);
|
|
369
|
+
}
|
|
370
|
+
/** Serialize the layout's four flat keys; a `null` layout writes none of them. */
|
|
371
|
+
function layoutLines(layout) {
|
|
372
|
+
if (layout === null)
|
|
373
|
+
return [];
|
|
374
|
+
return [
|
|
375
|
+
`width: ${layout.width}`,
|
|
376
|
+
`x: ${String(layout.x)}`,
|
|
377
|
+
`y: ${String(layout.y)}`,
|
|
378
|
+
`rotate: ${String(layout.rotate)}`,
|
|
379
|
+
];
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Serialize one note back to file text.
|
|
383
|
+
*
|
|
384
|
+
* The output is stable: the same note always produces the same bytes, so a
|
|
385
|
+
* no-op save cannot churn the file's mtime and trigger a spurious refresh.
|
|
386
|
+
*/
|
|
387
|
+
export function serializeNoteFile(note) {
|
|
388
|
+
const lines = [DELIMITER];
|
|
389
|
+
lines.push(`id: ${scalar(note.id)}`);
|
|
390
|
+
lines.push(`schema: ${String(FRONTMATTER_SCHEMA)}`);
|
|
391
|
+
if (note.title !== '')
|
|
392
|
+
lines.push(`title: ${quote(note.title)}`);
|
|
393
|
+
lines.push(`color: ${note.color}`);
|
|
394
|
+
if (note.tags.length > 0)
|
|
395
|
+
lines.push(`tags: [${note.tags.map((tag) => quote(tag)).join(', ')}]`);
|
|
396
|
+
if (note.pinned)
|
|
397
|
+
lines.push('pinned: true');
|
|
398
|
+
if (note.archived)
|
|
399
|
+
lines.push('archived: true');
|
|
400
|
+
lines.push(...layoutLines(note.layout));
|
|
401
|
+
lines.push(`created: ${quote(note.createdAt)}`);
|
|
402
|
+
lines.push(`updated: ${quote(note.updatedAt)}`);
|
|
403
|
+
if (note.extra !== undefined) {
|
|
404
|
+
for (const [key, value] of Object.entries(note.extra)) {
|
|
405
|
+
// Never let an extra key shadow a managed one.
|
|
406
|
+
if (KNOWN_KEYS.has(key))
|
|
407
|
+
continue;
|
|
408
|
+
lines.push(`${key}: ${scalar(value)}`);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
lines.push(DELIMITER);
|
|
412
|
+
// One blank line between the delimiter and the body so the body never begins
|
|
413
|
+
// with the delimiter itself, which `findFrontmatter` would otherwise treat as
|
|
414
|
+
// the closing line of the frontmatter.
|
|
415
|
+
return `${lines.join('\n')}\n\n${note.body}`;
|
|
416
|
+
}
|
|
417
|
+
/** True when a file name is a plausible note id, so a path cannot escape. */
|
|
418
|
+
export function isNoteId(value) {
|
|
419
|
+
return /^[A-Za-z0-9_-]{1,128}$/.test(value);
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* Remove the single newline that separates the closing delimiter from the body.
|
|
423
|
+
*
|
|
424
|
+
* Exactly one, and never a `trim()`: the body must round-trip byte for byte, and
|
|
425
|
+
* a body that genuinely starts with a blank line has to keep it.
|
|
426
|
+
*/
|
|
427
|
+
function stripLeadingBreak(body) {
|
|
428
|
+
return body.startsWith('\n') ? body.slice(1) : body;
|
|
429
|
+
}
|
|
430
|
+
/** The `.md` basename for a note id. */
|
|
431
|
+
export function fileNameOf(id) {
|
|
432
|
+
return `${id}.md`;
|
|
433
|
+
}
|
|
434
|
+
/** Recover a note id from a file's basename; `null` when it is not one of ours. */
|
|
435
|
+
export function idOfFile(name) {
|
|
436
|
+
if (!name.endsWith('.md'))
|
|
437
|
+
return null;
|
|
438
|
+
const id = name.slice(0, -3);
|
|
439
|
+
return isNoteId(id) ? id : null;
|
|
440
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -14,10 +14,15 @@ import { StickyWallStore } from './store.js';
|
|
|
14
14
|
export { Config, StickyWallStore };
|
|
15
15
|
export declare const name = "skillre-sticky-wall";
|
|
16
16
|
/**
|
|
17
|
-
* Required services.
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
17
|
+
* Required services.
|
|
18
|
+
*
|
|
19
|
+
* `connection` provides the authenticated Client route and is a hard dependency:
|
|
20
|
+
* without it the wall could never be reached, so mounting should fail loudly.
|
|
21
|
+
*
|
|
22
|
+
* `storageDomain` is deliberately NOT listed. Notes live on disk now, and the
|
|
23
|
+
* domain is consulted only when a 0.7.x install still needs migrating, so it is
|
|
24
|
+
* read through `ctx.get()` and its absence is a normal, supported state (a fresh
|
|
25
|
+
* install has nothing to migrate).
|
|
21
26
|
*/
|
|
22
27
|
export declare const inject: string[];
|
|
23
28
|
export declare function apply(ctx: Context, rawConfig?: Partial<WallConfig>): void;
|