@backmatter/next 0.0.73 → 0.0.75

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 CHANGED
@@ -94,13 +94,41 @@ var DEFAULT_MARKDOWN_ALLOW = ["bold", "italic", "link"];
94
94
  function isMarkdownFormat(value) {
95
95
  return MARKDOWN_FORMATS.includes(value);
96
96
  }
97
- function normalizeAllow(value) {
98
- if (!Array.isArray(value) || value.length === 0)
97
+ function normalizeAllow(value, empty = false) {
98
+ if (!Array.isArray(value) || value.length === 0 && !empty)
99
99
  return null;
100
100
  if (!value.every(isMarkdownFormat))
101
101
  return null;
102
102
  return MARKDOWN_FORMATS.filter((format) => value.includes(format));
103
103
  }
104
+ var MARK_NAME = /^[a-z][a-z0-9-]*$/;
105
+ function markLabel(name) {
106
+ const words = name.replace(/-/g, " ");
107
+ return words.charAt(0).toUpperCase() + words.slice(1);
108
+ }
109
+ function normalizeMarks(value, onBadName) {
110
+ if (!value || typeof value !== "object" || Array.isArray(value))
111
+ return null;
112
+ const out = {};
113
+ for (const [name, raw] of Object.entries(value)) {
114
+ if (!MARK_NAME.test(name)) {
115
+ onBadName?.(name);
116
+ return null;
117
+ }
118
+ if (!raw || typeof raw !== "object" || Array.isArray(raw))
119
+ return null;
120
+ const { label, description } = raw;
121
+ if (label !== undefined && typeof label !== "string")
122
+ return null;
123
+ if (description !== undefined && typeof description !== "string")
124
+ return null;
125
+ out[name] = {
126
+ label: label?.trim() || markLabel(name),
127
+ ...description?.trim() ? { description: description.trim() } : {}
128
+ };
129
+ }
130
+ return Object.keys(out).length > 0 ? out : null;
131
+ }
104
132
  var PICKER_VIEWS = ["auto", "grouped", "flat"];
105
133
  function isPickerView(value) {
106
134
  return PICKER_VIEWS.includes(value);
@@ -314,11 +342,20 @@ var constructors = {
314
342
  });
315
343
  },
316
344
  markdown: (opts) => {
317
- const allow = opts?.allow === undefined ? [...DEFAULT_MARKDOWN_ALLOW] : normalizeAllow(opts.allow);
345
+ const marks = opts?.marks === undefined ? null : normalizeMarks(opts.marks, (name) => {
346
+ throw new Error(`field.markdown mark "${name}" must be lowercase letters, digits and dashes`);
347
+ });
348
+ if (opts?.marks !== undefined && !marks)
349
+ throw new Error("field.markdown marks must declare a mark");
350
+ const allow = opts?.allow === undefined ? [...DEFAULT_MARKDOWN_ALLOW] : normalizeAllow(opts.allow, marks !== null);
318
351
  if (!allow) {
319
352
  throw new Error('field.markdown allow must list "bold", "italic", "link", "heading" and/or "list"');
320
353
  }
321
- return { ...base("markdown", { required: opts?.required, input: opts?.input }), allow };
354
+ return {
355
+ ...base("markdown", { required: opts?.required, input: opts?.input }),
356
+ allow,
357
+ ...marks ? { marks } : {}
358
+ };
322
359
  },
323
360
  image: (opts) => base("image", opts),
324
361
  media: (opts) => {
package/dist/loader.cjs CHANGED
@@ -87,13 +87,41 @@ var DEFAULT_MARKDOWN_ALLOW = ["bold", "italic", "link"];
87
87
  function isMarkdownFormat(value) {
88
88
  return MARKDOWN_FORMATS.includes(value);
89
89
  }
90
- function normalizeAllow(value) {
91
- if (!Array.isArray(value) || value.length === 0)
90
+ function normalizeAllow(value, empty = false) {
91
+ if (!Array.isArray(value) || value.length === 0 && !empty)
92
92
  return null;
93
93
  if (!value.every(isMarkdownFormat))
94
94
  return null;
95
95
  return MARKDOWN_FORMATS.filter((format) => value.includes(format));
96
96
  }
97
+ var MARK_NAME = /^[a-z][a-z0-9-]*$/;
98
+ function markLabel(name) {
99
+ const words = name.replace(/-/g, " ");
100
+ return words.charAt(0).toUpperCase() + words.slice(1);
101
+ }
102
+ function normalizeMarks(value, onBadName) {
103
+ if (!value || typeof value !== "object" || Array.isArray(value))
104
+ return null;
105
+ const out = {};
106
+ for (const [name, raw] of Object.entries(value)) {
107
+ if (!MARK_NAME.test(name)) {
108
+ onBadName?.(name);
109
+ return null;
110
+ }
111
+ if (!raw || typeof raw !== "object" || Array.isArray(raw))
112
+ return null;
113
+ const { label, description } = raw;
114
+ if (label !== undefined && typeof label !== "string")
115
+ return null;
116
+ if (description !== undefined && typeof description !== "string")
117
+ return null;
118
+ out[name] = {
119
+ label: label?.trim() || markLabel(name),
120
+ ...description?.trim() ? { description: description.trim() } : {}
121
+ };
122
+ }
123
+ return Object.keys(out).length > 0 ? out : null;
124
+ }
97
125
  var PICKER_VIEWS = ["auto", "grouped", "flat"];
98
126
  function isPickerView(value) {
99
127
  return PICKER_VIEWS.includes(value);
@@ -307,11 +335,20 @@ var constructors = {
307
335
  });
308
336
  },
309
337
  markdown: (opts) => {
310
- const allow = opts?.allow === undefined ? [...DEFAULT_MARKDOWN_ALLOW] : normalizeAllow(opts.allow);
338
+ const marks = opts?.marks === undefined ? null : normalizeMarks(opts.marks, (name) => {
339
+ throw new Error(`field.markdown mark "${name}" must be lowercase letters, digits and dashes`);
340
+ });
341
+ if (opts?.marks !== undefined && !marks)
342
+ throw new Error("field.markdown marks must declare a mark");
343
+ const allow = opts?.allow === undefined ? [...DEFAULT_MARKDOWN_ALLOW] : normalizeAllow(opts.allow, marks !== null);
311
344
  if (!allow) {
312
345
  throw new Error('field.markdown allow must list "bold", "italic", "link", "heading" and/or "list"');
313
346
  }
314
- return { ...base("markdown", { required: opts?.required, input: opts?.input }), allow };
347
+ return {
348
+ ...base("markdown", { required: opts?.required, input: opts?.input }),
349
+ allow,
350
+ ...marks ? { marks } : {}
351
+ };
315
352
  },
316
353
  image: (opts) => base("image", opts),
317
354
  media: (opts) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backmatter/next",
3
- "version": "0.0.73",
3
+ "version": "0.0.75",
4
4
  "description": "Experimental Next.js adapter for Backmatter. APIs will break. Not ready for production.",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -48,10 +48,10 @@
48
48
  "test": "bun test"
49
49
  },
