@lemonriver/core 0.5.0 → 0.7.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.
Files changed (83) hide show
  1. package/dist/{build-CS1goZxt.js → build-CKwrex8y.js} +1 -1
  2. package/dist/cli/bin.js +3 -3
  3. package/dist/{config-BpbcCbUv.js → config-B1_gk0sy.js} +2983 -266
  4. package/dist/{dev-BpYnfgLL.js → dev-DZ2Yzl2p.js} +1 -1
  5. package/dist/{preview-HSzPPkSs.js → preview-hMDcuHfP.js} +1 -1
  6. package/dist/vite/index.js +1 -1
  7. package/package.json +1 -1
  8. package/src/app/lemonriver/board/blocks/classify.test.ts +5 -0
  9. package/src/app/lemonriver/board/blocks/classify.ts +29 -2
  10. package/src/app/lemonriver/board/blocks/snapshot.ts +34 -4
  11. package/src/app/lemonriver/board/board-page.tsx +15 -0
  12. package/src/app/lemonriver/board/model/agent-tasks.ts +33 -1
  13. package/src/app/lemonriver/board/model/self-check.test.ts +329 -0
  14. package/src/app/lemonriver/board/model/self-check.ts +489 -0
  15. package/src/app/lemonriver/board/page-node.tsx +23 -1
  16. package/src/app/lemonriver/board/self-check-jump.ts +23 -0
  17. package/src/app/lemonriver/board/self-check-panel.tsx +272 -0
  18. package/src/app/lemonriver/board/self-check-runner.tsx +150 -0
  19. package/src/app/lemonriver/board/use-self-check.ts +117 -0
  20. package/src/app/lemonriver/chrome/app-chrome.tsx +12 -1
  21. package/src/app/lemonriver/chrome/filter-pill.tsx +30 -0
  22. package/src/app/lemonriver/chrome/sidebar-extras.tsx +12 -1
  23. package/src/app/lemonriver/features.test.ts +10 -0
  24. package/src/app/lemonriver/features.ts +3 -1
  25. package/src/app/lemonriver/feedback/pending-toast.ts +7 -2
  26. package/src/app/lemonriver/home/deck-card.tsx +20 -1
  27. package/src/app/lemonriver/home/home-page.tsx +107 -7
  28. package/src/app/lemonriver/home/use-deck-times.ts +4 -1
  29. package/src/app/lemonriver/outline/build-team.tsx +42 -0
  30. package/src/app/lemonriver/outline/describe-page.tsx +128 -27
  31. package/src/app/lemonriver/outline/draft.ts +38 -0
  32. package/src/app/lemonriver/outline/model.test.ts +79 -0
  33. package/src/app/lemonriver/outline/model.ts +80 -7
  34. package/src/app/lemonriver/outline/outline-page.tsx +56 -6
  35. package/src/app/lemonriver/outline/pick-style.tsx +282 -0
  36. package/src/app/lemonriver/routes.test.tsx +17 -5
  37. package/src/app/lemonriver/routes.tsx +28 -0
  38. package/src/app/lemonriver/share/capture.ts +76 -0
  39. package/src/app/lemonriver/share/client.ts +162 -0
  40. package/src/app/lemonriver/share/codes.test.ts +49 -0
  41. package/src/app/lemonriver/share/codes.ts +30 -0
  42. package/src/app/lemonriver/share/compare.test.ts +199 -0
  43. package/src/app/lemonriver/share/compare.ts +367 -0
  44. package/src/app/lemonriver/share/drop.ts +84 -0
  45. package/src/app/lemonriver/share/folder.test.ts +42 -0
  46. package/src/app/lemonriver/share/folder.ts +29 -0
  47. package/src/app/lemonriver/share/freeze.ts +51 -0
  48. package/src/app/lemonriver/share/guide.test.ts +78 -0
  49. package/src/app/lemonriver/share/guide.ts +222 -0
  50. package/src/app/lemonriver/share/import-dialog.tsx +391 -0
  51. package/src/app/lemonriver/share/import-entry.tsx +106 -0
  52. package/src/app/lemonriver/share/model.ts +150 -0
  53. package/src/app/lemonriver/share/naming.test.ts +59 -0
  54. package/src/app/lemonriver/share/naming.ts +71 -0
  55. package/src/app/lemonriver/share/paths.test.ts +75 -0
  56. package/src/app/lemonriver/share/paths.ts +181 -0
  57. package/src/app/lemonriver/share/pdf.test.ts +79 -0
  58. package/src/app/lemonriver/share/pdf.ts +169 -0
  59. package/src/app/lemonriver/share/share-dialog.tsx +286 -0
  60. package/src/app/lemonriver/share/summary.ts +47 -0
  61. package/src/app/lemonriver/slide-extensions.tsx +69 -23
  62. package/src/app/lemonriver/templates/client.ts +60 -0
  63. package/src/app/lemonriver/templates/demo-page.tsx +79 -0
  64. package/src/app/lemonriver/templates/knobs.test.ts +62 -0
  65. package/src/app/lemonriver/templates/knobs.ts +62 -0
  66. package/src/app/lemonriver/templates/library-page.tsx +295 -0
  67. package/src/app/lemonriver/templates/model.test.ts +175 -0
  68. package/src/app/lemonriver/templates/model.ts +339 -0
  69. package/src/app/lemonriver/templates/preview-content.test.ts +117 -0
  70. package/src/app/lemonriver/templates/preview-content.ts +149 -0
  71. package/src/app/lemonriver/templates/recommend.test.ts +71 -0
  72. package/src/app/lemonriver/templates/recommend.ts +72 -0
  73. package/src/app/lemonriver/templates/use-library.ts +58 -0
  74. package/src/app/lemonriver/templates/virtual.d.ts +28 -0
  75. package/src/app/lemonriver/text/en.ts +124 -0
  76. package/src/app/lemonriver/text/es.ts +124 -0
  77. package/src/app/lemonriver/text/ja.ts +124 -0
  78. package/src/app/lemonriver/text/types.ts +151 -0
  79. package/src/app/lemonriver/text/zh-cn.ts +119 -0
  80. package/src/app/lemonriver/text/zh-tw.ts +119 -0
  81. package/src/app/lemonriver/theme/pages.test.ts +4 -0
  82. package/src/app/lemonriver/theme/pages.ts +6 -1
  83. package/src/app/lemonriver/update/prompt.ts +1 -1
