prosemirror-link-preview 2.0.6 → 2.0.7
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 +169 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1 +1,170 @@
|
|
|
1
1
|
# prosemirror-link-preview
|
|
2
|
+
|
|
3
|
+
## Features
|
|
4
|
+
|
|
5
|
+
The ProseMirror-Link-Preview plugin offers several key features that enhance the user experience while editing:
|
|
6
|
+
|
|
7
|
+
1. **Dynamic Link Previews**: Whenever a valid URL is pasted into a ProseMirror document, the plugin automatically calls **your callback** function, which is one of the plugin's parameter, which fetches the necessary metadata, and the plugin renders a preview, providing a quick glimpse into the content behind the link.
|
|
8
|
+
|
|
9
|
+
2. **Rich Preview Styles**: The link previews generated by the plugin are visually appealing, making it easier to differentiate between regular text and linked content. The preview includes information such as the title, description, and an image associated with the link, where available.
|
|
10
|
+
|
|
11
|
+
3. **Configurable Behavior**: The plugin provides configuration options, allowing users to customize the behavior and appearance of link previews according to their specific needs. From adjusting the preview size to defining custom CSS styles, the plugin offers flexibility to match the desired editing environment.
|
|
12
|
+
|
|
13
|
+
## How to use?
|
|
14
|
+
|
|
15
|
+
1. **Installation**: Install the plugin from your preferred package manager. For example, using npm, run the following command:
|
|
16
|
+
`npm i -S prosemirror-link-preview`
|
|
17
|
+
2. **Import**: Import the plugin into your project. You also need to import some utility functions from the plugin to help with the configuration process.
|
|
18
|
+
```typescript
|
|
19
|
+
import {
|
|
20
|
+
previewPlugin,
|
|
21
|
+
addPreviewNode,
|
|
22
|
+
apply, // for plain prosemirror
|
|
23
|
+
createDecorations, // for plain prosemirror
|
|
24
|
+
findPlaceholder, // for plain prosemirror
|
|
25
|
+
applyYjs, // for yjs users
|
|
26
|
+
createDecorationsYjs, // for yjs users
|
|
27
|
+
findPlaceholderYjs, // for yjs users
|
|
28
|
+
} from "prosemirror-link-preview";
|
|
29
|
+
```
|
|
30
|
+
3. Import the CSS file for your setup. You can use your custom css to style the preview, here is an example(which is the actual css used by default)
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
import "prosemirror-link-preview/dist/styles/styles.css";
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
```css
|
|
37
|
+
.preview-img {
|
|
38
|
+
width: 100%;
|
|
39
|
+
height: auto;
|
|
40
|
+
object-fit: cover;
|
|
41
|
+
object-position: center;
|
|
42
|
+
}
|
|
43
|
+
.preview-root {
|
|
44
|
+
border: 1px solid #ccc;
|
|
45
|
+
border-radius: 0.5rem;
|
|
46
|
+
width: 100%;
|
|
47
|
+
max-width: 20rem;
|
|
48
|
+
display: flex;
|
|
49
|
+
flex-direction: column;
|
|
50
|
+
gap: 0.5rem;
|
|
51
|
+
align-items: flex-start;
|
|
52
|
+
justify-content: center;
|
|
53
|
+
box-shadow: rgba(149, 157, 165, 0.2) 0px 8px 24px;
|
|
54
|
+
overflow: hidden;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
.preview-title {
|
|
58
|
+
font-size: 1.5rem;
|
|
59
|
+
font-weight: 700;
|
|
60
|
+
color: #333;
|
|
61
|
+
padding: 0.5rem;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
.preview-description {
|
|
65
|
+
font-size: 1rem;
|
|
66
|
+
font-weight: 500;
|
|
67
|
+
color: #333;
|
|
68
|
+
padding: 0.5rem 0.5rem 1rem 0.5rem;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
.placeholder {
|
|
72
|
+
width: 1rem;
|
|
73
|
+
height: 1rem;
|
|
74
|
+
background: darkgray;
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
4. Update the image node in the ProseMirror schema to have all the necessary properties with `addPreviewNode`
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
const mySchema = new Schema({
|
|
82
|
+
nodes: addPreviewNode(schema.spec.nodes),
|
|
83
|
+
marks: schema.spec.marks,
|
|
84
|
+
});
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
5. Initialize the editor with the plugin
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
const v = new EditorView(document.querySelector("#editor") as HTMLElement, {
|
|
91
|
+
state: EditorState.create({
|
|
92
|
+
doc: DOMParser.fromSchema(mySchema).parse(document.createElement("div")),
|
|
93
|
+
plugins: [
|
|
94
|
+
...exampleSetup({ schema: mySchema }),
|
|
95
|
+
ySyncPlugin(yXmlFragment),
|
|
96
|
+
yUndoPlugin(),
|
|
97
|
+
previewPlugin(
|
|
98
|
+
mySchema,
|
|
99
|
+
async (link: string) => {
|
|
100
|
+
const data = await fetch("/api/link-preview", {
|
|
101
|
+
method: "POST",
|
|
102
|
+
body: JSON.stringify({
|
|
103
|
+
link,
|
|
104
|
+
}),
|
|
105
|
+
});
|
|
106
|
+
const {
|
|
107
|
+
data: { url, title, description, images },
|
|
108
|
+
} = await data.json();
|
|
109
|
+
return { url, title, description, images };
|
|
110
|
+
},
|
|
111
|
+
applyYjs,
|
|
112
|
+
createDecorationsYjs,
|
|
113
|
+
findPlaceholderYjs
|
|
114
|
+
),
|
|
115
|
+
],
|
|
116
|
+
}),
|
|
117
|
+
});
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
6. `previewPlugin` requires 5 parameters:
|
|
121
|
+
|
|
122
|
+
- `schema`: the ProseMirror schema updated with `addPreviewNode`
|
|
123
|
+
- `fetchLinkPreview`: a function that takes a link and returns a `Promise` that resolves to the link preview data, you can easily do this using next.js API routes
|
|
124
|
+
or just using `link-preview-js` library on your custom backend
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
import type { NextApiRequest, NextApiResponse } from "next";
|
|
128
|
+
import Cors from "cors";
|
|
129
|
+
import { getLinkPreview } from "link-preview-js";
|
|
130
|
+
// Initializing the cors middleware
|
|
131
|
+
// You can read more about the available options here: https://github.com/expressjs/cors#configuration-options
|
|
132
|
+
const cors = Cors({
|
|
133
|
+
methods: ["POST", "GET", "HEAD"],
|
|
134
|
+
});
|
|
135
|
+
// Helper method to wait for a middleware to execute before continuing
|
|
136
|
+
// And to throw an error when an error happens in a middleware
|
|
137
|
+
function runMiddleware(
|
|
138
|
+
req: NextApiRequest,
|
|
139
|
+
res: NextApiResponse,
|
|
140
|
+
fn: Function
|
|
141
|
+
) {
|
|
142
|
+
return new Promise((resolve, reject) => {
|
|
143
|
+
fn(req, res, (result: any) => {
|
|
144
|
+
if (result instanceof Error) {
|
|
145
|
+
return reject(result);
|
|
146
|
+
}
|
|
147
|
+
return resolve(result);
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
export default async function handler(
|
|
152
|
+
req: NextApiRequest,
|
|
153
|
+
res: NextApiResponse
|
|
154
|
+
) {
|
|
155
|
+
// Run the middleware
|
|
156
|
+
await runMiddleware(req, res, cors);
|
|
157
|
+
|
|
158
|
+
const { link } = JSON.parse(req.body);
|
|
159
|
+
console.log({ link });
|
|
160
|
+
|
|
161
|
+
const data = await getLinkPreview(link);
|
|
162
|
+
|
|
163
|
+
// Rest of the API logic
|
|
164
|
+
res.json({ data });
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
- `apply`: import from `prosemirror-link-preview`
|
|
169
|
+
- `createDecorations`: import from `prosemirror-link-preview`
|
|
170
|
+
- `findPlaceholder`: import from `prosemirror-link-preview`
|