50
50
  "dependencies": {
51
- "@backmatter/core": "^0.0.56",
51
+ "@backmatter/core": "^0.0.57",
52
52
  "@mdx-js/mdx": "^3.1.1",
53
53
  "remark-gfm": "^4.0.1",
54
- "@backmatter/studio": "^0.0.50"
54
+ "@backmatter/studio": "^0.0.52"
55
55
  },
56
56
  "peerDependencies": {
57
57
  "next": ">=16.3.0",
package/src/markdown.tsx CHANGED
@@ -1,6 +1,6 @@
1
1
  // packages/next/src/markdown.tsx
2
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).
3
+ // components. It never evaluates MDX or HTML (ADR 0007). Marks: ADR 0008.
4
4
  import { createElement, Fragment, type ComponentType, type ReactNode } from "react";
5
5
  import {
6
6
  MARKDOWN_FORMATS,
@@ -23,6 +23,8 @@ export type MarkdownComponents = Partial<{
23
23
  strong: ComponentType<ChildrenProps>;
24
24
  em: ComponentType<ChildrenProps>;
25
25
  a: ComponentType<ChildrenProps & { href: string }>;
26
+ /** Every mark the field declares; `name` says which. Without it a mark is `<span data-mark>`. */
27
+ mark: ComponentType<ChildrenProps & { name: string }>;
26
28
  }>;
27
29
 
28
30
  export type MarkdownProps = {
@@ -30,6 +32,10 @@ export type MarkdownProps = {
30
32
  source: string | null | undefined;
31
33
  /** The field's `allow`, for example `fields.body.allow`. Anything else renders as text. */
32
34
  allow?: readonly MarkdownFormat[];
35
+ /** The field's `marks`, for example `fields.title.marks`, or their names. Without it a mark renders as written. */
36
+ marks?: readonly string[] | Readonly<Record<string, unknown>>;
37
+ /** Render without `<p>`, for a field inside the site's own heading or paragraph. Paragraphs join with `<br>`. */
38
+ inline?: boolean;
33
39
  components?: MarkdownComponents;
34
40
  };
35
41
 
@@ -57,6 +63,11 @@ function inline(nodes: MarkdownInline[], components: MarkdownComponents): ReactN
57
63
  return element(components, "em", key, {}, inline(node.children, components));
58
64
  case "link":
59
65
  return element(components, "a", key, { href: node.href }, inline(node.children, components));
66
+ case "mark": {
67
+ const children = inline(node.children, components);
68
+ if (components.mark) return createElement(components.mark, { key, name: node.name }, children);
69
+ return createElement("span", { key, "data-mark": node.name }, children);
70
+ }
60
71
  }
61
72
  });
62
73
  }
@@ -72,10 +83,28 @@ function block(node: MarkdownBlock, key: number, components: MarkdownComponents)
72
83
  return element(components, tag, key, {}, inline(node.children, components));
73
84
  }
74
85
 
75
- /** `<Markdown source={props.body} allow={fields.body.allow} />` */
76
- export function Markdown({ source, allow = MARKDOWN_FORMATS, components = {} }: MarkdownProps) {
86
+ /** The inline content of every block, one line each, for `inline`. */
87
+ function lines(node: MarkdownBlock): MarkdownInline[][] {
88
+ return node.type === "list" ? node.items : [node.children];
89
+ }
90
+
91
+ function markNames(marks: MarkdownProps["marks"]): readonly string[] {
92
+ if (!marks) return [];
93
+ return Array.isArray(marks) ? marks : Object.keys(marks);
94
+ }
95
+
96
+ /** `<Markdown source={props.body} allow={fields.body.allow} marks={fields.body.marks} />` */
97
+ export function Markdown({ source, allow = MARKDOWN_FORMATS, marks, inline: flat, components = {} }: MarkdownProps) {
77
98
  if (!source) return null;
78
- const { blocks } = parseMarkdownField(source, allow);
99
+ const { blocks } = parseMarkdownField(source, allow, markNames(marks));
100
+ if (flat) {
101
+ const children: ReactNode[] = [];
102
+ blocks.flatMap(lines).forEach((line, index) => {
103
+ if (index > 0) children.push(createElement("br", { key: `br${index}` }));
104
+ children.push(createElement(Fragment, { key: index }, inline(line, components)));
105
+ });
106
+ return createElement(Fragment, null, children);
107
+ }
79
108
  return createElement(
80
109
  Fragment,
81
110
  null,