@@ -0,0 +1,106 @@
1
+ import { Import } from 'lucide-react';
2
+ import { type DragEvent, type ReactNode, useRef, useState } from 'react';
3
+ import { useLrText } from '../text';
4
+ import { readDropped } from './drop';
5
+
6
+ function hasFiles(event: DragEvent): boolean {
7
+ return Array.from(event.dataTransfer?.types ?? []).includes('Files');
8
+ }
9
+
10
+ /** The home page's Import button: a zip picker wearing the same clothes as the toolbar. */
11
+ export function ImportButton({
12
+ onBytes,
13
+ disabled,
14
+ }: {
15
+ onBytes: (bytes: Uint8Array) => void;
16
+ disabled?: boolean;
17
+ }) {
18
+ const lr = useLrText();
19
+ const inputRef = useRef<HTMLInputElement | null>(null);
20
+ return (
21
+ <>
22
+ <button
23
+ type="button"
24
+ data-lr-share-import
25
+ disabled={disabled}
26
+ onClick={() => inputRef.current?.click()}
27
+ className="flex h-9 shrink-0 items-center gap-1.5 whitespace-nowrap rounded-full border border-border bg-card px-3 text-[14px] font-medium text-foreground outline-none transition-colors duration-150 hover:bg-muted focus-visible:ring-2 focus-visible:ring-ring/40 disabled:opacity-60"
28
+ >
29
+ <Import className="size-4 text-muted-foreground" aria-hidden />
30
+ {lr.share.importAction}
31
+ </button>
32
+ <input
33
+ ref={inputRef}
34
+ type="file"
35
+ accept=".zip,application/zip"
36
+ className="hidden"
37
+ onChange={(event) => {
38
+ const file = event.target.files?.[0];
39
+ event.target.value = '';
40
+ if (!file) return;
41
+ void file.arrayBuffer().then((buffer) => onBytes(new Uint8Array(buffer)));
42
+ }}
43
+ />
44
+ </>
45
+ );
46
+ }
47
+
48
+ /**
49
+ * The home page accepts a package anywhere: the zip itself, or the folder
50
+ * Safari left behind after unpacking it. Spread `handlers` on the page element
51
+ * and render `hint` inside it.
52
+ */
53
+ export function useShareDrop({
54
+ onBytes,
55
+ onUnknown,
56
+ }: {
57
+ onBytes: (bytes: Uint8Array) => void;
58
+ onUnknown: () => void;
59
+ }): { handlers: Record<string, unknown>; hint: ReactNode } {
60
+ const lr = useLrText();
61
+ const [active, setActive] = useState(false);
62
+ const depth = useRef(0);
63
+
64
+ const handlers = {
65
+ 'data-lr-share-drop': '',
66
+ onDragEnter: (event: DragEvent) => {
67
+ if (!hasFiles(event)) return;
68
+ event.preventDefault();
69
+ depth.current += 1;
70
+ setActive(true);
71
+ },
72
+ onDragOver: (event: DragEvent) => {
73
+ if (!hasFiles(event)) return;
74
+ event.preventDefault();
75
+ event.dataTransfer.dropEffect = 'copy';
76
+ },
77
+ onDragLeave: () => {
78
+ depth.current = Math.max(0, depth.current - 1);
79
+ if (depth.current === 0) setActive(false);
80
+ },
81
+ onDrop: (event: DragEvent) => {
82
+ if (!hasFiles(event)) return;
83
+ event.preventDefault();
84
+ depth.current = 0;
85
+ setActive(false);
86
+ const transfer = event.dataTransfer;
87
+ void readDropped(transfer).then((dropped) => {
88
+ if (dropped) onBytes(dropped.bytes);
89
+ else onUnknown();
90
+ });
91
+ },
92
+ };
93
+
94
+ const hint = active ? (
95
+ <div
96
+ data-lr-share-drop-hint
97
+ className="pointer-events-none fixed inset-0 z-50 flex items-center justify-center bg-foreground/20 backdrop-blur-[2px]"
98
+ >
99
+ <div className="rounded-[16px] border-[2px] border-dashed border-[var(--lr-focus)] bg-card px-10 py-8 text-[18px] font-semibold text-foreground shadow-overlay">
100
+ {lr.share.dropHint}
101
+ </div>
102
+ </div>
103
+ ) : null;
104
+
105
+ return { handlers, hint };
106
+ }
@@ -0,0 +1,150 @@
1
+ /** The parts of a share package both ends must agree on. */
2
+
3
+ export const SHARE_FORMAT = 'lemonriver';
4
+ export const SHARE_VERSION = 1;
5
+ export const SHARE_ROOT = 'LemonRiver-files';
6
+ export const SHARE_MANIFEST = 'manifest.json';
7
+ export const SHARE_APP_NAME = 'LemonRiver';
8
+
9
+ export type ShareKind = 'share' | 'backup';
10
+
11
+ export type ShareDeckEntry = {
12
+ slideId: string;
13
+ title: string;
14
+ pages: number | null;
15
+ shareId: string;
16
+ theme: string | null;
17
+ thumbs: number;
18
+ };
19
+
20
+ export type ShareManifest = {
21
+ format: typeof SHARE_FORMAT;
22
+ version: number;
23
+ kind: ShareKind;
24
+ app: { name: string; version: string };
25
+ createdAt: number;
26
+ decks: ShareDeckEntry[];
27
+ files: Record<string, string>;
28
+ };
29
+
30
+ export type ShareCode =
31
+ | 'not-a-package'
32
+ | 'bad-manifest'
33
+ | 'newer-version'
34
+ | 'bad-path'
35
+ | 'blocked-file'
36
+ | 'checksum'
37
+ | 'missing-file'
38
+ | 'extra-file'
39
+ | 'too-large'
40
+ | 'too-many-files';
41
+
42
+ export type ManifestRead =
43
+ | { ok: true; manifest: ShareManifest }
44
+ | { ok: false; code: ShareCode; detail: string; appVersion?: string };
45
+
46
+ const KIND: readonly ShareKind[] = ['share', 'backup'];
47
+
48
+ function isRecord(value: unknown): value is Record<string, unknown> {
49
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
50
+ }
51
+
52
+ function readDeck(raw: unknown): ShareDeckEntry | null {
53
+ if (!isRecord(raw)) return null;
54
+ const { slideId, title, shareId } = raw;
55
+ if (typeof slideId !== 'string' || slideId === '') return null;
56
+ if (typeof title !== 'string') return null;
57
+ if (typeof shareId !== 'string' || shareId === '') return null;
58
+ const pages = typeof raw.pages === 'number' && raw.pages >= 0 ? Math.round(raw.pages) : null;
59
+ const thumbs = typeof raw.thumbs === 'number' && raw.thumbs >= 0 ? Math.round(raw.thumbs) : 0;
60
+ return {
61
+ slideId,
62
+ title,
63
+ pages,
64
+ shareId,
65
+ theme: typeof raw.theme === 'string' && raw.theme !== '' ? raw.theme : null,
66
+ thumbs,
67
+ };
68
+ }
69
+
70
+ /**
71
+ * Reads a manifest without trusting any of it. A version above ours is its own
72
+ * answer, because the package may still be perfectly well formed.
73
+ */
74
+ export function sanitizeManifest(raw: unknown): ManifestRead {
75
+ if (!isRecord(raw))
76
+ return { ok: false, code: 'bad-manifest', detail: 'manifest is not an object' };
77
+ if (raw.format !== SHARE_FORMAT) {
78
+ return { ok: false, code: 'not-a-package', detail: 'this zip is not a LemonRiver package' };
79
+ }
80
+ const version = raw.version;
81
+ if (typeof version !== 'number' || !Number.isInteger(version) || version < 1) {
82
+ return { ok: false, code: 'bad-manifest', detail: 'manifest has no usable version' };
83
+ }
84
+ const app = isRecord(raw.app) ? raw.app : {};
85
+ const appVersion = typeof app.version === 'string' ? app.version : '';
86
+ if (version > SHARE_VERSION) {
87
+ return {
88
+ ok: false,
89
+ code: 'newer-version',
90
+ detail: `made by a newer LemonRiver (${appVersion || 'unknown version'})`,
91
+ appVersion,
92
+ };
93
+ }
94
+ const kind = raw.kind;
95
+ if (typeof kind !== 'string' || !KIND.includes(kind as ShareKind)) {
96
+ return { ok: false, code: 'bad-manifest', detail: 'manifest has no usable kind' };
97
+ }
98
+ if (!Array.isArray(raw.decks) || raw.decks.length === 0) {
99
+ return { ok: false, code: 'bad-manifest', detail: 'manifest lists no decks' };
100
+ }
101
+ const decks: ShareDeckEntry[] = [];
102
+ for (const entry of raw.decks) {
103
+ const deck = readDeck(entry);
104
+ if (!deck)
105
+ return { ok: false, code: 'bad-manifest', detail: 'manifest has a broken deck entry' };
106
+ decks.push(deck);
107
+ }
108
+ if (!isRecord(raw.files)) {
109
+ return { ok: false, code: 'bad-manifest', detail: 'manifest lists no files' };
110
+ }
111
+ const files: Record<string, string> = {};
112
+ for (const [file, digest] of Object.entries(raw.files)) {
113
+ if (typeof digest !== 'string' || digest === '') {
114
+ return { ok: false, code: 'bad-manifest', detail: `no checksum for ${file}` };
115
+ }
116
+ files[file] = digest;
117
+ }
118
+ const createdAt =
119
+ typeof raw.createdAt === 'number' && Number.isFinite(raw.createdAt) ? raw.createdAt : 0;
120
+ return {
121
+ ok: true,
122
+ manifest: {
123
+ format: SHARE_FORMAT,
124
+ version,
125
+ kind: kind as ShareKind,
126
+ app: { name: typeof app.name === 'string' ? app.name : SHARE_APP_NAME, version: appVersion },
127
+ createdAt,
128
+ decks,
129
+ files,
130
+ },
131
+ };
132
+ }
133
+
134
+ /** The keys a board record may not carry into a package or back out of one. */
135
+ export const BOARD_STRIP_KEYS = [
136
+ 'applyBackup',
137
+ 'applyColumns',
138
+ 'applyColumnsBackup',
139
+ 'applying',
140
+ 'lastApply',
141
+ ] as const;
142
+
143
+ export function stripBoardRecord(record: Record<string, unknown>): Record<string, unknown> {
144
+ const kept: Record<string, unknown> = {};
145
+ for (const [key, value] of Object.entries(record)) {
146
+ if ((BOARD_STRIP_KEYS as readonly string[]).includes(key)) continue;
147
+ kept[key] = value;
148
+ }
149
+ return kept;
150
+ }
@@ -0,0 +1,59 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import {
3
+ dayStamp,
4
+ freeImportSlideId,
5
+ isValidExtraName,
6
+ packageBaseName,
7
+ safeFileName,
8
+ uniqueFileName,
9
+ } from './naming.ts';
10
+
11
+ describe('package file names', () => {
12
+ it('strips what Finder and Explorer refuse', () => {
13
+ expect(safeFileName('Q3/经营:汇报*?"<>|', 'q3')).toBe('Q3经营汇报');
14
+ expect(safeFileName(' 前后有空格 ', 'q3')).toBe('前后有空格');
15
+ expect(safeFileName('a\u0007b', 'q3')).toBe('ab');
16
+ });
17
+
18
+ it('falls back to the slide id, then to a name', () => {
19
+ expect(safeFileName('///', 'q3-report')).toBe('q3-report');
20
+ expect(safeFileName('', '')).toBe('LemonRiver');
21
+ });
22
+
23
+ it('keeps the name to 80 characters', () => {
24
+ expect(safeFileName('长'.repeat(200), 'q3')).toHaveLength(80);
25
+ });
26
+
27
+ it('numbers a second package with the same name', () => {
28
+ expect(uniqueFileName('Q3', [])).toBe('Q3');
29
+ expect(uniqueFileName('Q3', ['q3'])).toBe('Q3 (2)');
30
+ expect(uniqueFileName('Q3', ['Q3', 'Q3 (2)'])).toBe('Q3 (3)');
31
+ });
32
+
33
+ it('names one deck after itself and several after the day', () => {
34
+ expect(packageBaseName([{ title: 'Q3 经营汇报', slideId: 'q3' }], 0)).toBe('Q3 经营汇报');
35
+ const many = [
36
+ { title: 'A', slideId: 'a' },
37
+ { title: 'B', slideId: 'b' },
38
+ ];
39
+ const now = new Date(2026, 10, 1, 12).getTime();
40
+ expect(packageBaseName(many, now)).toBe('LemonRiver 备份 2026-11-01');
41
+ expect(dayStamp(now)).toBe('2026-11-01');
42
+ });
43
+
44
+ it('gives an imported deck its own id', () => {
45
+ expect(freeImportSlideId('q3', [])).toBe('q3');
46
+ expect(freeImportSlideId('q3', ['Q3'])).toBe('q3-imported');
47
+ expect(freeImportSlideId('q3', ['q3', 'q3-imported'])).toBe('q3-imported-2');
48
+ });
49
+
50
+ it('only lets a PDF, an HTML page or text ride along', () => {
51
+ expect(isValidExtraName('Q3 经营汇报.pdf')).toBe(true);
52
+ expect(isValidExtraName('怎么打开.html')).toBe(true);
53
+ expect(isValidExtraName('README.txt')).toBe(true);
54
+ expect(isValidExtraName('setup.sh')).toBe(false);
55
+ expect(isValidExtraName('../x.pdf')).toBe(false);
56
+ expect(isValidExtraName('.hidden.pdf')).toBe(false);
57
+ expect(isValidExtraName('no-extension')).toBe(false);
58
+ });
59
+ });
@@ -0,0 +1,71 @@
1
+ const RESERVED = new Set(['/', '\\', ':', '*', '?', '"', '<', '>', '|']);
2
+ const MAX_BASE = 80;
3
+
4
+ function clean(raw: string): string {
5
+ let out = '';
6
+ for (const character of raw) {
7
+ const code = character.codePointAt(0) ?? 0;
8
+ if (code < 0x20 || code === 0x7f) continue;
9
+ if (RESERVED.has(character)) continue;
10
+ out += character;
11
+ }
12
+ return out.replace(/\s+/g, ' ').trim().slice(0, MAX_BASE).trim();
13
+ }
14
+
15
+ /** What the user can call a file without tripping over macOS, Windows or Finder. */
16
+ export function safeFileName(raw: string, fallback: string): string {
17
+ const name = clean(raw);
18
+ if (name !== '') return name;
19
+ const fallbackName = clean(fallback);
20
+ return fallbackName !== '' ? fallbackName : 'LemonRiver';
21
+ }
22
+
23
+ function two(n: number): string {
24
+ return String(n).padStart(2, '0');
25
+ }
26
+
27
+ export function dayStamp(now: number): string {
28
+ const date = new Date(now);
29
+ return `${date.getFullYear()}-${two(date.getMonth() + 1)}-${two(date.getDate())}`;
30
+ }
31
+
32
+ /** One deck is named after itself; a set of them is a backup with a date. */
33
+ export function packageBaseName(decks: { title: string; slideId: string }[], now: number): string {
34
+ if (decks.length === 1) {
35
+ const [deck] = decks;
36
+ return safeFileName(deck.title, deck.slideId);
37
+ }
38
+ return safeFileName(`LemonRiver 备份 ${dayStamp(now)}`, `LemonRiver-${dayStamp(now)}`);
39
+ }
40
+
41
+ /** Finder and Explorer both treat these as the same name. */
42
+ export function uniqueFileName(base: string, taken: Iterable<string>): string {
43
+ const used = new Set([...taken].map((name) => name.toLowerCase()));
44
+ if (!used.has(base.toLowerCase())) return base;
45
+ for (let n = 2; ; n++) {
46
+ const candidate = `${base} (${n})`;
47
+ if (!used.has(candidate.toLowerCase())) return candidate;
48
+ }
49
+ }
50
+
51
+ /** A deck that arrives beside one with the same id gets its own. */
52
+ export function freeImportSlideId(slideId: string, taken: Iterable<string>): string {
53
+ const used = new Set([...taken].map((id) => id.toLowerCase()));
54
+ if (!used.has(slideId.toLowerCase())) return slideId;
55
+ for (let n = 1; ; n++) {
56
+ const candidate = n === 1 ? `${slideId}-imported` : `${slideId}-imported-${n}`;
57
+ if (!used.has(candidate.toLowerCase())) return candidate;
58
+ }
59
+ }
60
+
61
+ const EXTRA_EXTENSIONS = new Set(['pdf', 'html', 'txt']);
62
+
63
+ /** The PDF and the how-to-open page ride at the top of the zip. */
64
+ export function isValidExtraName(name: string): boolean {
65
+ if (name === '' || name.startsWith('.') || name.includes('/') || name.includes('\\'))
66
+ return false;
67
+ if (name.includes('\0')) return false;
68
+ const dot = name.lastIndexOf('.');
69
+ if (dot <= 0) return false;
70
+ return EXTRA_EXTENSIONS.has(name.slice(dot + 1).toLowerCase());
71
+ }
@@ -0,0 +1,75 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { isJunkPath, isPackagePath, packagePathProblem, slideIdOfPath } from './paths.ts';
3
+
4
+ describe('package paths', () => {
5
+ it('accepts the folders a package is made of', () => {
6
+ expect(isPackagePath('manifest.json')).toBe(true);
7
+ expect(isPackagePath('slides/q3-report/index.tsx')).toBe(true);
8
+ expect(isPackagePath('slides/q3-report/kit/skeletons.tsx')).toBe(true);
9
+ expect(isPackagePath('slides/q3-report/assets/logo.png')).toBe(true);
10
+ expect(isPackagePath('board/q3-report.json')).toBe(true);
11
+ expect(isPackagePath('thumbs/q3-report/01.jpg')).toBe(true);
12
+ expect(isPackagePath('themes/aero.md')).toBe(true);
13
+ expect(isPackagePath('themes/aero.demo.tsx')).toBe(true);
14
+ expect(isPackagePath('slides/q3-report/kit/fonts/OFL.txt')).toBe(true);
15
+ expect(isPackagePath('slides/q3-report/kit/LICENSE')).toBe(true);
16
+ });
17
+
18
+ it('refuses paths that climb out or lie about where they are', () => {
19
+ expect(packagePathProblem('../x')?.code).toBe('bad-path');
20
+ expect(packagePathProblem('slides/../x.tsx')?.code).toBe('bad-path');
21
+ expect(packagePathProblem('slides/q3/kit/../index.tsx')?.code).toBe('bad-path');
22
+ expect(packagePathProblem('slides/q3/kit/../../logo.png')?.code).toBe('bad-path');
23
+ expect(packagePathProblem('slides/q3/./index.tsx')?.code).toBe('bad-path');
24
+ expect(packagePathProblem('/etc/passwd')?.code).toBe('bad-path');
25
+ expect(packagePathProblem('C:/x.tsx')?.code).toBe('bad-path');
26
+ expect(packagePathProblem('slides\\q3\\index.tsx')?.code).toBe('bad-path');
27
+ expect(packagePathProblem('slides//index.tsx')?.code).toBe('bad-path');
28
+ expect(packagePathProblem('slides/q3/.hidden.tsx')?.code).toBe('bad-path');
29
+ expect(packagePathProblem('slides/q3/index.tsx\0.png')?.code).toBe('bad-path');
30
+ });
31
+
32
+ it('refuses node_modules and files that would configure the other machine', () => {
33
+ expect(packagePathProblem('slides/q3/node_modules/x.js')?.code).toBe('blocked-file');
34
+ expect(packagePathProblem('slides/q3/package.json')?.code).toBe('blocked-file');
35
+ expect(packagePathProblem('slides/q3/package-lock.json')?.code).toBe('blocked-file');
36
+ expect(packagePathProblem('slides/q3/pnpm-lock.yaml')?.code).toBe('blocked-file');
37
+ expect(packagePathProblem('slides/q3/yarn.lock')?.code).toBe('blocked-file');
38
+ expect(packagePathProblem('slides/q3/bun.lockb')?.code).toBe('blocked-file');
39
+ expect(packagePathProblem('slides/q3/.npmrc')?.code).toBe('bad-path');
40
+ expect(packagePathProblem('slides/q3/vite.config.ts')?.code).toBe('blocked-file');
41
+ expect(packagePathProblem('slides/q3/lemonriver.config.ts')?.code).toBe('blocked-file');
42
+ expect(packagePathProblem('slides/q3/open-slide.config.ts')?.code).toBe('blocked-file');
43
+ });
44
+
45
+ it('refuses an extension a deck may not carry', () => {
46
+ expect(packagePathProblem('slides/q3/setup.sh')?.code).toBe('blocked-file');
47
+ expect(packagePathProblem('slides/q3/run.command')?.code).toBe('blocked-file');
48
+ expect(packagePathProblem('slides/q3/Evil.app')?.code).toBe('blocked-file');
49
+ expect(packagePathProblem('slides/q3/tool')?.code).toBe('blocked-file');
50
+ });
51
+
52
+ it('wants one of the shapes for every folder', () => {
53
+ expect(packagePathProblem('slides/q3')?.code).toBe('bad-path');
54
+ expect(packagePathProblem('board/q3')?.code).toBe('bad-path');
55
+ expect(packagePathProblem('board/a/b.json')?.code).toBe('bad-path');
56
+ expect(packagePathProblem('thumbs/q3/1.jpg')?.code).toBe('bad-path');
57
+ expect(packagePathProblem('themes/aero.css')?.code).toBe('bad-path');
58
+ expect(packagePathProblem('extras/Q3.pdf')?.code).toBe('bad-path');
59
+ expect(packagePathProblem('manifest.json/x')?.code).toBe('bad-path');
60
+ });
61
+
62
+ it('names the deck a file belongs to', () => {
63
+ expect(slideIdOfPath('slides/q3-report/index.tsx')).toBe('q3-report');
64
+ expect(slideIdOfPath('board/q3-report.json')).toBe('q3-report');
65
+ expect(slideIdOfPath('thumbs/q3-report/01.jpg')).toBe('q3-report');
66
+ expect(slideIdOfPath('manifest.json')).toBeNull();
67
+ expect(slideIdOfPath('themes/aero.md')).toBeNull();
68
+ });
69
+
70
+ it('ignores what a Mac leaves in a zip', () => {
71
+ expect(isJunkPath('__MACOSX/._index.tsx')).toBe(true);
72
+ expect(isJunkPath('LemonRiver-files/.DS_Store')).toBe(true);
73
+ expect(isJunkPath('LemonRiver-files/slides/q3/index.tsx')).toBe(false);
74
+ });
75
+ });
@@ -0,0 +1,181 @@
1
+ import type { ShareCode } from './model.ts';
2
+
3
+ /** Slide ids are the same shape the editor uses for a deck folder. */
4
+ export const SHARE_SLIDE_ID_RE = /^[a-z0-9_-]+$/i;
5
+
6
+ const SLIDE_EXTENSIONS = new Set([
7
+ 'tsx',
8
+ 'ts',
9
+ 'jsx',
10
+ 'js',
11
+ 'mjs',
12
+ 'json',
13
+ 'css',
14
+ 'md',
15
+ 'txt',
16
+ 'csv',
17
+ 'png',
18
+ 'jpg',
19
+ 'jpeg',
20
+ 'gif',
21
+ 'webp',
22
+ 'svg',
23
+ 'avif',
24
+ 'ico',
25
+ 'bmp',
26
+ 'mp4',
27
+ 'webm',
28
+ 'mov',
29
+ 'mp3',
30
+ 'wav',
31
+ 'm4a',
32
+ 'ogg',
33
+ 'woff',
34
+ 'woff2',
35
+ 'ttf',
36
+ 'otf',
37
+ 'pdf',
38
+ 'xlsx',
39
+ 'xls',
40
+ 'docx',
41
+ 'doc',
42
+ 'pptx',
43
+ 'key',
44
+ 'numbers',
45
+ 'pages',
46
+ ]);
47
+
48
+ const BLOCKED_SLIDE_FILES =
49
+ /^(?:package\.json|package-lock\.json|pnpm-lock\.yaml|yarn\.lock|bun\.lockb|\.npmrc|vite\.config\..+|lemonriver\.config\..+|open-slide\.config\..+)$/i;
50
+
51
+ export type PathProblem = { code: ShareCode; detail: string };
52
+
53
+ function extensionOf(name: string): string {
54
+ const dot = name.lastIndexOf('.');
55
+ return dot > 0 ? name.slice(dot + 1).toLowerCase() : '';
56
+ }
57
+
58
+ /** `LICENSE`, `OFL.txt` and friends: no extension we would accept otherwise. */
59
+ function looksLikeLicence(name: string): boolean {
60
+ return (
61
+ /^(?:LICENSE|OFL)/i.test(name) ||
62
+ (name === name.toUpperCase() && /^[A-Z0-9][A-Z0-9._-]*$/.test(name))
63
+ );
64
+ }
65
+
66
+ function structuralProblem(rel: string): PathProblem | null {
67
+ if (rel === '') return { code: 'bad-path', detail: 'empty path' };
68
+ if (rel.includes('\0')) return { code: 'bad-path', detail: 'path holds a null byte' };
69
+ if (rel.includes('\\')) return { code: 'bad-path', detail: 'backslash in path' };
70
+ if (rel.startsWith('/') || /^[a-z]:/i.test(rel)) {
71
+ return { code: 'bad-path', detail: 'absolute path' };
72
+ }
73
+ for (const segment of rel.split('/')) {
74
+ if (segment === '') return { code: 'bad-path', detail: 'empty path segment' };
75
+ if (segment === '.' || segment === '..') return { code: 'bad-path', detail: 'path climbs out' };
76
+ // `.` and `..` belong to the rule above; this one is about hidden files.
77
+ if (segment.startsWith('.') && segment !== '..') {
78
+ return { code: 'bad-path', detail: `hidden segment "${segment}"` };
79
+ }
80
+ if (segment.toLowerCase() === 'node_modules') {
81
+ return { code: 'blocked-file', detail: 'node_modules is not shipped' };
82
+ }
83
+ }
84
+ return null;
85
+ }
86
+
87
+ function slideFileProblem(rest: string): PathProblem | null {
88
+ const name = rest.slice(rest.lastIndexOf('/') + 1);
89
+ if (BLOCKED_SLIDE_FILES.test(name)) {
90
+ return { code: 'blocked-file', detail: `${name} would configure the other machine` };
91
+ }
92
+ if (looksLikeLicence(name)) return null;
93
+ const extension = extensionOf(name);
94
+ if (extension === '' || !SLIDE_EXTENSIONS.has(extension)) {
95
+ return { code: 'blocked-file', detail: `"${name}" is not a file a deck may carry` };
96
+ }
97
+ return null;
98
+ }
99
+
100
+ /**
101
+ * One rule for both ends: what may sit under `LemonRiver-files/`. Anything the
102
+ * packer would refuse is also refused on the way in.
103
+ */
104
+ export function packagePathProblem(rel: string): PathProblem | null {
105
+ const structural = structuralProblem(rel);
106
+ if (structural) return structural;
107
+
108
+ const segments = rel.split('/');
109
+ const [head, ...rest] = segments;
110
+
111
+ if (head === 'manifest.json') {
112
+ return segments.length === 1 ? null : { code: 'bad-path', detail: 'manifest.json is a file' };
113
+ }
114
+
115
+ if (head === 'board') {
116
+ if (rest.length !== 1 || !rest[0].endsWith('.json')) {
117
+ return { code: 'bad-path', detail: 'board holds <slideId>.json only' };
118
+ }
119
+ const id = rest[0].slice(0, -'.json'.length);
120
+ if (!SHARE_SLIDE_ID_RE.test(id)) return { code: 'bad-path', detail: `bad slide id "${id}"` };
121
+ return null;
122
+ }
123
+
124
+ if (head === 'themes') {
125
+ if (rest.length !== 1) return { code: 'bad-path', detail: 'themes holds files only' };
126
+ const name = rest[0];
127
+ if (name.endsWith('.demo.tsx')) {
128
+ const id = name.slice(0, -'.demo.tsx'.length);
129
+ if (!SHARE_SLIDE_ID_RE.test(id)) return { code: 'bad-path', detail: `bad theme id "${id}"` };
130
+ return null;
131
+ }
132
+ if (name.endsWith('.md')) {
133
+ const id = name.slice(0, -'.md'.length);
134
+ if (!SHARE_SLIDE_ID_RE.test(id)) return { code: 'bad-path', detail: `bad theme id "${id}"` };
135
+ return null;
136
+ }
137
+ return { code: 'bad-path', detail: 'a theme is <id>.md or <id>.demo.tsx' };
138
+ }
139
+
140
+ if (head === 'thumbs') {
141
+ if (rest.length !== 2 || !/^\d{2,}\.jpg$/.test(rest[1])) {
142
+ return { code: 'bad-path', detail: 'a thumbnail is <slideId>/NN.jpg' };
143
+ }
144
+ if (!SHARE_SLIDE_ID_RE.test(rest[0])) {
145
+ return { code: 'bad-path', detail: `bad slide id "${rest[0]}"` };
146
+ }
147
+ return null;
148
+ }
149
+
150
+ if (head === 'slides') {
151
+ if (rest.length < 2) return { code: 'bad-path', detail: 'a slide holds at least one file' };
152
+ if (!SHARE_SLIDE_ID_RE.test(rest[0])) {
153
+ return { code: 'bad-path', detail: `bad slide id "${rest[0]}"` };
154
+ }
155
+ return slideFileProblem(rest.slice(1).join('/'));
156
+ }
157
+
158
+ return { code: 'bad-path', detail: `"${head}" is not part of a package` };
159
+ }
160
+
161
+ export function isPackagePath(rel: string): boolean {
162
+ return packagePathProblem(rel) === null;
163
+ }
164
+
165
+ /** The deck a package path belongs to, or null for manifest and themes. */
166
+ export function slideIdOfPath(rel: string): string | null {
167
+ const segments = rel.split('/');
168
+ if (segments[0] === 'slides' || segments[0] === 'board' || segments[0] === 'thumbs') {
169
+ if (segments[0] === 'board') {
170
+ return segments[1]?.endsWith('.json') ? segments[1].slice(0, -'.json'.length) : null;
171
+ }
172
+ return segments[1] ?? null;
173
+ }
174
+ return null;
175
+ }
176
+
177
+ /** Ignored on the way in: what a Mac leaves behind when it zips a folder. */
178
+ export function isJunkPath(rel: string): boolean {
179
+ if (rel.startsWith('__MACOSX/') || rel === '__MACOSX') return true;
180
+ return rel.split('/').some((segment) => segment === '.DS_Store');
181
+ }