@draftbase/renderer 0.4.1 → 0.4.3
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.md +25 -0
- package/dist/MDXContent.d.ts +6 -11
- package/dist/MDXContent.js +4 -8
- package/dist/MDXContent.test.js +1 -3
- package/dist/MDXErrorBoundary.js +2 -4
- package/dist/compileMDX.test.js +2 -4
- package/dist/core.d.ts +4 -11
- package/dist/core.js +4 -11
- package/dist/reactNative.d.ts +2 -15
- package/dist/reactNative.js +34 -16
- package/dist/reactNative.test.js +2 -3
- package/dist/reactNativeComponents.d.ts +8 -21
- package/dist/reactNativeComponents.js +18 -9
- package/dist/reactNativeComponents.test.js +2 -3
- package/dist/reactNativeStyles.d.ts +2 -3
- package/dist/reactNativeStyles.js +2 -3
- package/dist/toHtml.d.ts +17 -7
- package/dist/toHtml.js +63 -11
- package/dist/toHtml.test.js +40 -0
- package/dist/vue.d.ts +2 -4
- package/dist/vue.js +2 -4
- package/package.json +3 -1
package/README.md
CHANGED
|
@@ -232,6 +232,31 @@ import { toHtml } from "@draftbase/renderer";
|
|
|
232
232
|
const html = await toHtml(entry.fields.body);
|
|
233
233
|
```
|
|
234
234
|
|
|
235
|
+
By default, custom tags in the source (`<Callout>`, `<EntryLink>`, ...) pass through as literal HTML — `toHtml` produces a plain string, not a React tree, so there's no component tree to substitute into. Pass `components` to render one to an HTML string instead, keyed by tag name (case-insensitive):
|
|
236
|
+
|
|
237
|
+
```ts
|
|
238
|
+
const html = await toHtml(entry.fields.body, {
|
|
239
|
+
components: {
|
|
240
|
+
Callout: (props, childrenHtml) => `<div class="callout-${props.type}">${childrenHtml}</div>`,
|
|
241
|
+
},
|
|
242
|
+
externalLinks: true, // adds target="_blank" rel="noopener noreferrer" to off-site <a> tags
|
|
243
|
+
});
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
Unlike a React `components` map, each prop always arrives as a string — real HTML parsing, not JSX evaluation, so `count="3"` is `"3"`, never `3`. `childrenHtml` is the tag's contents already rendered to HTML, with any nested custom tags resolved first.
|
|
247
|
+
|
|
248
|
+
`EntryLink` gets a default (`<a href="/entries/{id}">`) the moment `components` is passed at all, same as the React renderer's default — override it the same way as any other tag:
|
|
249
|
+
|
|
250
|
+
```ts
|
|
251
|
+
const html = await toHtml(entry.fields.body, {
|
|
252
|
+
components: {
|
|
253
|
+
EntryLink: (props, childrenHtml) => `<a href="/blog/${props.id}">${childrenHtml}</a>`,
|
|
254
|
+
},
|
|
255
|
+
});
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
Omit `components` entirely and every custom tag, `EntryLink` included, stays literal HTML (previous behavior); omit `externalLinks` and links are left as-is.
|
|
259
|
+
|
|
235
260
|
## Custom components
|
|
236
261
|
|
|
237
262
|
Content can invoke JSX components by name inside the MDX source (e.g. `<Callout type="warning">...</Callout>`). Pass the implementations:
|
package/dist/MDXContent.d.ts
CHANGED
|
@@ -9,19 +9,15 @@ export interface CompiledMDX {
|
|
|
9
9
|
}
|
|
10
10
|
export type { FailedMDX };
|
|
11
11
|
/**
|
|
12
|
-
* Compiles raw MDX/markdown into a renderable React component — no DOM assumptions,
|
|
13
|
-
*
|
|
14
|
-
* `MDXContent` component below does the latter). Standard markdown elements (`p`,
|
|
15
|
-
* `h1`, `table`, ...) render via real DOM tags with zero setup; only a JSX component
|
|
16
|
-
* with no built-in default (e.g. `<Callout>`) needs `components`.
|
|
12
|
+
* Compiles raw MDX/markdown into a renderable React component — no DOM assumptions, safe for client effects or RSC.
|
|
13
|
+
* Standard markdown elements render via real DOM tags with zero setup; only a JSX component with no built-in default needs `components`.
|
|
17
14
|
*/
|
|
18
15
|
export declare function compileMDX(source: string): Promise<CompiledMDX | FailedMDX>;
|
|
19
16
|
export interface MDXContentProps {
|
|
20
17
|
/** Raw MDX/markdown string, e.g. an entry's rich text field. */
|
|
21
18
|
source: string;
|
|
22
|
-
/** Custom components available by name inside the MDX source
|
|
23
|
-
*
|
|
24
|
-
* non-DOM renderers such as React Native, which have no intrinsic `div`/`p`/`a` tags. */
|
|
19
|
+
/** Custom components available by name inside the MDX source, and/or overrides for standard markdown elements —
|
|
20
|
+
* required on non-DOM renderers such as React Native, which have no intrinsic `div`/`p`/`a` tags. */
|
|
25
21
|
components?: MDXComponents;
|
|
26
22
|
/** Skip the default `db-content` styling class. */
|
|
27
23
|
unstyled?: boolean;
|
|
@@ -35,8 +31,7 @@ export interface MDXContentProps {
|
|
|
35
31
|
errorTag?: ElementType;
|
|
36
32
|
}
|
|
37
33
|
/**
|
|
38
|
-
* Next.js Server Component wrapper around {@link compileMDX}. For non-RSC React
|
|
39
|
-
*
|
|
40
|
-
* and render `Content` yourself — `await` inside a component body only works as an RSC.
|
|
34
|
+
* Next.js Server Component wrapper around {@link compileMDX}. For non-RSC React, call `compileMDX` directly instead —
|
|
35
|
+
* `await` inside a component body only works as an RSC.
|
|
41
36
|
*/
|
|
42
37
|
export declare function MDXContent({ source, components, unstyled, className, wrapperTag, errorTag, }: MDXContentProps): Promise<ReactNode>;
|
package/dist/MDXContent.js
CHANGED
|
@@ -5,19 +5,15 @@ import { wrapperClassName } from "./wrapperClassName.js";
|
|
|
5
5
|
import { MDXErrorBoundary } from "./MDXErrorBoundary.js";
|
|
6
6
|
const defaultComponents = { EntryLink: makeDefaultEntryLink(runtime) };
|
|
7
7
|
/**
|
|
8
|
-
* Compiles raw MDX/markdown into a renderable React component — no DOM assumptions,
|
|
9
|
-
*
|
|
10
|
-
* `MDXContent` component below does the latter). Standard markdown elements (`p`,
|
|
11
|
-
* `h1`, `table`, ...) render via real DOM tags with zero setup; only a JSX component
|
|
12
|
-
* with no built-in default (e.g. `<Callout>`) needs `components`.
|
|
8
|
+
* Compiles raw MDX/markdown into a renderable React component — no DOM assumptions, safe for client effects or RSC.
|
|
9
|
+
* Standard markdown elements render via real DOM tags with zero setup; only a JSX component with no built-in default needs `components`.
|
|
13
10
|
*/
|
|
14
11
|
export async function compileMDX(source) {
|
|
15
12
|
return compileMDXCore(source, runtime, defaultComponents);
|
|
16
13
|
}
|
|
17
14
|
/**
|
|
18
|
-
* Next.js Server Component wrapper around {@link compileMDX}. For non-RSC React
|
|
19
|
-
*
|
|
20
|
-
* and render `Content` yourself — `await` inside a component body only works as an RSC.
|
|
15
|
+
* Next.js Server Component wrapper around {@link compileMDX}. For non-RSC React, call `compileMDX` directly instead —
|
|
16
|
+
* `await` inside a component body only works as an RSC.
|
|
21
17
|
*/
|
|
22
18
|
export async function MDXContent({ source, components, unstyled, className, wrapperTag = "div", errorTag = "p", }) {
|
|
23
19
|
const Wrapper = wrapperTag;
|
package/dist/MDXContent.test.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
2
|
import test from "node:test";
|
|
3
3
|
import { MDXContent } from "./MDXContent.js";
|
|
4
|
-
//
|
|
5
|
-
// Server Component; call it directly (as React's RSC runtime would) and inspect the
|
|
6
|
-
// element tree it returns, same style as compileMDX.test.ts.
|
|
4
|
+
// MDXContent is an async Server Component; call it directly (as React's RSC runtime would) and inspect the returned element tree.
|
|
7
5
|
test("MDXContent wraps compiled MDX in the styled wrapper element", async () => {
|
|
8
6
|
const element = (await MDXContent({ source: "# Hello" }));
|
|
9
7
|
assert.equal(element.type, "div");
|
package/dist/MDXErrorBoundary.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { Component } from "react";
|
|
3
|
-
// Catches render-time errors from compiled MDX content — most commonly a JSX component
|
|
4
|
-
//
|
|
5
|
-
// `components`. Without this, that throws "Element type is invalid" and crashes the whole
|
|
6
|
-
// page instead of just the content block.
|
|
3
|
+
// Catches render-time errors from compiled MDX content — most commonly a JSX component referenced in the
|
|
4
|
+
// source but missing from `components`, which otherwise throws and crashes the whole page.
|
|
7
5
|
export class MDXErrorBoundary extends Component {
|
|
8
6
|
state = { hasError: false };
|
|
9
7
|
static getDerivedStateFromError() {
|
package/dist/compileMDX.test.js
CHANGED
|
@@ -3,10 +3,8 @@ import test from "node:test";
|
|
|
3
3
|
import { compileMDX as compileReactMDX } from "./MDXContent.js";
|
|
4
4
|
import { compileMDX as compileVueMDX } from "./vue.js";
|
|
5
5
|
const SOURCE = "# Hello\n\nWorld";
|
|
6
|
-
/**
|
|
7
|
-
*
|
|
8
|
-
* same "call it like a plain function" style as the rest of these tests (no renderer
|
|
9
|
-
* mounted). */
|
|
6
|
+
/** Content is wrapped to merge in default components (see core.ts) — unwrap by invoking each
|
|
7
|
+
* function-typed element until reaching real output (no renderer mounted). */
|
|
10
8
|
function resolve(element) {
|
|
11
9
|
return typeof element.type === "function"
|
|
12
10
|
? resolve(element.type(element.props))
|
package/dist/core.d.ts
CHANGED
|
@@ -9,19 +9,12 @@ export interface FailedMDX {
|
|
|
9
9
|
error: unknown;
|
|
10
10
|
}
|
|
11
11
|
/**
|
|
12
|
-
* Shared MDX-to-component evaluation
|
|
13
|
-
*
|
|
14
|
-
* supplies its own without duplicating the remark/rehype pipeline.
|
|
15
|
-
*
|
|
16
|
-
* `defaultComponents`, when given, are merged underneath whatever the caller passes
|
|
17
|
-
* as `Content`'s own `components` prop (caller always wins per-tag) — this is what
|
|
18
|
-
* lets a framework entry point ship working defaults (e.g. a default `EntryLink`,
|
|
19
|
-
* or React Native's Text/View mapping) without requiring a mapping up front.
|
|
12
|
+
* Shared MDX-to-component evaluation, parameterized by JSX runtime so each framework entry point avoids duplicating
|
|
13
|
+
* the remark/rehype pipeline. `defaultComponents`, when given, are merged underneath the caller's own `components` (caller always wins per-tag).
|
|
20
14
|
*/
|
|
21
15
|
export declare function compileMDXCore<TComponent>(source: string, jsxRuntime: JsxRuntime, defaultComponents?: Record<string, unknown>): Promise<CompiledMDX<TComponent> | FailedMDX>;
|
|
22
|
-
/** Default `EntryLink` for a given JSX runtime — renders `<a href="/entries/{id}"
|
|
23
|
-
*
|
|
24
|
-
* route entries differently still override it by passing `components.EntryLink`. */
|
|
16
|
+
/** Default `EntryLink` for a given JSX runtime — renders `<a href="/entries/{id}">`; apps that
|
|
17
|
+
* route entries differently override it by passing `components.EntryLink`. */
|
|
25
18
|
export declare function makeDefaultEntryLink(jsxRuntime: JsxRuntime): ({ id, children }: {
|
|
26
19
|
id?: string;
|
|
27
20
|
children?: unknown;
|
package/dist/core.js
CHANGED
|
@@ -2,14 +2,8 @@ import { evaluate } from "@mdx-js/mdx";
|
|
|
2
2
|
import remarkGfm from "remark-gfm";
|
|
3
3
|
import rehypeSlug from "rehype-slug";
|
|
4
4
|
/**
|
|
5
|
-
* Shared MDX-to-component evaluation
|
|
6
|
-
*
|
|
7
|
-
* supplies its own without duplicating the remark/rehype pipeline.
|
|
8
|
-
*
|
|
9
|
-
* `defaultComponents`, when given, are merged underneath whatever the caller passes
|
|
10
|
-
* as `Content`'s own `components` prop (caller always wins per-tag) — this is what
|
|
11
|
-
* lets a framework entry point ship working defaults (e.g. a default `EntryLink`,
|
|
12
|
-
* or React Native's Text/View mapping) without requiring a mapping up front.
|
|
5
|
+
* Shared MDX-to-component evaluation, parameterized by JSX runtime so each framework entry point avoids duplicating
|
|
6
|
+
* the remark/rehype pipeline. `defaultComponents`, when given, are merged underneath the caller's own `components` (caller always wins per-tag).
|
|
13
7
|
*/
|
|
14
8
|
export async function compileMDXCore(source, jsxRuntime, defaultComponents) {
|
|
15
9
|
try {
|
|
@@ -37,9 +31,8 @@ function withDefaultComponents(Content, defaults, jsxRuntime) {
|
|
|
37
31
|
});
|
|
38
32
|
};
|
|
39
33
|
}
|
|
40
|
-
/** Default `EntryLink` for a given JSX runtime — renders `<a href="/entries/{id}"
|
|
41
|
-
*
|
|
42
|
-
* route entries differently still override it by passing `components.EntryLink`. */
|
|
34
|
+
/** Default `EntryLink` for a given JSX runtime — renders `<a href="/entries/{id}">`; apps that
|
|
35
|
+
* route entries differently override it by passing `components.EntryLink`. */
|
|
43
36
|
export function makeDefaultEntryLink(jsxRuntime) {
|
|
44
37
|
return function EntryLink({ id, children }) {
|
|
45
38
|
return jsxRuntime.jsx("a", { href: id ? `/entries/${id}` : undefined, children });
|
package/dist/reactNative.d.ts
CHANGED
|
@@ -10,21 +10,8 @@ export interface CompiledMDX {
|
|
|
10
10
|
}
|
|
11
11
|
export type { FailedMDX };
|
|
12
12
|
/**
|
|
13
|
-
* Wires up a React Native `compileMDX` with default Text/View/Image mappings for
|
|
14
|
-
*
|
|
15
|
-
* primitives once instead of mapping every tag on every call:
|
|
16
|
-
*
|
|
17
|
-
* ```ts
|
|
18
|
-
* import { Text, View, Image } from "react-native";
|
|
19
|
-
* import { createReactNativeRenderer } from "@draftbase/renderer/react-native";
|
|
20
|
-
*
|
|
21
|
-
* const { compileMDX } = createReactNativeRenderer({ Text, View, Image });
|
|
22
|
-
* ```
|
|
23
|
-
*
|
|
24
|
-
* Ships default styling that mirrors the web look (heading scale, link color, table
|
|
25
|
-
* layout, ...) — pass `styleOptions.unstyled` to drop it, or `styleOptions.styles` to
|
|
26
|
-
* override specific tags (`{ h1: { fontSize: 32 } }`). Pass `components` on the
|
|
27
|
-
* returned `Content` to override individual tags' component, same as the web entry point.
|
|
13
|
+
* Wires up a React Native `compileMDX` with default Text/View/Image mappings for every standard markdown element
|
|
14
|
+
* (and `EntryLink`), so a project sets up its RN primitives once instead of mapping every tag on every call.
|
|
28
15
|
*/
|
|
29
16
|
export declare function createReactNativeRenderer(primitives: ReactNativePrimitives, styleOptions?: ReactNativeStyleOptions): {
|
|
30
17
|
compileMDX: (source: string) => Promise<CompiledMDX | FailedMDX>;
|
package/dist/reactNative.js
CHANGED
|
@@ -2,26 +2,44 @@ import * as runtime from "react/jsx-runtime";
|
|
|
2
2
|
import { compileMDXCore } from "./core.js";
|
|
3
3
|
import { buildReactNativeComponents, } from "./reactNativeComponents.js";
|
|
4
4
|
/**
|
|
5
|
-
* Wires up a React Native `compileMDX` with default Text/View/Image mappings for
|
|
6
|
-
*
|
|
7
|
-
* primitives once instead of mapping every tag on every call:
|
|
8
|
-
*
|
|
9
|
-
* ```ts
|
|
10
|
-
* import { Text, View, Image } from "react-native";
|
|
11
|
-
* import { createReactNativeRenderer } from "@draftbase/renderer/react-native";
|
|
12
|
-
*
|
|
13
|
-
* const { compileMDX } = createReactNativeRenderer({ Text, View, Image });
|
|
14
|
-
* ```
|
|
15
|
-
*
|
|
16
|
-
* Ships default styling that mirrors the web look (heading scale, link color, table
|
|
17
|
-
* layout, ...) — pass `styleOptions.unstyled` to drop it, or `styleOptions.styles` to
|
|
18
|
-
* override specific tags (`{ h1: { fontSize: 32 } }`). Pass `components` on the
|
|
19
|
-
* returned `Content` to override individual tags' component, same as the web entry point.
|
|
5
|
+
* Wires up a React Native `compileMDX` with default Text/View/Image mappings for every standard markdown element
|
|
6
|
+
* (and `EntryLink`), so a project sets up its RN primitives once instead of mapping every tag on every call.
|
|
20
7
|
*/
|
|
21
8
|
export function createReactNativeRenderer(primitives, styleOptions) {
|
|
22
9
|
const defaultComponents = buildReactNativeComponents(primitives, styleOptions);
|
|
23
10
|
async function compileMDX(source) {
|
|
24
|
-
|
|
11
|
+
const result = await compileMDXCore(source, runtime, defaultComponents);
|
|
12
|
+
if (!result.ok)
|
|
13
|
+
return result;
|
|
14
|
+
// MDX bakes the source's literal inter-block whitespace into the compiled output as bare
|
|
15
|
+
// "\n" string children of the root Fragment (e.g. between a heading and the paragraph after
|
|
16
|
+
// it). React Native throws on a bare string child that isn't inside a <Text>, so those need
|
|
17
|
+
// stripping here — element-mapped tags already handle it themselves (see reactNativeComponents.ts).
|
|
18
|
+
const Content = result.Content;
|
|
19
|
+
// withDefaultComponents (core.ts) wraps the compiled MDX component in one props-forwarding
|
|
20
|
+
// layer, so unwrap exactly that to reach the actual root element (a Fragment for a multi-block
|
|
21
|
+
// document, or a single mapped element like Text/View otherwise) before stripping whitespace.
|
|
22
|
+
// Calling any further would start invoking the RN primitives (Text/View/Image) themselves.
|
|
23
|
+
const RootWithoutWhitespace = (props) => {
|
|
24
|
+
const wrapped = Content(props);
|
|
25
|
+
const rootElement = typeof wrapped.type === "function"
|
|
26
|
+
? wrapped.type(wrapped.props)
|
|
27
|
+
: wrapped;
|
|
28
|
+
return stripWhitespaceRootChildren(rootElement);
|
|
29
|
+
};
|
|
30
|
+
return {
|
|
31
|
+
ok: true,
|
|
32
|
+
Content: RootWithoutWhitespace,
|
|
33
|
+
};
|
|
25
34
|
}
|
|
26
35
|
return { compileMDX };
|
|
27
36
|
}
|
|
37
|
+
function stripWhitespaceRootChildren(element) {
|
|
38
|
+
const children = element.props?.children;
|
|
39
|
+
if (!Array.isArray(children))
|
|
40
|
+
return element;
|
|
41
|
+
const filtered = children.filter((child) => typeof child !== "string" || child.trim() !== "");
|
|
42
|
+
if (filtered.length === children.length)
|
|
43
|
+
return element;
|
|
44
|
+
return runtime.jsxs(element.type, { ...element.props, children: filtered }, element.key ?? undefined);
|
|
45
|
+
}
|
package/dist/reactNative.test.js
CHANGED
|
@@ -6,9 +6,8 @@ const Text = () => null;
|
|
|
6
6
|
const View = () => null;
|
|
7
7
|
const Image = () => null;
|
|
8
8
|
const LEAVES = [Text, View, Image];
|
|
9
|
-
/** Unwraps nested function-component elements
|
|
10
|
-
*
|
|
11
|
-
* into the primitive itself (Text/View/Image are opaque leaves here, not JSX). */
|
|
9
|
+
/** Unwraps nested function-component elements down to the RN primitive actually used,
|
|
10
|
+
* without calling into it (Text/View/Image are opaque leaves here, not JSX). */
|
|
12
11
|
function resolve(element) {
|
|
13
12
|
return typeof element.type === "function" && !LEAVES.includes(element.type)
|
|
14
13
|
? resolve(element.type(element.props))
|
|
@@ -25,12 +25,8 @@ export interface ReactNativeStyleOptions {
|
|
|
25
25
|
styles?: Partial<Record<string, Record<string, unknown>>>;
|
|
26
26
|
}
|
|
27
27
|
/**
|
|
28
|
-
* Default MDX `components` map for React Native, built from its Text/View/Image
|
|
29
|
-
*
|
|
30
|
-
* does, so every standard markdown element needs an explicit mapping — this covers
|
|
31
|
-
* the common ones, styled to match the web default look (heading scale, link color,
|
|
32
|
-
* table layout, ...), so a project only wires up the three RN primitives once (via
|
|
33
|
-
* `createReactNativeRenderer`) instead of mapping and styling every tag itself.
|
|
28
|
+
* Default MDX `components` map for React Native, built from its Text/View/Image primitives. RN has no intrinsic
|
|
29
|
+
* host tags for `p`/`h1`/`a`/etc the way web React does, so every standard markdown element needs an explicit mapping.
|
|
34
30
|
*/
|
|
35
31
|
export declare function buildReactNativeComponents({ Text, View, Image }: ReactNativePrimitives, { unstyled, styles }?: ReactNativeStyleOptions): {
|
|
36
32
|
p: ComponentType<{
|
|
@@ -138,27 +134,18 @@ export declare function buildReactNativeComponents({ Text, View, Image }: ReactN
|
|
|
138
134
|
children?: ReactNode;
|
|
139
135
|
style?: unknown;
|
|
140
136
|
}) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>);
|
|
141
|
-
ul:
|
|
137
|
+
ul: (props: {
|
|
142
138
|
children?: ReactNode;
|
|
143
139
|
style?: unknown;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
style?: unknown;
|
|
147
|
-
}) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>);
|
|
148
|
-
ol: ComponentType<{
|
|
149
|
-
children?: ReactNode;
|
|
150
|
-
style?: unknown;
|
|
151
|
-
}> | ((props: {
|
|
152
|
-
children?: ReactNode;
|
|
153
|
-
style?: unknown;
|
|
154
|
-
}) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>);
|
|
155
|
-
blockquote: ComponentType<{
|
|
140
|
+
}) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
|
|
141
|
+
ol: (props: {
|
|
156
142
|
children?: ReactNode;
|
|
157
143
|
style?: unknown;
|
|
158
|
-
}
|
|
144
|
+
}) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
|
|
145
|
+
blockquote: (props: {
|
|
159
146
|
children?: ReactNode;
|
|
160
147
|
style?: unknown;
|
|
161
|
-
}) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any
|
|
148
|
+
}) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
|
|
162
149
|
hr: ComponentType<{
|
|
163
150
|
children?: ReactNode;
|
|
164
151
|
style?: unknown;
|
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
import * as runtime from "react/jsx-runtime";
|
|
2
2
|
import { defaultReactNativeStyles } from "./reactNativeStyles.js";
|
|
3
3
|
/**
|
|
4
|
-
* Default MDX `components` map for React Native, built from its Text/View/Image
|
|
5
|
-
*
|
|
6
|
-
* does, so every standard markdown element needs an explicit mapping — this covers
|
|
7
|
-
* the common ones, styled to match the web default look (heading scale, link color,
|
|
8
|
-
* table layout, ...), so a project only wires up the three RN primitives once (via
|
|
9
|
-
* `createReactNativeRenderer`) instead of mapping and styling every tag itself.
|
|
4
|
+
* Default MDX `components` map for React Native, built from its Text/View/Image primitives. RN has no intrinsic
|
|
5
|
+
* host tags for `p`/`h1`/`a`/etc the way web React does, so every standard markdown element needs an explicit mapping.
|
|
10
6
|
*/
|
|
11
7
|
export function buildReactNativeComponents({ Text, View, Image }, { unstyled, styles } = {}) {
|
|
12
8
|
const styleFor = (tag) => unstyled ? styles?.[tag] : { ...defaultReactNativeStyles[tag], ...styles?.[tag] };
|
|
@@ -16,6 +12,19 @@ export function buildReactNativeComponents({ Text, View, Image }, { unstyled, st
|
|
|
16
12
|
return Base;
|
|
17
13
|
return (props) => runtime.jsx(Base, { ...props, style: props.style ? [style, props.style] : style });
|
|
18
14
|
}
|
|
15
|
+
// MDX bakes the source's literal formatting whitespace ("\n" between block elements) into the
|
|
16
|
+
// compiled JSX as string children, even though structurally these tags never hold real text.
|
|
17
|
+
// React Native's Text/View don't tolerate a bare string child of View, so container tags need it
|
|
18
|
+
// stripped — Text-mapped tags (p, li, td, ...) keep it, since inline whitespace there is real content.
|
|
19
|
+
function stripWhitespaceChildren(children) {
|
|
20
|
+
if (Array.isArray(children))
|
|
21
|
+
return children.filter((child) => typeof child !== "string" || child.trim() !== "");
|
|
22
|
+
return typeof children === "string" && children.trim() === "" ? undefined : children;
|
|
23
|
+
}
|
|
24
|
+
function container(Base, tag) {
|
|
25
|
+
const Styled = styled(Base, tag);
|
|
26
|
+
return (props) => runtime.jsx(Styled, { ...props, children: stripWhitespaceChildren(props.children) });
|
|
27
|
+
}
|
|
19
28
|
const Img = ({ src, alt }) => runtime.jsx(Image, {
|
|
20
29
|
source: { uri: src },
|
|
21
30
|
accessibilityLabel: alt,
|
|
@@ -38,9 +47,9 @@ export function buildReactNativeComponents({ Text, View, Image }, { unstyled, st
|
|
|
38
47
|
li: styled(Text, "li"),
|
|
39
48
|
th: styled(Text, "th"),
|
|
40
49
|
td: styled(Text, "td"),
|
|
41
|
-
ul:
|
|
42
|
-
ol:
|
|
43
|
-
blockquote:
|
|
50
|
+
ul: container(View, "ul"),
|
|
51
|
+
ol: container(View, "ol"),
|
|
52
|
+
blockquote: container(View, "blockquote"),
|
|
44
53
|
hr: styled(View, "hr"),
|
|
45
54
|
table: styled(View, "table"),
|
|
46
55
|
thead: View,
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
2
|
import test from "node:test";
|
|
3
3
|
import { buildReactNativeComponents } from "./reactNativeComponents.js";
|
|
4
|
-
// Stand-ins for react-native's Text/View/Image — this package doesn't depend on
|
|
5
|
-
//
|
|
6
|
-
// logic is tested against fakes with the same shape.
|
|
4
|
+
// Stand-ins for react-native's Text/View/Image — this package doesn't depend on react-native itself
|
|
5
|
+
// (it only resolves inside Metro, not plain Node), so the mapping is tested against fakes with the same shape.
|
|
7
6
|
const Text = () => null;
|
|
8
7
|
const View = () => null;
|
|
9
8
|
const Image = () => null;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/** Default per-tag style objects for the React Native preset — mirrors `styles.css`'s
|
|
2
|
-
* web
|
|
3
|
-
* spacing) so RN output looks like the web default instead of unstyled plain text. */
|
|
1
|
+
/** Default per-tag style objects for the React Native preset — mirrors `styles.css`'s web defaults
|
|
2
|
+
* so RN output looks like the web default instead of unstyled plain text. */
|
|
4
3
|
export declare const defaultReactNativeStyles: Record<string, Record<string, unknown>>;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
/** Default per-tag style objects for the React Native preset — mirrors `styles.css`'s
|
|
2
|
-
* web
|
|
3
|
-
* spacing) so RN output looks like the web default instead of unstyled plain text. */
|
|
1
|
+
/** Default per-tag style objects for the React Native preset — mirrors `styles.css`'s web defaults
|
|
2
|
+
* so RN output looks like the web default instead of unstyled plain text. */
|
|
4
3
|
export const defaultReactNativeStyles = {
|
|
5
4
|
p: { marginBottom: 12, lineHeight: 22 },
|
|
6
5
|
h1: { fontSize: 28, fontWeight: "700", marginTop: 20, marginBottom: 10 },
|
package/dist/toHtml.d.ts
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
*/
|
|
7
|
-
|
|
1
|
+
/** Renders a custom JSX tag to an HTML string. `props` values are always strings (HTML parsing,
|
|
2
|
+
* not JSX). `childrenHtml` is the tag's contents, already rendered, nested tags resolved first. */
|
|
3
|
+
export type ToHtmlComponent = (props: Record<string, string>, childrenHtml: string) => string;
|
|
4
|
+
export interface ToHtmlOptions {
|
|
5
|
+
/** Renders custom tags (e.g. `<Callout>`, `<EntryLink id>`) by tag name, case-insensitive.
|
|
6
|
+
* `EntryLink` defaults to `<a href="/entries/{id}">` unless overridden. */
|
|
7
|
+
components?: Record<string, ToHtmlComponent>;
|
|
8
|
+
/** Adds `target`/`rel` to links whose `href` has a URL scheme (`https:`, `mailto:`, ...).
|
|
9
|
+
* `true` uses `target="_blank" rel="noopener noreferrer"`; pass an object to override. */
|
|
10
|
+
externalLinks?: boolean | {
|
|
11
|
+
target?: string;
|
|
12
|
+
rel?: string;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
/** Renders MDX/markdown to a static HTML string — no React, no mounted tree. Use
|
|
16
|
+
* `compileMDX`/`MDXContent` instead when you need a React tree. */
|
|
17
|
+
export declare function toHtml(source: string, options?: ToHtmlOptions): Promise<string>;
|
package/dist/toHtml.js
CHANGED
|
@@ -2,21 +2,73 @@ import { unified } from "unified";
|
|
|
2
2
|
import remarkParse from "remark-parse";
|
|
3
3
|
import remarkGfm from "remark-gfm";
|
|
4
4
|
import remarkRehype from "remark-rehype";
|
|
5
|
+
import rehypeRaw from "rehype-raw";
|
|
5
6
|
import rehypeSlug from "rehype-slug";
|
|
6
7
|
import rehypeStringify from "rehype-stringify";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
import { toHtml as hastToHtml } from "hast-util-to-html";
|
|
9
|
+
const SCHEME_HREF = /^[a-z][a-z0-9+.-]*:/i;
|
|
10
|
+
const defaultEntryLink = (props, childrenHtml) => `<a href="/entries/${props.id ?? ""}">${childrenHtml}</a>`;
|
|
11
|
+
function hastPropsToStrings(properties) {
|
|
12
|
+
const props = {};
|
|
13
|
+
for (const [key, value] of Object.entries(properties ?? {})) {
|
|
14
|
+
if (typeof value === "string")
|
|
15
|
+
props[key] = value;
|
|
16
|
+
else if (Array.isArray(value))
|
|
17
|
+
props[key] = value.join(" ");
|
|
18
|
+
}
|
|
19
|
+
return props;
|
|
20
|
+
}
|
|
21
|
+
function rehypeDraftbase(options) {
|
|
22
|
+
const componentsByTag = options.components
|
|
23
|
+
? new Map(Object.entries({ EntryLink: defaultEntryLink, ...options.components }).map(([name, render]) => [name.toLowerCase(), render]))
|
|
24
|
+
: undefined;
|
|
25
|
+
return (tree) => {
|
|
26
|
+
function transform(node) {
|
|
27
|
+
for (const child of node.children ?? [])
|
|
28
|
+
transform(child);
|
|
29
|
+
if (node.type !== "element" || !node.tagName)
|
|
30
|
+
return;
|
|
31
|
+
const render = componentsByTag?.get(node.tagName);
|
|
32
|
+
if (render) {
|
|
33
|
+
const childrenHtml = hastToHtml({ type: "root", children: node.children ?? [] }, {
|
|
34
|
+
allowDangerousHtml: true,
|
|
35
|
+
});
|
|
36
|
+
node.value = render(hastPropsToStrings(node.properties), childrenHtml);
|
|
37
|
+
node.type = "raw";
|
|
38
|
+
node.tagName = undefined;
|
|
39
|
+
node.properties = undefined;
|
|
40
|
+
node.children = undefined;
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (node.tagName === "a" && options.externalLinks) {
|
|
44
|
+
const href = node.properties?.href;
|
|
45
|
+
if (typeof href !== "string" || !SCHEME_HREF.test(href))
|
|
46
|
+
return;
|
|
47
|
+
const overrides = typeof options.externalLinks === "object" ? options.externalLinks : {};
|
|
48
|
+
node.properties = {
|
|
49
|
+
...node.properties,
|
|
50
|
+
target: overrides.target ?? "_blank",
|
|
51
|
+
rel: overrides.rel ?? "noopener noreferrer",
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
transform(tree);
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/** Renders MDX/markdown to a static HTML string — no React, no mounted tree. Use
|
|
59
|
+
* `compileMDX`/`MDXContent` instead when you need a React tree. */
|
|
60
|
+
export async function toHtml(source, options = {}) {
|
|
61
|
+
const needsRawParse = Boolean(options.components);
|
|
62
|
+
const processor = unified()
|
|
15
63
|
.use(remarkParse)
|
|
16
64
|
.use(remarkGfm)
|
|
17
|
-
.use(remarkRehype, { allowDangerousHtml: true })
|
|
65
|
+
.use(remarkRehype, { allowDangerousHtml: true });
|
|
66
|
+
if (needsRawParse)
|
|
67
|
+
processor.use(rehypeRaw);
|
|
68
|
+
processor
|
|
18
69
|
.use(rehypeSlug)
|
|
19
|
-
.use(
|
|
20
|
-
.
|
|
70
|
+
.use(rehypeDraftbase, options)
|
|
71
|
+
.use(rehypeStringify, { allowDangerousHtml: true });
|
|
72
|
+
const file = await processor.process(source);
|
|
21
73
|
return String(file);
|
|
22
74
|
}
|
package/dist/toHtml.test.js
CHANGED
|
@@ -10,3 +10,43 @@ test("supports gfm tables", async () => {
|
|
|
10
10
|
const html = await toHtml("| a | b |\n| - | - |\n| 1 | 2 |");
|
|
11
11
|
assert.match(html, /<table>/);
|
|
12
12
|
});
|
|
13
|
+
test("leaves custom tags as literal HTML when components isn't passed", async () => {
|
|
14
|
+
const html = await toHtml('<EntryLink id="abc">Post</EntryLink><Callout>hi</Callout>');
|
|
15
|
+
assert.match(html, /<EntryLink id="abc">Post<\/EntryLink>/);
|
|
16
|
+
assert.match(html, /<Callout>hi<\/Callout>/);
|
|
17
|
+
});
|
|
18
|
+
test("EntryLink defaults to /entries/{id} once components is passed", async () => {
|
|
19
|
+
const html = await toHtml('<EntryLink id="abc">Post</EntryLink>', { components: {} });
|
|
20
|
+
assert.match(html, /<a href="\/entries\/abc">Post<\/a>/);
|
|
21
|
+
});
|
|
22
|
+
test("components renders a custom tag, keyed case-insensitively, with string props", async () => {
|
|
23
|
+
const html = await toHtml('<Callout type="warning">Careful</Callout>', {
|
|
24
|
+
components: {
|
|
25
|
+
Callout: (props, childrenHtml) => `<div class="callout-${props.type}">${childrenHtml}</div>`,
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
assert.match(html, /<div class="callout-warning">Careful<\/div>/);
|
|
29
|
+
});
|
|
30
|
+
test("components overrides the default EntryLink", async () => {
|
|
31
|
+
const html = await toHtml('<EntryLink id="abc">Post</EntryLink>', {
|
|
32
|
+
components: {
|
|
33
|
+
EntryLink: (props, childrenHtml) => `<a href="/blog/${props.id}">${childrenHtml}</a>`,
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
assert.match(html, /<a href="\/blog\/abc">Post<\/a>/);
|
|
37
|
+
});
|
|
38
|
+
test("components resolves nested custom tags before serializing the parent's children", async () => {
|
|
39
|
+
const html = await toHtml('<Callout><EntryLink id="abc">Post</EntryLink></Callout>', {
|
|
40
|
+
components: {
|
|
41
|
+
Callout: (_props, childrenHtml) => `<div>${childrenHtml}</div>`,
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
assert.match(html, /<div><a href="\/entries\/abc">Post<\/a><\/div>/);
|
|
45
|
+
});
|
|
46
|
+
test("adds target/rel to external links when externalLinks is true", async () => {
|
|
47
|
+
const html = await toHtml("[ext](https://example.com) and [rel](/local)", {
|
|
48
|
+
externalLinks: true,
|
|
49
|
+
});
|
|
50
|
+
assert.match(html, /<a href="https:\/\/example\.com" target="_blank" rel="noopener noreferrer">ext<\/a>/);
|
|
51
|
+
assert.match(html, /<a href="\/local">rel<\/a>/);
|
|
52
|
+
});
|
package/dist/vue.d.ts
CHANGED
|
@@ -6,9 +6,7 @@ export interface CompiledMDX {
|
|
|
6
6
|
}
|
|
7
7
|
export type { FailedMDX };
|
|
8
8
|
/**
|
|
9
|
-
* Compiles raw MDX/markdown into a renderable Vue component. Vue has no RSC-style
|
|
10
|
-
*
|
|
11
|
-
* yourself: `h(Content, props)` or `<component :is="Content" />`. Standard markdown
|
|
12
|
-
* elements render via real DOM tags with zero setup.
|
|
9
|
+
* Compiles raw MDX/markdown into a renderable Vue component. Vue has no RSC-style async component, so call this
|
|
10
|
+
* from `setup()`/a composable and render the result yourself: `h(Content, props)`.
|
|
13
11
|
*/
|
|
14
12
|
export declare function compileMDX(source: string): Promise<CompiledMDX | FailedMDX>;
|
package/dist/vue.js
CHANGED
|
@@ -2,10 +2,8 @@ import { compileMDXCore, makeDefaultEntryLink } from "./core.js";
|
|
|
2
2
|
import { vueJsxRuntime } from "./vueRuntime.js";
|
|
3
3
|
const defaultComponents = { EntryLink: makeDefaultEntryLink(vueJsxRuntime) };
|
|
4
4
|
/**
|
|
5
|
-
* Compiles raw MDX/markdown into a renderable Vue component. Vue has no RSC-style
|
|
6
|
-
*
|
|
7
|
-
* yourself: `h(Content, props)` or `<component :is="Content" />`. Standard markdown
|
|
8
|
-
* elements render via real DOM tags with zero setup.
|
|
5
|
+
* Compiles raw MDX/markdown into a renderable Vue component. Vue has no RSC-style async component, so call this
|
|
6
|
+
* from `setup()`/a composable and render the result yourself: `h(Content, props)`.
|
|
9
7
|
*/
|
|
10
8
|
export async function compileMDX(source) {
|
|
11
9
|
return compileMDXCore(source, vueJsxRuntime, defaultComponents);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@draftbase/renderer",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.3",
|
|
4
4
|
"description": "Framework-agnostic MDX renderer for Draftbase content (React, Next.js RSC, React Native, Astro, Vite, Vue)",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"draftbase",
|
|
@@ -61,7 +61,9 @@
|
|
|
61
61
|
"remark-gfm": "^4.0.0",
|
|
62
62
|
"remark-parse": "^11.0.0",
|
|
63
63
|
"remark-rehype": "^11.1.2",
|
|
64
|
+
"rehype-raw": "^7.0.0",
|
|
64
65
|
"rehype-slug": "^6.0.0",
|
|
66
|
+
"hast-util-to-html": "^9.0.0",
|
|
65
67
|
"rehype-stringify": "^10.0.1",
|
|
66
68
|
"unified": "^11.0.5"
|
|
67
69
|
},
|