@backmatter/next 0.0.66 → 0.0.67
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/dist/config.cjs +20 -0
- package/dist/loader.cjs +20 -0
- package/package.json +4 -3
- package/src/markdown.tsx +84 -0
package/dist/config.cjs
CHANGED
|
@@ -89,6 +89,18 @@ function isMediaKind(value) {
|
|
|
89
89
|
}
|
|
90
90
|
|
|
91
91
|
// ../core/src/fields.ts
|
|
92
|
+
var MARKDOWN_FORMATS = ["bold", "italic", "link", "heading", "list"];
|
|
93
|
+
var DEFAULT_MARKDOWN_ALLOW = ["bold", "italic", "link"];
|
|
94
|
+
function isMarkdownFormat(value) {
|
|
95
|
+
return MARKDOWN_FORMATS.includes(value);
|
|
96
|
+
}
|
|
97
|
+
function normalizeAllow(value) {
|
|
98
|
+
if (!Array.isArray(value) || value.length === 0)
|
|
99
|
+
return null;
|
|
100
|
+
if (!value.every(isMarkdownFormat))
|
|
101
|
+
return null;
|
|
102
|
+
return MARKDOWN_FORMATS.filter((format) => value.includes(format));
|
|
103
|
+
}
|
|
92
104
|
var PICKER_VIEWS = ["auto", "grouped", "flat"];
|
|
93
105
|
function isPickerView(value) {
|
|
94
106
|
return PICKER_VIEWS.includes(value);
|
|
@@ -301,6 +313,13 @@ var constructors = {
|
|
|
301
313
|
...opts?.input ? { input: opts.input } : {}
|
|
302
314
|
});
|
|
303
315
|
},
|
|
316
|
+
markdown: (opts) => {
|
|
317
|
+
const allow = opts?.allow === undefined ? [...DEFAULT_MARKDOWN_ALLOW] : normalizeAllow(opts.allow);
|
|
318
|
+
if (!allow) {
|
|
319
|
+
throw new Error('field.markdown allow must list "bold", "italic", "link", "heading" and/or "list"');
|
|
320
|
+
}
|
|
321
|
+
return { ...base("markdown", { required: opts?.required, input: opts?.input }), allow };
|
|
322
|
+
},
|
|
304
323
|
image: (opts) => base("image", opts),
|
|
305
324
|
media: (opts) => {
|
|
306
325
|
const def = base("media", opts);
|
|
@@ -333,6 +352,7 @@ var constructors = {
|
|
|
333
352
|
};
|
|
334
353
|
var field = {
|
|
335
354
|
text: described(constructors.text, 0),
|
|
355
|
+
markdown: described(constructors.markdown, 0),
|
|
336
356
|
image: described(constructors.image, 0),
|
|
337
357
|
media: described(constructors.media, 0),
|
|
338
358
|
number: described(constructors.number, 0),
|
package/dist/loader.cjs
CHANGED
|
@@ -82,6 +82,18 @@ function isMediaKind(value) {
|
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
// ../core/src/fields.ts
|
|
85
|
+
var MARKDOWN_FORMATS = ["bold", "italic", "link", "heading", "list"];
|
|
86
|
+
var DEFAULT_MARKDOWN_ALLOW = ["bold", "italic", "link"];
|
|
87
|
+
function isMarkdownFormat(value) {
|
|
88
|
+
return MARKDOWN_FORMATS.includes(value);
|
|
89
|
+
}
|
|
90
|
+
function normalizeAllow(value) {
|
|
91
|
+
if (!Array.isArray(value) || value.length === 0)
|
|
92
|
+
return null;
|
|
93
|
+
if (!value.every(isMarkdownFormat))
|
|
94
|
+
return null;
|
|
95
|
+
return MARKDOWN_FORMATS.filter((format) => value.includes(format));
|
|
96
|
+
}
|
|
85
97
|
var PICKER_VIEWS = ["auto", "grouped", "flat"];
|
|
86
98
|
function isPickerView(value) {
|
|
87
99
|
return PICKER_VIEWS.includes(value);
|
|
@@ -294,6 +306,13 @@ var constructors = {
|
|
|
294
306
|
...opts?.input ? { input: opts.input } : {}
|
|
295
307
|
});
|
|
296
308
|
},
|
|
309
|
+
markdown: (opts) => {
|
|
310
|
+
const allow = opts?.allow === undefined ? [...DEFAULT_MARKDOWN_ALLOW] : normalizeAllow(opts.allow);
|
|
311
|
+
if (!allow) {
|
|
312
|
+
throw new Error('field.markdown allow must list "bold", "italic", "link", "heading" and/or "list"');
|
|
313
|
+
}
|
|
314
|
+
return { ...base("markdown", { required: opts?.required, input: opts?.input }), allow };
|
|
315
|
+
},
|
|
297
316
|
image: (opts) => base("image", opts),
|
|
298
317
|
media: (opts) => {
|
|
299
318
|
const def = base("media", opts);
|
|
@@ -326,6 +345,7 @@ var constructors = {
|
|
|
326
345
|
};
|
|
327
346
|
var field = {
|
|
328
347
|
text: described(constructors.text, 0),
|
|
348
|
+
markdown: described(constructors.markdown, 0),
|
|
329
349
|
image: described(constructors.image, 0),
|
|
330
350
|
media: described(constructors.media, 0),
|
|
331
351
|
number: described(constructors.number, 0),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@backmatter/next",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.67",
|
|
4
4
|
"description": "Experimental Next.js adapter for Backmatter. APIs will break. Not ready for production.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"publishConfig": {
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
".": "./src/index.ts",
|
|
16
16
|
"./preview": "./src/preview.ts",
|
|
17
17
|
"./devbar": "./src/devbar.tsx",
|
|
18
|
+
"./markdown": "./src/markdown.tsx",
|
|
18
19
|
"./mdx": "./src/mdx.ts",
|
|
19
20
|
"./mdx/client": "./src/mdx-client.tsx",
|
|
20
21
|
"./config": {
|
|
@@ -47,10 +48,10 @@
|
|
|
47
48
|
"test": "bun test"
|
|
48
49
|
},
|
|
49
50
|
"dependencies": {
|
|
50
|
-
"@backmatter/core": "^0.0.
|
|
51
|
+
"@backmatter/core": "^0.0.54",
|
|
51
52
|
"@mdx-js/mdx": "^3.1.1",
|
|
52
53
|
"remark-gfm": "^4.0.1",
|
|
53
|
-
"@backmatter/studio": "^0.0.
|
|
54
|
+
"@backmatter/studio": "^0.0.44"
|
|
54
55
|
},
|
|
55
56
|
"peerDependencies": {
|
|
56
57
|
"next": ">=16.3.0",
|
package/src/markdown.tsx
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// packages/next/src/markdown.tsx
|
|
2
|
+
// Renders a `field.markdown()` value. No hooks, so it works in server and client
|
|
3
|
+
// components. It never evaluates MDX or HTML (ADR 0007).
|
|
4
|
+
import { createElement, Fragment, type ComponentType, type ReactNode } from "react";
|
|
5
|
+
import {
|
|
6
|
+
MARKDOWN_FORMATS,
|
|
7
|
+
parseMarkdownField,
|
|
8
|
+
type MarkdownBlock,
|
|
9
|
+
type MarkdownFormat,
|
|
10
|
+
type MarkdownInline,
|
|
11
|
+
} from "@backmatter/core";
|
|
12
|
+
|
|
13
|
+
type ChildrenProps = { children?: ReactNode };
|
|
14
|
+
|
|
15
|
+
/** Elements a site can replace, for example `a` with its own link component. */
|
|
16
|
+
export type MarkdownComponents = Partial<{
|
|
17
|
+
p: ComponentType<ChildrenProps>;
|
|
18
|
+
h3: ComponentType<ChildrenProps>;
|
|
19
|
+
h4: ComponentType<ChildrenProps>;
|
|
20
|
+
ul: ComponentType<ChildrenProps>;
|
|
21
|
+
ol: ComponentType<ChildrenProps>;
|
|
22
|
+
li: ComponentType<ChildrenProps>;
|
|
23
|
+
strong: ComponentType<ChildrenProps>;
|
|
24
|
+
em: ComponentType<ChildrenProps>;
|
|
25
|
+
a: ComponentType<ChildrenProps & { href: string }>;
|
|
26
|
+
}>;
|
|
27
|
+
|
|
28
|
+
export type MarkdownProps = {
|
|
29
|
+
/** The field's value. */
|
|
30
|
+
source: string | null | undefined;
|
|
31
|
+
/** The field's `allow`, for example `fields.body.allow`. Anything else renders as text. */
|
|
32
|
+
allow?: readonly MarkdownFormat[];
|
|
33
|
+
components?: MarkdownComponents;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
function element(
|
|
37
|
+
components: MarkdownComponents,
|
|
38
|
+
tag: keyof MarkdownComponents,
|
|
39
|
+
key: number,
|
|
40
|
+
props: Record<string, unknown>,
|
|
41
|
+
children: ReactNode,
|
|
42
|
+
): ReactNode {
|
|
43
|
+
const component = components[tag] as ComponentType<Record<string, unknown>> | undefined;
|
|
44
|
+
return createElement(component ?? tag, { key, ...props }, children);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function inline(nodes: MarkdownInline[], components: MarkdownComponents): ReactNode[] {
|
|
48
|
+
return nodes.map((node, key) => {
|
|
49
|
+
switch (node.type) {
|
|
50
|
+
case "text":
|
|
51
|
+
return createElement(Fragment, { key }, node.value);
|
|
52
|
+
case "break":
|
|
53
|
+
return createElement("br", { key });
|
|
54
|
+
case "strong":
|
|
55
|
+
return element(components, "strong", key, {}, inline(node.children, components));
|
|
56
|
+
case "emphasis":
|
|
57
|
+
return element(components, "em", key, {}, inline(node.children, components));
|
|
58
|
+
case "link":
|
|
59
|
+
return element(components, "a", key, { href: node.href }, inline(node.children, components));
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function block(node: MarkdownBlock, key: number, components: MarkdownComponents): ReactNode {
|
|
65
|
+
if (node.type === "list") {
|
|
66
|
+
const items = node.items.map((item, index) =>
|
|
67
|
+
element(components, "li", index, {}, inline(item, components)),
|
|
68
|
+
);
|
|
69
|
+
return element(components, node.ordered ? "ol" : "ul", key, {}, items);
|
|
70
|
+
}
|
|
71
|
+
const tag = node.type === "heading" ? (`h${node.level}` as const) : "p";
|
|
72
|
+
return element(components, tag, key, {}, inline(node.children, components));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** `<Markdown source={props.body} allow={fields.body.allow} />` */
|
|
76
|
+
export function Markdown({ source, allow = MARKDOWN_FORMATS, components = {} }: MarkdownProps) {
|
|
77
|
+
if (!source) return null;
|
|
78
|
+
const { blocks } = parseMarkdownField(source, allow);
|
|
79
|
+
return createElement(
|
|
80
|
+
Fragment,
|
|
81
|
+
null,
|
|
82
|
+
blocks.map((node, key) => block(node, key, components)),
|
|
83
|
+
);
|
|
84
|
+
}
|