@crossworks/share-ui 0.239.37 → 0.239.45
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/package.json +12 -12
- package/src/note-presenter.test.ts +54 -0
- package/src/note-presenter.tsx +75 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crossworks/share-ui",
|
|
3
|
-
"version": "0.239.
|
|
3
|
+
"version": "0.239.45",
|
|
4
4
|
"description": "The server-rendered share surface — the /s/<token> presenters, view-payload contract, mini-app sandbox, and the few primitives they need. Lives in MANTLE (the server renders these), published for jackdaw to consume; may depend only on @mantle/{client-types,content-core} (the jackdaw-repo-split boundary).",
|
|
5
5
|
"exports": {
|
|
6
6
|
"./app-presenter": "./src/app-presenter.tsx",
|
|
@@ -38,18 +38,18 @@
|
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@dicebear/core": "^10.7.0",
|
|
40
40
|
"@dicebear/styles": "^10.6.0",
|
|
41
|
-
"@firecms/neat": "1.0
|
|
42
|
-
"@mantle/client-types": "npm:@crossworks/client-types@0.239.
|
|
43
|
-
"@mantle/content-core": "npm:@crossworks/content-core@0.239.
|
|
44
|
-
"@radix-ui/react-label": "^2.1.
|
|
45
|
-
"@radix-ui/react-slot": "^1.
|
|
41
|
+
"@firecms/neat": "1.1.0",
|
|
42
|
+
"@mantle/client-types": "npm:@crossworks/client-types@0.239.45",
|
|
43
|
+
"@mantle/content-core": "npm:@crossworks/content-core@0.239.45",
|
|
44
|
+
"@radix-ui/react-label": "^2.1.16",
|
|
45
|
+
"@radix-ui/react-slot": "^1.4.0",
|
|
46
46
|
"class-variance-authority": "^0.7.1",
|
|
47
47
|
"clsx": "^2.1.1",
|
|
48
|
-
"katex": "^0.
|
|
49
|
-
"lucide-react": "^1.
|
|
48
|
+
"katex": "^0.19.0",
|
|
49
|
+
"lucide-react": "^1.52.0",
|
|
50
50
|
"react-markdown": "^10.1.0",
|
|
51
51
|
"remark-gfm": "^4.0.1",
|
|
52
|
-
"tailwind-merge": "^3.
|
|
52
|
+
"tailwind-merge": "^3.7.0"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
55
|
"react": "^19.2.7",
|
|
@@ -57,9 +57,9 @@
|
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
59
|
"@types/katex": "^0.16.8",
|
|
60
|
-
"@types/node": "^26.4
|
|
61
|
-
"@types/react": "^19.
|
|
62
|
-
"@types/react-dom": "^19.
|
|
60
|
+
"@types/node": "^26.6.4",
|
|
61
|
+
"@types/react": "^19.3.0",
|
|
62
|
+
"@types/react-dom": "^19.3.0"
|
|
63
63
|
},
|
|
64
64
|
"license": "SEE LICENSE IN LICENSE.md",
|
|
65
65
|
"repository": {
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { createElement } from 'react';
|
|
2
|
+
import { renderToStaticMarkup } from 'react-dom/server';
|
|
3
|
+
import { describe, expect, it } from 'vitest';
|
|
4
|
+
import { NotePresenter } from './note-presenter';
|
|
5
|
+
|
|
6
|
+
const FILE = '11111111-1111-4111-8111-111111111111';
|
|
7
|
+
const DRAW = '22222222-2222-4222-8222-222222222222';
|
|
8
|
+
const content = [
|
|
9
|
+
``,
|
|
10
|
+
'',
|
|
11
|
+
``,
|
|
12
|
+
'',
|
|
13
|
+
`Read [spec.pdf](media:${FILE}) and [the site](https://example.com).`,
|
|
14
|
+
'',
|
|
15
|
+
') [bad link](javascript:alert(1))',
|
|
16
|
+
].join('\n');
|
|
17
|
+
|
|
18
|
+
const render = (props: Record<string, unknown> = {}) =>
|
|
19
|
+
renderToStaticMarkup(
|
|
20
|
+
createElement(NotePresenter, { view: { title: 'n', content }, chrome: 'embedded', ...props }),
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
describe('NotePresenter: media: and draw: references', () => {
|
|
24
|
+
const html = render({
|
|
25
|
+
assetUrl: (id: string) => `/s/tok/a/${id}`,
|
|
26
|
+
drawUrl: (id: string) => `/s/tok/draw/${id}`,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('resolves a picture and a drawing through the routes it is given', () => {
|
|
30
|
+
expect(html).toContain(`<img src="/s/tok/a/${FILE}" alt="photo"/>`);
|
|
31
|
+
expect(html).toContain(`<img src="/s/tok/draw/${DRAW}" alt="sketch"/>`);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('resolves a media: file link and leaves an ordinary link alone', () => {
|
|
35
|
+
expect(html).toContain(
|
|
36
|
+
`<a href="/s/tok/a/${FILE}" target="_blank" rel="noopener noreferrer">spec.pdf</a>`,
|
|
37
|
+
);
|
|
38
|
+
expect(html).toContain('<a href="https://example.com">the site</a>');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('keeps the default safety rule for any other scheme', () => {
|
|
42
|
+
expect(html).not.toContain('javascript:');
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('without routes, draws alt text and plain text and asks for nothing', () => {
|
|
46
|
+
const bare = render();
|
|
47
|
+
expect(bare).not.toContain('media:');
|
|
48
|
+
expect(bare).not.toContain('draw:');
|
|
49
|
+
expect(bare).toContain('[photo]');
|
|
50
|
+
expect(bare).toContain('[sketch]');
|
|
51
|
+
expect(bare).toContain('<span>spec.pdf</span>');
|
|
52
|
+
expect(bare).not.toMatch(/<img[^>]*src="[^"]*(?:11111111|22222222)/);
|
|
53
|
+
});
|
|
54
|
+
});
|
package/src/note-presenter.tsx
CHANGED
|
@@ -1,29 +1,102 @@
|
|
|
1
|
-
import ReactMarkdown from 'react-markdown';
|
|
1
|
+
import ReactMarkdown, { defaultUrlTransform, type Components } from 'react-markdown';
|
|
2
2
|
import remarkGfm from 'remark-gfm';
|
|
3
|
+
import { drawNodeId, mediaFileId } from '@mantle/content-core/markdown-refs';
|
|
3
4
|
import { isEmbedded, type PresenterChrome } from './lib/presenter-chrome';
|
|
4
5
|
|
|
6
|
+
/** Is `href` one of the app's reference schemes a note resolves itself
|
|
7
|
+
* (`media:` a file, `draw:` a drawing)? */
|
|
8
|
+
function isNoteRef(href: string): boolean {
|
|
9
|
+
const s = href.trim();
|
|
10
|
+
return mediaFileId(s) !== null || drawNodeId(s) !== null;
|
|
11
|
+
}
|
|
12
|
+
|
|
5
13
|
/**
|
|
6
14
|
* Public note render — markdown (GFM) in a prose column.
|
|
7
15
|
*
|
|
8
16
|
* Embedded keeps the measure. A note is prose, and prose wants a line length
|
|
9
17
|
* whoever is reading it; what it drops is the hero title the shell already
|
|
10
18
|
* draws, and the standalone page's tall top padding. See `PresenterChrome`.
|
|
19
|
+
*
|
|
20
|
+
* A note's own pictures and file links are written in the app's reference
|
|
21
|
+
* schemes: ``, `[spec.pdf](media:<file-id>)`,
|
|
22
|
+
* ``. `assetUrl` / `drawUrl` name the surface's byte
|
|
23
|
+
* route for each (the public share passes its token routes, as it does for
|
|
24
|
+
* the page presenter); the route decides what it serves. Without one, a
|
|
25
|
+
* picture draws its alt text and a link its text, so nothing is asked for.
|
|
26
|
+
* Any other href keeps ReactMarkdown's own safety rule.
|
|
11
27
|
*/
|
|
12
28
|
export function NotePresenter({
|
|
13
29
|
view,
|
|
14
30
|
chrome,
|
|
31
|
+
assetUrl,
|
|
32
|
+
drawUrl,
|
|
15
33
|
}: {
|
|
16
34
|
view: { title: string; content: string };
|
|
17
35
|
chrome?: PresenterChrome;
|
|
36
|
+
/** A `media:` file's bytes on this surface. */
|
|
37
|
+
assetUrl?: (fileId: string) => string;
|
|
38
|
+
/** A `draw:` drawing's snapshot on this surface. */
|
|
39
|
+
drawUrl?: (drawId: string) => string;
|
|
18
40
|
}) {
|
|
19
41
|
const embedded = isEmbedded(chrome);
|
|
42
|
+
/** The URL a reference resolves to, or null when this surface has no
|
|
43
|
+
* route for it. */
|
|
44
|
+
const refUrl = (href: string): string | null => {
|
|
45
|
+
const s = href.trim();
|
|
46
|
+
const file = mediaFileId(s);
|
|
47
|
+
if (file) return assetUrl ? assetUrl(file) : null;
|
|
48
|
+
const draw = drawNodeId(s);
|
|
49
|
+
if (draw) return drawUrl ? drawUrl(draw) : null;
|
|
50
|
+
return null;
|
|
51
|
+
};
|
|
52
|
+
const components: Components = {
|
|
53
|
+
img: ({ node: _node, src, alt, ...rest }) => {
|
|
54
|
+
if (typeof src === 'string' && isNoteRef(src)) {
|
|
55
|
+
const url = refUrl(src);
|
|
56
|
+
return url ? (
|
|
57
|
+
// eslint-disable-next-line @next/next/no-img-element -- token-routed share bytes; next/image cannot optimise them
|
|
58
|
+
<img src={url} alt={alt ?? ''} />
|
|
59
|
+
) : (
|
|
60
|
+
<span className="text-muted-foreground">[{alt?.trim() || 'image'}]</span>
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
// eslint-disable-next-line @next/next/no-img-element -- see above
|
|
64
|
+
return <img src={src} alt={alt ?? ''} {...rest} />;
|
|
65
|
+
},
|
|
66
|
+
a: ({ node: _node, href, children, ...rest }) => {
|
|
67
|
+
if (typeof href === 'string' && isNoteRef(href)) {
|
|
68
|
+
const url = refUrl(href);
|
|
69
|
+
return url ? (
|
|
70
|
+
<a href={url} target="_blank" rel="noopener noreferrer">
|
|
71
|
+
{children}
|
|
72
|
+
</a>
|
|
73
|
+
) : (
|
|
74
|
+
<span>{children}</span>
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
return (
|
|
78
|
+
<a href={href} {...rest}>
|
|
79
|
+
{children}
|
|
80
|
+
</a>
|
|
81
|
+
);
|
|
82
|
+
},
|
|
83
|
+
};
|
|
20
84
|
return (
|
|
21
85
|
<article className={embedded ? 'max-w-3xl px-6 py-6' : 'mx-auto max-w-3xl px-6 py-12 md:py-16'}>
|
|
22
86
|
{!embedded && (
|
|
23
87
|
<h1 className="mb-8 text-3xl font-bold tracking-tight text-balance">{view.title}</h1>
|
|
24
88
|
)}
|
|
25
89
|
<div className="prose dark:prose-invert max-w-none prose-accent prose-document">
|
|
26
|
-
<ReactMarkdown
|
|
90
|
+
<ReactMarkdown
|
|
91
|
+
remarkPlugins={[remarkGfm]}
|
|
92
|
+
components={components}
|
|
93
|
+
// A reference reaches the components above untouched, so it
|
|
94
|
+
// resolves there; ReactMarkdown's default would turn `media:` into
|
|
95
|
+
// an empty string first. Everything else keeps the default rule.
|
|
96
|
+
urlTransform={(url) => (isNoteRef(url) ? url : defaultUrlTransform(url))}
|
|
97
|
+
>
|
|
98
|
+
{view.content}
|
|
99
|
+
</ReactMarkdown>
|
|
27
100
|
</div>
|
|
28
101
|
</article>
|
|
29
102
|
);
|