tiptap-link-card
Version:
Link Card Block for TipTap Editor
101 lines (73 loc) • 2.5 kB
Markdown
Link card extension for TipTap editor that creates preview cards for URLs. Transform plain links into cards with title, description, and image.
```bash
npm install tiptap-link-card
```
```typescript
import { Editor } from "@tiptap/core";
import StarterKit from "@tiptap/starter-kit";
import LinkCard from "tiptap-link-card";
import "tiptap-link-card/dist/style.css";
const editor = new Editor({
element: document.querySelector("#editor"),
extensions: [
StarterKit,
LinkCard.configure({
// Optional: Enable automatic paste handling
addPasteHandler: true,
// Required for automatic URL resolution
dataResolver: async (url: string) => {
// Implement your own URL metadata extraction logic
const response = await fetch(
`/api/link-preview?url=${encodeURIComponent(url)}`
);
return await response.json();
},
}),
],
});
```
| Option | Type | Default | Description |
| ----------------- | ---------- | ----------- | ----------------------------------------------------- |
| `addPasteHandler` | `boolean` | `false` | Enable automatic link card creation when pasting URLs |
| `dataResolver` | `function` | `undefined` | Function to resolve link metadata from URLs |
The `dataResolver` function should return a promise that resolves to an object with the following structure:
```typescript
type SetLinkCardOptions = {
href: string; // The URL of the link
title: string; // The title of the page
description?: string; // Optional description/summary
imageSrc?: string; // Optional preview image URL
};
```
Insert a link card with specific attributes:
```typescript
editor.commands.setLinkCard({
href: "https://example.com",
title: "Example Website",
description: "This is an example website with great content.",
imageSrc: "https://example.com/preview.jpg",
});
```
Insert a link card by URL (requires `dataResolver`):
```typescript
editor.commands.setLinkCardUrl("https://example.com");
```
Use default style:
```typescript
import "tiptap-link-card/dist/style.css";
```
Or style card by yourself!
Contributions are welcome! Please feel free to submit a Pull Request.
MIT License.