@datocms/svelte 1.2.1 → 1.2.2
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 +33 -6
- package/package.json +169 -24
- package/components/Head/Head.svelte +0 -14
- package/components/Head/Head.svelte.d.ts +0 -61
- package/components/Head/README.md +0 -68
- package/components/Head/__tests__/Head.test.d.ts +0 -1
- package/components/Head/__tests__/Head.test.js +0 -11
- package/components/Head/__tests__/__fixtures__/head.d.ts +0 -2
- package/components/Head/__tests__/__fixtures__/head.js +0 -280
- package/components/Head/__tests__/__snapshots__/Head.test.ts.snap +0 -221
- package/components/Image/Image.svelte +0 -172
- package/components/Image/Image.svelte.d.ts +0 -83
- package/components/Image/Placeholder.svelte +0 -30
- package/components/Image/Placeholder.svelte.d.ts +0 -22
- package/components/Image/README.md +0 -167
- package/components/Image/Sizer.svelte +0 -17
- package/components/Image/Sizer.svelte.d.ts +0 -19
- package/components/Image/Source.svelte +0 -6
- package/components/Image/Source.svelte.d.ts +0 -18
- package/components/Image/__tests__/Image.svelte.test.d.ts +0 -1
- package/components/Image/__tests__/Image.svelte.test.js +0 -44
- package/components/Image/__tests__/__fixtures__/image.d.ts +0 -40
- package/components/Image/__tests__/__fixtures__/image.js +0 -40
- package/components/Image/__tests__/__snapshots__/Image.svelte.test.ts.snap +0 -903
- package/components/StructuredText/Node.svelte +0 -112
- package/components/StructuredText/Node.svelte.d.ts +0 -22
- package/components/StructuredText/README.md +0 -201
- package/components/StructuredText/StructuredText.svelte +0 -15
- package/components/StructuredText/StructuredText.svelte.d.ts +0 -19
- package/components/StructuredText/__tests__/StructuredText.svelte.test.d.ts +0 -1
- package/components/StructuredText/__tests__/StructuredText.svelte.test.js +0 -171
- package/components/StructuredText/__tests__/__fixtures__/Block.svelte +0 -11
- package/components/StructuredText/__tests__/__fixtures__/Block.svelte.d.ts +0 -19
- package/components/StructuredText/__tests__/__fixtures__/CustomSpan.svelte +0 -49
- package/components/StructuredText/__tests__/__fixtures__/CustomSpan.svelte.d.ts +0 -19
- package/components/StructuredText/__tests__/__fixtures__/IncreasedLevelHeading.svelte +0 -8
- package/components/StructuredText/__tests__/__fixtures__/IncreasedLevelHeading.svelte.d.ts +0 -19
- package/components/StructuredText/__tests__/__fixtures__/InlineItem.svelte +0 -8
- package/components/StructuredText/__tests__/__fixtures__/InlineItem.svelte.d.ts +0 -19
- package/components/StructuredText/__tests__/__fixtures__/ItemLink.svelte +0 -15
- package/components/StructuredText/__tests__/__fixtures__/ItemLink.svelte.d.ts +0 -21
- package/components/StructuredText/__tests__/__fixtures__/structuredText.d.ts +0 -7
- package/components/StructuredText/__tests__/__fixtures__/structuredText.js +0 -575
- package/components/StructuredText/__tests__/__fixtures__/types.d.ts +0 -27
- package/components/StructuredText/__tests__/__fixtures__/types.js +0 -1
- package/components/StructuredText/__tests__/__snapshots__/StructuredText.svelte.test.ts.snap +0 -531
- package/components/StructuredText/nodes/Blockquote.svelte +0 -5
- package/components/StructuredText/nodes/Blockquote.svelte.d.ts +0 -19
- package/components/StructuredText/nodes/Code.svelte +0 -6
- package/components/StructuredText/nodes/Code.svelte.d.ts +0 -17
- package/components/StructuredText/nodes/Heading.svelte +0 -8
- package/components/StructuredText/nodes/Heading.svelte.d.ts +0 -19
- package/components/StructuredText/nodes/Link.svelte +0 -20
- package/components/StructuredText/nodes/Link.svelte.d.ts +0 -19
- package/components/StructuredText/nodes/List.svelte +0 -10
- package/components/StructuredText/nodes/List.svelte.d.ts +0 -19
- package/components/StructuredText/nodes/ListItem.svelte +0 -5
- package/components/StructuredText/nodes/ListItem.svelte.d.ts +0 -19
- package/components/StructuredText/nodes/Paragraph.svelte +0 -5
- package/components/StructuredText/nodes/Paragraph.svelte.d.ts +0 -19
- package/components/StructuredText/nodes/Root.svelte +0 -5
- package/components/StructuredText/nodes/Root.svelte.d.ts +0 -19
- package/components/StructuredText/nodes/Span.svelte +0 -49
- package/components/StructuredText/nodes/Span.svelte.d.ts +0 -19
- package/components/StructuredText/nodes/ThematicBreak.svelte +0 -5
- package/components/StructuredText/nodes/ThematicBreak.svelte.d.ts +0 -17
- package/components/StructuredText/utils/Lines.svelte +0 -11
- package/components/StructuredText/utils/Lines.svelte.d.ts +0 -16
- package/index.d.ts +0 -9
- package/index.js +0 -3
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
<script context="module">import {
|
|
2
|
-
isBlock,
|
|
3
|
-
isBlockquote,
|
|
4
|
-
isCode,
|
|
5
|
-
isHeading,
|
|
6
|
-
isInlineItem,
|
|
7
|
-
isItemLink,
|
|
8
|
-
isLink,
|
|
9
|
-
isList,
|
|
10
|
-
isListItem,
|
|
11
|
-
isParagraph,
|
|
12
|
-
isRoot,
|
|
13
|
-
isSpan,
|
|
14
|
-
isThematicBreak,
|
|
15
|
-
RenderError
|
|
16
|
-
} from "datocms-structured-text-utils";
|
|
17
|
-
import Paragraph from "./nodes/Paragraph.svelte";
|
|
18
|
-
import Root from "./nodes/Root.svelte";
|
|
19
|
-
import Span from "./nodes/Span.svelte";
|
|
20
|
-
import Link from "./nodes/Link.svelte";
|
|
21
|
-
import List from "./nodes/List.svelte";
|
|
22
|
-
import Heading from "./nodes/Heading.svelte";
|
|
23
|
-
import Blockquote from "./nodes/Blockquote.svelte";
|
|
24
|
-
import ListItem from "./nodes/ListItem.svelte";
|
|
25
|
-
import ThematicBreak from "./nodes/ThematicBreak.svelte";
|
|
26
|
-
import Code from "./nodes/Code.svelte";
|
|
27
|
-
export const DEFAULT_COMPONENTS = [
|
|
28
|
-
[isParagraph, Paragraph],
|
|
29
|
-
[isRoot, Root],
|
|
30
|
-
[isSpan, Span],
|
|
31
|
-
[isLink, Link],
|
|
32
|
-
[isList, List],
|
|
33
|
-
[isHeading, Heading],
|
|
34
|
-
[isBlockquote, Blockquote],
|
|
35
|
-
[isListItem, ListItem],
|
|
36
|
-
[isThematicBreak, ThematicBreak],
|
|
37
|
-
[isCode, Code]
|
|
38
|
-
];
|
|
39
|
-
const throwRenderErrorForMissingComponent = (node) => {
|
|
40
|
-
if (isInlineItem(node)) {
|
|
41
|
-
throw new RenderError(
|
|
42
|
-
`The Structured Text document contains an 'inlineItem' node, but no component for rendering is specified!`,
|
|
43
|
-
node
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
if (isItemLink(node)) {
|
|
47
|
-
throw new RenderError(
|
|
48
|
-
`The Structured Text document contains an 'itemLink' node, but no component for rendering is specified!`,
|
|
49
|
-
node
|
|
50
|
-
);
|
|
51
|
-
}
|
|
52
|
-
if (isBlock(node)) {
|
|
53
|
-
throw new RenderError(
|
|
54
|
-
`The Structured Text document contains a 'block' node, but no component for rendering is specified!`,
|
|
55
|
-
node
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
const throwRenderErrorForMissingBlock = (node) => {
|
|
60
|
-
throw new RenderError(
|
|
61
|
-
`The Structured Text document contains a 'block' node, but cannot find a record with ID ${node.item} inside data.blocks!`,
|
|
62
|
-
node
|
|
63
|
-
);
|
|
64
|
-
};
|
|
65
|
-
const throwRenderErrorForMissingLink = (node) => {
|
|
66
|
-
throw new RenderError(
|
|
67
|
-
`The Structured Text document contains an 'itemLink' node, but cannot find a record with ID ${node.item} inside data.links!`,
|
|
68
|
-
node
|
|
69
|
-
);
|
|
70
|
-
};
|
|
71
|
-
const findBlock = (node, blocks) => (blocks || []).find(({ id }) => id === node.item);
|
|
72
|
-
const findLink = (node, links) => (links || []).find(({ id }) => id === node.item);
|
|
73
|
-
</script>
|
|
74
|
-
|
|
75
|
-
<script>import { hasChildren } from "datocms-structured-text-utils";
|
|
76
|
-
export let node;
|
|
77
|
-
export let blocks;
|
|
78
|
-
export let links;
|
|
79
|
-
export let components = [];
|
|
80
|
-
$:
|
|
81
|
-
block = isBlock(node) && (findBlock(node, blocks) || throwRenderErrorForMissingBlock(node));
|
|
82
|
-
$:
|
|
83
|
-
link = isItemLink(node) && (findLink(node, links) || throwRenderErrorForMissingLink(node)) || isInlineItem(node) && (findLink(node, links) || throwRenderErrorForMissingLink(node));
|
|
84
|
-
$:
|
|
85
|
-
predicateComponentTuple = [...components, ...DEFAULT_COMPONENTS].find(([predicate, component2]) => predicate(node)) || throwRenderErrorForMissingComponent(node);
|
|
86
|
-
$:
|
|
87
|
-
component = (predicateComponentTuple ?? [])[1];
|
|
88
|
-
</script>
|
|
89
|
-
|
|
90
|
-
{#if component}
|
|
91
|
-
{#if isBlock(node)}
|
|
92
|
-
<svelte:component this={component} {node} {block} />
|
|
93
|
-
{:else if isInlineItem(node)}
|
|
94
|
-
<svelte:component this={component} {node} {link} />
|
|
95
|
-
{:else if isItemLink(node)}
|
|
96
|
-
<svelte:component this={component} {node} {link}>
|
|
97
|
-
{#if hasChildren(node)}
|
|
98
|
-
{#each node.children as child}
|
|
99
|
-
<svelte:self node={child} {blocks} {links} {components} />
|
|
100
|
-
{/each}
|
|
101
|
-
{/if}
|
|
102
|
-
</svelte:component>
|
|
103
|
-
{:else}
|
|
104
|
-
<svelte:component this={component} {node}>
|
|
105
|
-
{#if hasChildren(node)}
|
|
106
|
-
{#each node.children as child}
|
|
107
|
-
<svelte:self node={child} {blocks} {links} {components} />
|
|
108
|
-
{/each}
|
|
109
|
-
{/if}
|
|
110
|
-
</svelte:component>
|
|
111
|
-
{/if}
|
|
112
|
-
{/if}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { SvelteComponentTyped } from "svelte";
|
|
2
|
-
export declare const DEFAULT_COMPONENTS: PredicateComponentTuple[];
|
|
3
|
-
import { type Node, type StructuredText } from 'datocms-structured-text-utils';
|
|
4
|
-
import type { PredicateComponentTuple } from '../..';
|
|
5
|
-
declare const __propDef: {
|
|
6
|
-
props: {
|
|
7
|
-
node: Node;
|
|
8
|
-
blocks: StructuredText['blocks'];
|
|
9
|
-
links: StructuredText['links'];
|
|
10
|
-
components?: PredicateComponentTuple[] | undefined;
|
|
11
|
-
};
|
|
12
|
-
events: {
|
|
13
|
-
[evt: string]: CustomEvent<any>;
|
|
14
|
-
};
|
|
15
|
-
slots: {};
|
|
16
|
-
};
|
|
17
|
-
export type NodeProps = typeof __propDef.props;
|
|
18
|
-
export type NodeEvents = typeof __propDef.events;
|
|
19
|
-
export type NodeSlots = typeof __propDef.slots;
|
|
20
|
-
export default class Node extends SvelteComponentTyped<NodeProps, NodeEvents, NodeSlots> {
|
|
21
|
-
}
|
|
22
|
-
export {};
|
|
@@ -1,201 +0,0 @@
|
|
|
1
|
-
# Structured text
|
|
2
|
-
|
|
3
|
-
`StructuredText />` is a Svelte component that you can use to render the value contained inside a DatoCMS [Structured Text field type](https://www.datocms.com/docs/structured-text/dast).
|
|
4
|
-
|
|
5
|
-
### Table of contents
|
|
6
|
-
|
|
7
|
-
- [Setup](#setup)
|
|
8
|
-
- [Basic usage](#basic-usage)
|
|
9
|
-
- [Custom renderers](#custom-renderers)
|
|
10
|
-
- [Props](#props)
|
|
11
|
-
|
|
12
|
-
### Setup
|
|
13
|
-
|
|
14
|
-
Import the component like this:
|
|
15
|
-
|
|
16
|
-
```js
|
|
17
|
-
import { StructuredText } from '@datocms/svelte';
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
## Basic usage
|
|
21
|
-
|
|
22
|
-
```svelte
|
|
23
|
-
<script>
|
|
24
|
-
|
|
25
|
-
import { onMount } from 'svelte';
|
|
26
|
-
|
|
27
|
-
import { StructuredText } from '@datocms/svelte';
|
|
28
|
-
|
|
29
|
-
const query = `
|
|
30
|
-
query {
|
|
31
|
-
blogPost {
|
|
32
|
-
title
|
|
33
|
-
content {
|
|
34
|
-
value
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
`;
|
|
39
|
-
|
|
40
|
-
export let data = null;
|
|
41
|
-
|
|
42
|
-
onMount(async () => {
|
|
43
|
-
const response = await fetch('https://graphql.datocms.com/', {
|
|
44
|
-
method: 'POST',
|
|
45
|
-
headers: {
|
|
46
|
-
'Content-Type': 'application/json',
|
|
47
|
-
Authorization: "Bearer faeb9172e232a75339242faafb9e56de8c8f13b735f7090964",
|
|
48
|
-
},
|
|
49
|
-
body: JSON.stringify({ query })
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
const json = await response.json()
|
|
53
|
-
|
|
54
|
-
data = json.data;
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
</script>
|
|
58
|
-
|
|
59
|
-
<article>
|
|
60
|
-
{#if data}
|
|
61
|
-
<h1>{{ data.blogPost.title }}</h1>
|
|
62
|
-
<StructuredText data={data.blogPost.content} />
|
|
63
|
-
{/if}
|
|
64
|
-
</article>
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
## Customization
|
|
68
|
-
|
|
69
|
-
The `<StructuredText />` component comes with a set of default components that are use to render all the nodes present in [DatoCMS Dast trees](https://www.datocms.com/docs/structured-text/dast). These default components are enough to cover most of the simple cases.
|
|
70
|
-
|
|
71
|
-
You need to use custom components in the following cases:
|
|
72
|
-
|
|
73
|
-
- you have to render blocks, inline items or item links: there's no conventional way of rendering theses nodes, so you must create and pass custom components;
|
|
74
|
-
- you need to render a conventional node differently (e.g. you may want a custom render for blockquotes)
|
|
75
|
-
|
|
76
|
-
### Custom components for blocks
|
|
77
|
-
|
|
78
|
-
Here is an example using custom components for blocks, inline and item links. Take a look at the [test fixtures](https://github.com/datocms/datocms-svelte/tree/main/src/lib/components/StructuredText/__tests__/__fixtures__) to see examples on how to implement these components.
|
|
79
|
-
|
|
80
|
-
```svelte
|
|
81
|
-
<script>
|
|
82
|
-
|
|
83
|
-
import { onMount } from 'svelte';
|
|
84
|
-
|
|
85
|
-
import { isBlock, isInlineItem, isItemLink } from 'datocms-structured-text-utils';
|
|
86
|
-
|
|
87
|
-
import { StructuredText } from '@datocms/svelte';
|
|
88
|
-
|
|
89
|
-
import Block from './Block.svelte';
|
|
90
|
-
import InlineItem from './InlineItem.svelte';
|
|
91
|
-
import ItemLink from './ItemLink.svelte';
|
|
92
|
-
|
|
93
|
-
const query = `
|
|
94
|
-
query {
|
|
95
|
-
blogPost {
|
|
96
|
-
title
|
|
97
|
-
content {
|
|
98
|
-
value
|
|
99
|
-
links {
|
|
100
|
-
__typename
|
|
101
|
-
... on TeamMemberRecord {
|
|
102
|
-
id
|
|
103
|
-
firstName
|
|
104
|
-
slug
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
blocks {
|
|
108
|
-
__typename
|
|
109
|
-
... on ImageRecord {
|
|
110
|
-
id
|
|
111
|
-
image {
|
|
112
|
-
responsiveImage(
|
|
113
|
-
imgixParams: { fit: crop, w: 300, h: 300, auto: format }
|
|
114
|
-
) {
|
|
115
|
-
srcSet
|
|
116
|
-
webpSrcSet
|
|
117
|
-
sizes
|
|
118
|
-
src
|
|
119
|
-
width
|
|
120
|
-
height
|
|
121
|
-
aspectRatio
|
|
122
|
-
alt
|
|
123
|
-
title
|
|
124
|
-
base64
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
`;
|
|
133
|
-
|
|
134
|
-
export let data = null;
|
|
135
|
-
|
|
136
|
-
onMount(async () => {
|
|
137
|
-
const response = await fetch('https://graphql.datocms.com/', {
|
|
138
|
-
method: 'POST',
|
|
139
|
-
headers: {
|
|
140
|
-
'Content-Type': 'application/json',
|
|
141
|
-
Authorization: "Bearer faeb9172e232a75339242faafb9e56de8c8f13b735f7090964",
|
|
142
|
-
},
|
|
143
|
-
body: JSON.stringify({ query })
|
|
144
|
-
})
|
|
145
|
-
|
|
146
|
-
const json = await response.json()
|
|
147
|
-
|
|
148
|
-
data = json.data;
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
</script>
|
|
152
|
-
|
|
153
|
-
<article>
|
|
154
|
-
{#if data}
|
|
155
|
-
<h1>{{ data.blogPost.title }}</h1>
|
|
156
|
-
<datocms-structured-text
|
|
157
|
-
data={data.blogPost.content}
|
|
158
|
-
components={[
|
|
159
|
-
[isInlineItem, InlineItem],
|
|
160
|
-
[isItemLink, ItemLink],
|
|
161
|
-
[isBlock, Block]
|
|
162
|
-
]}
|
|
163
|
-
/>
|
|
164
|
-
{/if}
|
|
165
|
-
</article>
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
### Override default rendering of nodes
|
|
169
|
-
|
|
170
|
-
`<StructuredText />` automatically renders all nodes (except for `inline_item`, `item_link` and `block`) using a set of default components, that you might want to customize. For example:
|
|
171
|
-
|
|
172
|
-
- For `heading` nodes, you might want to add an anchor;
|
|
173
|
-
- For `code` nodes, you might want to use a custom syntax highlighting component;
|
|
174
|
-
|
|
175
|
-
In this case, you can easily override default rendering rules with the `components` props. See test fixtures for example implementations of custom components (e.g. [this special heading component](https://github.com/datocms/datocms-svelte/blob/main/src/lib/components/StructuredText/__tests__/__fixtures__/IncreasedLevelHeading.svelte)).
|
|
176
|
-
|
|
177
|
-
```svelte
|
|
178
|
-
<script>
|
|
179
|
-
import { isHeading, isCode } from 'datocms-structured-text-utils';
|
|
180
|
-
|
|
181
|
-
import Heading from './Heading.svelte';
|
|
182
|
-
import Code from './Code.svelte';
|
|
183
|
-
|
|
184
|
-
export let data;
|
|
185
|
-
</script>
|
|
186
|
-
|
|
187
|
-
<StructuredText
|
|
188
|
-
data={data.blogPost.content}
|
|
189
|
-
components={[
|
|
190
|
-
[isHeading, Heading],
|
|
191
|
-
[isCode, Code]
|
|
192
|
-
]}
|
|
193
|
-
/>
|
|
194
|
-
```
|
|
195
|
-
|
|
196
|
-
## Props
|
|
197
|
-
|
|
198
|
-
| prop | type | required | description | default |
|
|
199
|
-
| ---------- | ----------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------ | ------- |
|
|
200
|
-
| data | `StructuredText \| DastNode` | :white_check_mark: | The actual [field value](https://www.datocms.com/docs/structured-text/dast) you get from DatoCMS | |
|
|
201
|
-
| components | [`PredicateComponentTuple[] \| null`](https://github.com/datocms/datocms-svelte/blob/main/src/lib/index.ts) | Only required if data contain `block`, `inline_item` or `item_link` nodes | Array of tuples formed by a predicate function and custom component | `[]` |
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
<script>import { isStructuredText } from "datocms-structured-text-utils";
|
|
2
|
-
import Node from "./Node.svelte";
|
|
3
|
-
export let data = null;
|
|
4
|
-
export let components = [];
|
|
5
|
-
$:
|
|
6
|
-
node = data != null && (isStructuredText(data) ? data.value : data).document;
|
|
7
|
-
$:
|
|
8
|
-
blocks = isStructuredText(data) ? data?.blocks : void 0;
|
|
9
|
-
$:
|
|
10
|
-
links = isStructuredText(data) ? data?.links : void 0;
|
|
11
|
-
</script>
|
|
12
|
-
|
|
13
|
-
{#if node}
|
|
14
|
-
<Node {node} {blocks} {links} {components} />
|
|
15
|
-
{/if}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { SvelteComponentTyped } from "svelte";
|
|
2
|
-
import { type StructuredText, type Document } from 'datocms-structured-text-utils';
|
|
3
|
-
import type { PredicateComponentTuple } from '../..';
|
|
4
|
-
declare const __propDef: {
|
|
5
|
-
props: {
|
|
6
|
-
/** The actual field value you get from DatoCMS **/ data?: StructuredText<import("datocms-structured-text-utils").Record, import("datocms-structured-text-utils").Record> | Document | null | undefined;
|
|
7
|
-
components?: PredicateComponentTuple[] | undefined;
|
|
8
|
-
};
|
|
9
|
-
events: {
|
|
10
|
-
[evt: string]: CustomEvent<any>;
|
|
11
|
-
};
|
|
12
|
-
slots: {};
|
|
13
|
-
};
|
|
14
|
-
export type StructuredTextProps = typeof __propDef.props;
|
|
15
|
-
export type StructuredTextEvents = typeof __propDef.events;
|
|
16
|
-
export type StructuredTextSlots = typeof __propDef.slots;
|
|
17
|
-
export default class StructuredText extends SvelteComponentTyped<StructuredTextProps, StructuredTextEvents, StructuredTextSlots> {
|
|
18
|
-
}
|
|
19
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import '@testing-library/jest-dom';
|
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import '@testing-library/jest-dom';
|
|
3
|
-
import { render } from '@testing-library/svelte';
|
|
4
|
-
import { isBlock, isHeading, isInlineItem, isItemLink, isSpan } from 'datocms-structured-text-utils';
|
|
5
|
-
import { StructuredText } from '../../..';
|
|
6
|
-
import { heading, paragraphWithLink, structuredTextWithBlocksAndLinks } from './__fixtures__/structuredText';
|
|
7
|
-
import CustomSpan from './__fixtures__/CustomSpan.svelte';
|
|
8
|
-
import IncreasedLevelHeading from './__fixtures__/IncreasedLevelHeading.svelte';
|
|
9
|
-
import ItemLink from './__fixtures__/ItemLink.svelte';
|
|
10
|
-
import Block from './__fixtures__/Block.svelte';
|
|
11
|
-
import InlineItem from './__fixtures__/InlineItem.svelte';
|
|
12
|
-
describe('StructuredText', () => {
|
|
13
|
-
describe('with no value', () => {
|
|
14
|
-
it('renders null', () => {
|
|
15
|
-
const { container } = render(StructuredText, { props: { data: null } });
|
|
16
|
-
expect(container).toMatchSnapshot();
|
|
17
|
-
});
|
|
18
|
-
});
|
|
19
|
-
describe('with a very simple pure dast (only the `value` of a structured text)', () => {
|
|
20
|
-
describe('with default rules', () => {
|
|
21
|
-
it('renders the document', () => {
|
|
22
|
-
const { container } = render(StructuredText, { props: { data: heading.value } });
|
|
23
|
-
expect(container).toMatchSnapshot();
|
|
24
|
-
});
|
|
25
|
-
});
|
|
26
|
-
describe('with custom mark rules', () => {
|
|
27
|
-
it('renders the document', () => {
|
|
28
|
-
const { container } = render(StructuredText, {
|
|
29
|
-
props: { data: heading, components: [[isSpan, CustomSpan]] }
|
|
30
|
-
});
|
|
31
|
-
expect(container).toMatchSnapshot();
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
describe('with a very simple dast', () => {
|
|
36
|
-
describe('with default rules', () => {
|
|
37
|
-
it('renders the document', () => {
|
|
38
|
-
const { container } = render(StructuredText, { props: { data: heading } });
|
|
39
|
-
expect(container).toMatchSnapshot();
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
describe('with custom mark rules', () => {
|
|
43
|
-
it('renders the document', () => {
|
|
44
|
-
const { container } = render(StructuredText, {
|
|
45
|
-
props: { data: heading, components: [[isSpan, CustomSpan]] }
|
|
46
|
-
});
|
|
47
|
-
expect(container).toMatchSnapshot();
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
describe('with a dast which has a link inside', () => {
|
|
52
|
-
describe('with default rules', () => {
|
|
53
|
-
it('renders the document', () => {
|
|
54
|
-
const { container } = render(StructuredText, { props: { data: paragraphWithLink } });
|
|
55
|
-
expect(container).toMatchSnapshot();
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
});
|
|
59
|
-
describe('with a dast with no links nor blocks', () => {
|
|
60
|
-
describe('with default rules', () => {
|
|
61
|
-
it('renders the document', () => {
|
|
62
|
-
const { container } = render(StructuredText, {
|
|
63
|
-
props: { data: heading }
|
|
64
|
-
});
|
|
65
|
-
expect(container).toMatchSnapshot();
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
describe('with custom rules', () => {
|
|
69
|
-
it('renders the document', () => {
|
|
70
|
-
const { container } = render(StructuredText, {
|
|
71
|
-
props: { data: heading, components: [[isHeading, IncreasedLevelHeading]] }
|
|
72
|
-
});
|
|
73
|
-
expect(container).toMatchSnapshot();
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
describe('with a dast including links and blocks', () => {
|
|
78
|
-
describe('with default rules', () => {
|
|
79
|
-
it('renders the document', () => {
|
|
80
|
-
const { container } = render(StructuredText, {
|
|
81
|
-
props: {
|
|
82
|
-
data: structuredTextWithBlocksAndLinks,
|
|
83
|
-
components: [
|
|
84
|
-
[isInlineItem, InlineItem],
|
|
85
|
-
[isItemLink, ItemLink],
|
|
86
|
-
[isBlock, Block]
|
|
87
|
-
]
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
expect(container).toMatchSnapshot();
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
describe('with missing component for item links', () => {
|
|
94
|
-
it('raises an error', () => {
|
|
95
|
-
expect(() => {
|
|
96
|
-
render(StructuredText, {
|
|
97
|
-
props: {
|
|
98
|
-
data: structuredTextWithBlocksAndLinks,
|
|
99
|
-
components: [
|
|
100
|
-
[isBlock, Block],
|
|
101
|
-
[isInlineItem, InlineItem]
|
|
102
|
-
]
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
}).toThrowErrorMatchingSnapshot();
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
describe('with missing component for inline items', () => {
|
|
109
|
-
it('raises an error', () => {
|
|
110
|
-
expect(() => {
|
|
111
|
-
render(StructuredText, {
|
|
112
|
-
props: {
|
|
113
|
-
data: structuredTextWithBlocksAndLinks,
|
|
114
|
-
components: [
|
|
115
|
-
[isBlock, Block],
|
|
116
|
-
[isItemLink, ItemLink]
|
|
117
|
-
]
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
}).toThrowErrorMatchingSnapshot();
|
|
121
|
-
});
|
|
122
|
-
});
|
|
123
|
-
describe('with missing links', () => {
|
|
124
|
-
it('raises an error', () => {
|
|
125
|
-
expect(() => {
|
|
126
|
-
render(StructuredText, {
|
|
127
|
-
props: {
|
|
128
|
-
data: { ...structuredTextWithBlocksAndLinks, links: [] },
|
|
129
|
-
components: [
|
|
130
|
-
[isBlock, Block],
|
|
131
|
-
[isInlineItem, InlineItem],
|
|
132
|
-
[isItemLink, ItemLink]
|
|
133
|
-
]
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
}).toThrowErrorMatchingSnapshot();
|
|
137
|
-
});
|
|
138
|
-
});
|
|
139
|
-
describe('with missing component for blocks', () => {
|
|
140
|
-
it('raises an error', () => {
|
|
141
|
-
expect(() => {
|
|
142
|
-
render(StructuredText, {
|
|
143
|
-
props: {
|
|
144
|
-
data: structuredTextWithBlocksAndLinks,
|
|
145
|
-
components: [
|
|
146
|
-
[isInlineItem, InlineItem],
|
|
147
|
-
[isItemLink, ItemLink]
|
|
148
|
-
]
|
|
149
|
-
}
|
|
150
|
-
});
|
|
151
|
-
}).toThrowErrorMatchingSnapshot();
|
|
152
|
-
});
|
|
153
|
-
});
|
|
154
|
-
describe('with missing blocks', () => {
|
|
155
|
-
it('raises an error', () => {
|
|
156
|
-
expect(() => {
|
|
157
|
-
render(StructuredText, {
|
|
158
|
-
props: {
|
|
159
|
-
data: { ...structuredTextWithBlocksAndLinks, blocks: [] },
|
|
160
|
-
components: [
|
|
161
|
-
[isBlock, Block],
|
|
162
|
-
[isInlineItem, InlineItem],
|
|
163
|
-
[isItemLink, ItemLink]
|
|
164
|
-
]
|
|
165
|
-
}
|
|
166
|
-
});
|
|
167
|
-
}).toThrowErrorMatchingSnapshot();
|
|
168
|
-
});
|
|
169
|
-
});
|
|
170
|
-
});
|
|
171
|
-
});
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
<script>import { isBlock } from "datocms-structured-text-utils";
|
|
2
|
-
export let node;
|
|
3
|
-
export let block;
|
|
4
|
-
</script>
|
|
5
|
-
|
|
6
|
-
{#if block.__typename == 'QuoteRecord' && isBlock(node)}
|
|
7
|
-
<figure>
|
|
8
|
-
<blockquote>{block.quote}</blockquote>
|
|
9
|
-
<figcaption>{block.author}</figcaption>
|
|
10
|
-
</figure>
|
|
11
|
-
{/if}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { SvelteComponentTyped } from "svelte";
|
|
2
|
-
import { type Block } from 'datocms-structured-text-utils';
|
|
3
|
-
import type { QuoteRecord } from './types';
|
|
4
|
-
declare const __propDef: {
|
|
5
|
-
props: {
|
|
6
|
-
node: Block;
|
|
7
|
-
block: QuoteRecord;
|
|
8
|
-
};
|
|
9
|
-
events: {
|
|
10
|
-
[evt: string]: CustomEvent<any>;
|
|
11
|
-
};
|
|
12
|
-
slots: {};
|
|
13
|
-
};
|
|
14
|
-
export type BlockProps = typeof __propDef.props;
|
|
15
|
-
export type BlockEvents = typeof __propDef.events;
|
|
16
|
-
export type BlockSlots = typeof __propDef.slots;
|
|
17
|
-
export default class Block extends SvelteComponentTyped<BlockProps, BlockEvents, BlockSlots> {
|
|
18
|
-
}
|
|
19
|
-
export {};
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
<script>import Lines from "../../utils/Lines.svelte";
|
|
2
|
-
export let node;
|
|
3
|
-
$:
|
|
4
|
-
({ type, value, marks } = node);
|
|
5
|
-
$:
|
|
6
|
-
[mark, ...otherMarks] = marks ?? [];
|
|
7
|
-
</script>
|
|
8
|
-
|
|
9
|
-
{#if mark}
|
|
10
|
-
{#if mark === 'emphasis'}
|
|
11
|
-
<em>
|
|
12
|
-
<svelte:self node={{ type, value, marks: otherMarks }}>
|
|
13
|
-
<slot />
|
|
14
|
-
</svelte:self>
|
|
15
|
-
</em>
|
|
16
|
-
{:else if mark === 'highlight'}
|
|
17
|
-
<mark>
|
|
18
|
-
<svelte:self node={{ type, value, marks: otherMarks }}>
|
|
19
|
-
<slot />
|
|
20
|
-
</svelte:self>
|
|
21
|
-
</mark>
|
|
22
|
-
{:else if mark === 'strikethrough'}
|
|
23
|
-
<del>
|
|
24
|
-
<svelte:self node={{ type, value, marks: otherMarks }}>
|
|
25
|
-
<slot />
|
|
26
|
-
</svelte:self>
|
|
27
|
-
</del>
|
|
28
|
-
{:else if mark === 'strong'}
|
|
29
|
-
<b>
|
|
30
|
-
<svelte:self node={{ type, value, marks: otherMarks }}>
|
|
31
|
-
<slot />
|
|
32
|
-
</svelte:self>
|
|
33
|
-
</b>
|
|
34
|
-
{:else if mark === 'underline'}
|
|
35
|
-
<u>
|
|
36
|
-
<svelte:self node={{ type, value, marks: otherMarks }}>
|
|
37
|
-
<slot />
|
|
38
|
-
</svelte:self>
|
|
39
|
-
</u>
|
|
40
|
-
{:else if mark === 'code'}
|
|
41
|
-
<pre>
|
|
42
|
-
<svelte:self node={{ type, value, marks: otherMarks }}>
|
|
43
|
-
<slot />
|
|
44
|
-
</svelte:self>
|
|
45
|
-
</pre>
|
|
46
|
-
{/if}
|
|
47
|
-
{:else}
|
|
48
|
-
<Lines lines={node.value.split(/\n/)} />
|
|
49
|
-
{/if}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { SvelteComponentTyped } from "svelte";
|
|
2
|
-
import type { Span } from 'datocms-structured-text-utils';
|
|
3
|
-
declare const __propDef: {
|
|
4
|
-
props: {
|
|
5
|
-
node: Span;
|
|
6
|
-
};
|
|
7
|
-
events: {
|
|
8
|
-
[evt: string]: CustomEvent<any>;
|
|
9
|
-
};
|
|
10
|
-
slots: {
|
|
11
|
-
default: {};
|
|
12
|
-
};
|
|
13
|
-
};
|
|
14
|
-
export type CustomSpanProps = typeof __propDef.props;
|
|
15
|
-
export type CustomSpanEvents = typeof __propDef.events;
|
|
16
|
-
export type CustomSpanSlots = typeof __propDef.slots;
|
|
17
|
-
export default class CustomSpan extends SvelteComponentTyped<CustomSpanProps, CustomSpanEvents, CustomSpanSlots> {
|
|
18
|
-
}
|
|
19
|
-
export {};
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { SvelteComponentTyped } from "svelte";
|
|
2
|
-
import type { Heading } from 'datocms-structured-text-utils';
|
|
3
|
-
declare const __propDef: {
|
|
4
|
-
props: {
|
|
5
|
-
node: Heading;
|
|
6
|
-
};
|
|
7
|
-
events: {
|
|
8
|
-
[evt: string]: CustomEvent<any>;
|
|
9
|
-
};
|
|
10
|
-
slots: {
|
|
11
|
-
default: {};
|
|
12
|
-
};
|
|
13
|
-
};
|
|
14
|
-
export type IncreasedLevelHeadingProps = typeof __propDef.props;
|
|
15
|
-
export type IncreasedLevelHeadingEvents = typeof __propDef.events;
|
|
16
|
-
export type IncreasedLevelHeadingSlots = typeof __propDef.slots;
|
|
17
|
-
export default class IncreasedLevelHeading extends SvelteComponentTyped<IncreasedLevelHeadingProps, IncreasedLevelHeadingEvents, IncreasedLevelHeadingSlots> {
|
|
18
|
-
}
|
|
19
|
-
export {};
|