@tiptap/extension-twitch 3.0.0
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/LICENSE.md +21 -0
- package/README.md +18 -0
- package/dist/index.cjs +256 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +206 -0
- package/dist/index.d.ts +206 -0
- package/dist/index.js +224 -0
- package/dist/index.js.map +1 -0
- package/package.json +48 -0
- package/src/index.ts +6 -0
- package/src/twitch.ts +257 -0
- package/src/utils.ts +213 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { Node } from '@tiptap/core';
|
|
2
|
+
|
|
3
|
+
interface TwitchOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Controls if the paste handler for Twitch videos should be added.
|
|
6
|
+
* @default true
|
|
7
|
+
* @example false
|
|
8
|
+
*/
|
|
9
|
+
addPasteHandler: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Controls if the Twitch video should be allowed to go fullscreen.
|
|
12
|
+
* @default true
|
|
13
|
+
* @example false
|
|
14
|
+
*/
|
|
15
|
+
allowFullscreen: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Controls if the Twitch video should autoplay.
|
|
18
|
+
* @default false
|
|
19
|
+
* @example true
|
|
20
|
+
*/
|
|
21
|
+
autoplay: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Controls if the Twitch video should start muted.
|
|
24
|
+
* @default false
|
|
25
|
+
* @example true
|
|
26
|
+
*/
|
|
27
|
+
muted: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* The time in the video where playback starts (format: 1h2m3s).
|
|
30
|
+
* Only works for video embeds, not for clips or channels.
|
|
31
|
+
* @default undefined
|
|
32
|
+
* @example '1h2m3s'
|
|
33
|
+
*/
|
|
34
|
+
time?: string;
|
|
35
|
+
/**
|
|
36
|
+
* The parent domain for the Twitch embed. Required for embed functionality.
|
|
37
|
+
* @default 'localhost'
|
|
38
|
+
* @example 'example.com'
|
|
39
|
+
*/
|
|
40
|
+
parent: string;
|
|
41
|
+
/**
|
|
42
|
+
* The height of the Twitch video.
|
|
43
|
+
* @default 480
|
|
44
|
+
* @example 720
|
|
45
|
+
*/
|
|
46
|
+
height: number;
|
|
47
|
+
/**
|
|
48
|
+
* The width of the Twitch video.
|
|
49
|
+
* @default 640
|
|
50
|
+
* @example 1280
|
|
51
|
+
*/
|
|
52
|
+
width: number;
|
|
53
|
+
/**
|
|
54
|
+
* The HTML attributes for a Twitch video node.
|
|
55
|
+
* @default {}
|
|
56
|
+
* @example { class: 'foo' }
|
|
57
|
+
*/
|
|
58
|
+
HTMLAttributes: Record<string, any>;
|
|
59
|
+
/**
|
|
60
|
+
* Controls if the Twitch node should be inline or not.
|
|
61
|
+
* @default false
|
|
62
|
+
* @example true
|
|
63
|
+
*/
|
|
64
|
+
inline: boolean;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The options for setting a Twitch video.
|
|
68
|
+
*/
|
|
69
|
+
type SetTwitchVideoOptions = {
|
|
70
|
+
src: string;
|
|
71
|
+
width?: number;
|
|
72
|
+
height?: number;
|
|
73
|
+
autoplay?: boolean;
|
|
74
|
+
muted?: boolean;
|
|
75
|
+
time?: string;
|
|
76
|
+
};
|
|
77
|
+
declare module '@tiptap/core' {
|
|
78
|
+
interface Commands<ReturnType> {
|
|
79
|
+
twitch: {
|
|
80
|
+
/**
|
|
81
|
+
* Insert a Twitch video
|
|
82
|
+
* @param options The Twitch video attributes
|
|
83
|
+
* @example editor.commands.setTwitchVideo({ src: 'https://www.twitch.tv/videos/1234567890' })
|
|
84
|
+
*/
|
|
85
|
+
setTwitchVideo: (options: SetTwitchVideoOptions) => ReturnType;
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* This extension adds support for Twitch videos.
|
|
91
|
+
* @see https://www.tiptap.dev/api/nodes/twitch
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* import { useEditor, EditorContent } from '@tiptap/react'
|
|
96
|
+
* import { StarterKit } from '@tiptap/starter-kit'
|
|
97
|
+
* import { Twitch } from '@tiptap/extension-twitch'
|
|
98
|
+
*
|
|
99
|
+
* const editor = useEditor({
|
|
100
|
+
* extensions: [
|
|
101
|
+
* StarterKit,
|
|
102
|
+
* Twitch.configure({
|
|
103
|
+
* parent: 'example.com',
|
|
104
|
+
* allowFullscreen: true,
|
|
105
|
+
* }),
|
|
106
|
+
* ],
|
|
107
|
+
* content: '<p>Hello World!</p>',
|
|
108
|
+
* })
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
declare const Twitch: Node<TwitchOptions, any>;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Regex patterns for matching Twitch URLs
|
|
115
|
+
* Matches:
|
|
116
|
+
* - https://twitch.tv/videos/1234567890
|
|
117
|
+
* - https://www.twitch.tv/videos/1234567890
|
|
118
|
+
* - https://twitch.tv/examplechannel/clip/ClipName-123
|
|
119
|
+
* - https://www.twitch.tv/examplechannel/clip/ClipName-123
|
|
120
|
+
* - https://clips.twitch.tv/ClipName
|
|
121
|
+
* - https://www.clips.twitch.tv/ClipName
|
|
122
|
+
* - https://twitch.tv/examplechannel (channel)
|
|
123
|
+
* - https://www.twitch.tv/examplechannel
|
|
124
|
+
*/
|
|
125
|
+
declare const TWITCH_REGEX: RegExp;
|
|
126
|
+
declare const TWITCH_REGEX_GLOBAL: RegExp;
|
|
127
|
+
/**
|
|
128
|
+
* Validates if a URL is a valid Twitch video, clip or channel URL
|
|
129
|
+
*
|
|
130
|
+
* @param url - The URL to validate
|
|
131
|
+
* @returns true if the URL is a valid Twitch URL
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```ts
|
|
135
|
+
* isValidTwitchUrl('https://www.twitch.tv/videos/1234567890') // true
|
|
136
|
+
* isValidTwitchUrl('https://www.twitch.tv/examplechannel/clip/ExampleClipName') // true
|
|
137
|
+
* isValidTwitchUrl('https://www.twitch.tv/examplechannel') // true
|
|
138
|
+
* isValidTwitchUrl('https://clips.twitch.tv/ExampleClipName') // true
|
|
139
|
+
* isValidTwitchUrl('invalid') // false
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
142
|
+
declare const isValidTwitchUrl: (url: string) => RegExpMatchArray | null;
|
|
143
|
+
interface GetEmbedUrlOptions {
|
|
144
|
+
url: string;
|
|
145
|
+
allowFullscreen?: boolean;
|
|
146
|
+
autoplay?: boolean;
|
|
147
|
+
muted?: boolean;
|
|
148
|
+
time?: string;
|
|
149
|
+
parent?: string;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Extracts the video, clip or channel identifier from a Twitch URL
|
|
153
|
+
*
|
|
154
|
+
* @param url - The Twitch URL
|
|
155
|
+
* @returns Object containing type ('video', 'clip' or 'channel') and ID, or null if invalid
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* ```ts
|
|
159
|
+
* getTwitchIdentifier('https://www.twitch.tv/videos/1234567890')
|
|
160
|
+
* // { type: 'video', id: '1234567890' }
|
|
161
|
+
*
|
|
162
|
+
* getTwitchIdentifier('https://www.twitch.tv/examplechannel/clip/ExampleClipName-ABC123')
|
|
163
|
+
* // { type: 'clip', id: 'ExampleClipName-ABC123' }
|
|
164
|
+
*
|
|
165
|
+
* getTwitchIdentifier('https://www.twitch.tv/examplechannel')
|
|
166
|
+
* // { type: 'channel', id: 'examplechannel' }
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
declare const getTwitchIdentifier: (url: string) => {
|
|
170
|
+
type: "video" | "clip" | "channel";
|
|
171
|
+
id: string;
|
|
172
|
+
} | null;
|
|
173
|
+
/**
|
|
174
|
+
* Generates an embed URL from a Twitch video, clip or channel URL with optional parameters
|
|
175
|
+
*
|
|
176
|
+
* @param options - The embed URL options
|
|
177
|
+
* @returns The embed URL or null if invalid
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```ts
|
|
181
|
+
* getEmbedUrlFromTwitchUrl({
|
|
182
|
+
* url: 'https://www.twitch.tv/videos/1234567890',
|
|
183
|
+
* parent: 'example.com',
|
|
184
|
+
* muted: true,
|
|
185
|
+
* time: '1h2m3s'
|
|
186
|
+
* })
|
|
187
|
+
* // Returns: https://player.twitch.tv/?video=1234567890&parent=example.com&muted=true&time=1h2m3s
|
|
188
|
+
*
|
|
189
|
+
* getEmbedUrlFromTwitchUrl({
|
|
190
|
+
* url: 'https://www.twitch.tv/examplechannel/clip/ExampleClipName-ABC123',
|
|
191
|
+
* parent: 'example.com',
|
|
192
|
+
* autoplay: true,
|
|
193
|
+
* muted: true
|
|
194
|
+
* })
|
|
195
|
+
* // Returns: https://clips.twitch.tv/embed?clip=ExampleClipName-ABC123&parent=example.com&autoplay=true&muted=true
|
|
196
|
+
*
|
|
197
|
+
* getEmbedUrlFromTwitchUrl({
|
|
198
|
+
* url: 'https://www.twitch.tv/examplechannel',
|
|
199
|
+
* parent: 'example.com'
|
|
200
|
+
* })
|
|
201
|
+
* // Returns: https://player.twitch.tv/?channel=examplechannel&parent=example.com
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
declare const getEmbedUrlFromTwitchUrl: (options: GetEmbedUrlOptions) => string | null;
|
|
205
|
+
|
|
206
|
+
export { type GetEmbedUrlOptions, TWITCH_REGEX, TWITCH_REGEX_GLOBAL, Twitch, type TwitchOptions, Twitch as default, getEmbedUrlFromTwitchUrl, getTwitchIdentifier, isValidTwitchUrl };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
// src/twitch.ts
|
|
2
|
+
import { createAtomBlockMarkdownSpec, mergeAttributes, Node, nodePasteRule } from "@tiptap/core";
|
|
3
|
+
|
|
4
|
+
// src/utils.ts
|
|
5
|
+
var TWITCH_REGEX = /^(https?:\/\/)?(www\.)?(twitch\.tv|clips\.twitch\.tv)\/(?:videos\/(\d+)|(\w+)\/clip\/([\w-]+)|([\w-]+)(?:\/)?)?(\?.*)?$/;
|
|
6
|
+
var TWITCH_REGEX_GLOBAL = /^(https?:\/\/)?(www\.)?(twitch\.tv|clips\.twitch\.tv)\/(?:videos\/(\d+)|(\w+)\/clip\/([\w-]+)|([\w-]+)(?:\/)?)?(\?.*)?$/g;
|
|
7
|
+
var isValidTwitchUrl = (url) => {
|
|
8
|
+
return url.match(TWITCH_REGEX);
|
|
9
|
+
};
|
|
10
|
+
var getTwitchIdentifier = (url) => {
|
|
11
|
+
if (!isValidTwitchUrl(url)) {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
const cleanUrl = url.split("?")[0];
|
|
15
|
+
if (cleanUrl.includes("clips.twitch.tv/")) {
|
|
16
|
+
const clipRegex = /clips\.twitch\.tv\/([\w-]+)/;
|
|
17
|
+
const match = cleanUrl.match(clipRegex);
|
|
18
|
+
return match ? { type: "clip", id: match[1] } : null;
|
|
19
|
+
}
|
|
20
|
+
if (cleanUrl.includes("twitch.tv/")) {
|
|
21
|
+
const videoRegex = /twitch\.tv\/videos\/(\d+)/;
|
|
22
|
+
const videoMatch = cleanUrl.match(videoRegex);
|
|
23
|
+
if (videoMatch) {
|
|
24
|
+
return { type: "video", id: videoMatch[1] };
|
|
25
|
+
}
|
|
26
|
+
const channelClipRegex = /twitch\.tv\/([\w-]+)\/clip\/([\w-]+)/;
|
|
27
|
+
const clipMatch = cleanUrl.match(channelClipRegex);
|
|
28
|
+
if (clipMatch) {
|
|
29
|
+
return { type: "clip", id: clipMatch[2] };
|
|
30
|
+
}
|
|
31
|
+
const channelRegex = /twitch\.tv\/([\w-]+)(?:\/)?$/;
|
|
32
|
+
const channelMatch = cleanUrl.match(channelRegex);
|
|
33
|
+
if (channelMatch) {
|
|
34
|
+
return { type: "channel", id: channelMatch[1] };
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return null;
|
|
38
|
+
};
|
|
39
|
+
var getEmbedUrlFromTwitchUrl = (options) => {
|
|
40
|
+
const { url, allowFullscreen = true, autoplay = false, muted = false, time, parent } = options;
|
|
41
|
+
const identifier = getTwitchIdentifier(url);
|
|
42
|
+
if (!identifier) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
const parentDomain = parent || "localhost";
|
|
46
|
+
if (identifier.type === "clip") {
|
|
47
|
+
const clipUrl = new URL("https://clips.twitch.tv/embed");
|
|
48
|
+
clipUrl.searchParams.set("clip", identifier.id);
|
|
49
|
+
clipUrl.searchParams.set("parent", parentDomain);
|
|
50
|
+
if (autoplay) {
|
|
51
|
+
clipUrl.searchParams.set("autoplay", "true");
|
|
52
|
+
}
|
|
53
|
+
if (muted) {
|
|
54
|
+
clipUrl.searchParams.set("muted", "true");
|
|
55
|
+
}
|
|
56
|
+
return clipUrl.toString();
|
|
57
|
+
}
|
|
58
|
+
if (identifier.type === "video") {
|
|
59
|
+
const videoUrl = new URL("https://player.twitch.tv/");
|
|
60
|
+
videoUrl.searchParams.set("video", identifier.id);
|
|
61
|
+
videoUrl.searchParams.set("parent", parentDomain);
|
|
62
|
+
if (allowFullscreen) {
|
|
63
|
+
videoUrl.searchParams.set("allowfullscreen", "true");
|
|
64
|
+
}
|
|
65
|
+
if (autoplay) {
|
|
66
|
+
videoUrl.searchParams.set("autoplay", "true");
|
|
67
|
+
}
|
|
68
|
+
if (muted) {
|
|
69
|
+
videoUrl.searchParams.set("muted", "true");
|
|
70
|
+
}
|
|
71
|
+
if (time) {
|
|
72
|
+
videoUrl.searchParams.set("time", time);
|
|
73
|
+
}
|
|
74
|
+
return videoUrl.toString();
|
|
75
|
+
}
|
|
76
|
+
if (identifier.type === "channel") {
|
|
77
|
+
const channelUrl = new URL("https://player.twitch.tv/");
|
|
78
|
+
channelUrl.searchParams.set("channel", identifier.id);
|
|
79
|
+
channelUrl.searchParams.set("parent", parentDomain);
|
|
80
|
+
if (allowFullscreen) {
|
|
81
|
+
channelUrl.searchParams.set("allowfullscreen", "true");
|
|
82
|
+
}
|
|
83
|
+
if (autoplay) {
|
|
84
|
+
channelUrl.searchParams.set("autoplay", "true");
|
|
85
|
+
}
|
|
86
|
+
if (muted) {
|
|
87
|
+
channelUrl.searchParams.set("muted", "true");
|
|
88
|
+
}
|
|
89
|
+
return channelUrl.toString();
|
|
90
|
+
}
|
|
91
|
+
return null;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
// src/twitch.ts
|
|
95
|
+
var Twitch = Node.create({
|
|
96
|
+
name: "twitch",
|
|
97
|
+
addOptions() {
|
|
98
|
+
return {
|
|
99
|
+
addPasteHandler: true,
|
|
100
|
+
allowFullscreen: true,
|
|
101
|
+
autoplay: false,
|
|
102
|
+
muted: false,
|
|
103
|
+
time: void 0,
|
|
104
|
+
parent: "localhost",
|
|
105
|
+
height: 480,
|
|
106
|
+
width: 640,
|
|
107
|
+
HTMLAttributes: {},
|
|
108
|
+
inline: false
|
|
109
|
+
};
|
|
110
|
+
},
|
|
111
|
+
inline() {
|
|
112
|
+
return this.options.inline;
|
|
113
|
+
},
|
|
114
|
+
group() {
|
|
115
|
+
return this.options.inline ? "inline" : "block";
|
|
116
|
+
},
|
|
117
|
+
draggable: true,
|
|
118
|
+
addAttributes() {
|
|
119
|
+
return {
|
|
120
|
+
src: {
|
|
121
|
+
default: null
|
|
122
|
+
},
|
|
123
|
+
width: {
|
|
124
|
+
default: this.options.width
|
|
125
|
+
},
|
|
126
|
+
height: {
|
|
127
|
+
default: this.options.height
|
|
128
|
+
},
|
|
129
|
+
autoplay: {
|
|
130
|
+
default: this.options.autoplay
|
|
131
|
+
},
|
|
132
|
+
muted: {
|
|
133
|
+
default: this.options.muted
|
|
134
|
+
},
|
|
135
|
+
time: {
|
|
136
|
+
default: this.options.time
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
},
|
|
140
|
+
parseHTML() {
|
|
141
|
+
return [
|
|
142
|
+
{
|
|
143
|
+
tag: "div[data-twitch-video] iframe"
|
|
144
|
+
}
|
|
145
|
+
];
|
|
146
|
+
},
|
|
147
|
+
addCommands() {
|
|
148
|
+
return {
|
|
149
|
+
setTwitchVideo: (options) => ({ commands }) => {
|
|
150
|
+
if (!isValidTwitchUrl(options.src)) {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
return commands.insertContent({
|
|
154
|
+
type: this.name,
|
|
155
|
+
attrs: options
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
},
|
|
160
|
+
addPasteRules() {
|
|
161
|
+
if (!this.options.addPasteHandler) {
|
|
162
|
+
return [];
|
|
163
|
+
}
|
|
164
|
+
return [
|
|
165
|
+
nodePasteRule({
|
|
166
|
+
find: TWITCH_REGEX_GLOBAL,
|
|
167
|
+
type: this.type,
|
|
168
|
+
getAttributes: (match) => {
|
|
169
|
+
return { src: match.input };
|
|
170
|
+
}
|
|
171
|
+
})
|
|
172
|
+
];
|
|
173
|
+
},
|
|
174
|
+
renderHTML({ HTMLAttributes }) {
|
|
175
|
+
var _a, _b, _c;
|
|
176
|
+
const embedUrl = getEmbedUrlFromTwitchUrl({
|
|
177
|
+
url: HTMLAttributes.src,
|
|
178
|
+
allowFullscreen: this.options.allowFullscreen,
|
|
179
|
+
autoplay: (_a = HTMLAttributes.autoplay) != null ? _a : this.options.autoplay,
|
|
180
|
+
muted: (_b = HTMLAttributes.muted) != null ? _b : this.options.muted,
|
|
181
|
+
time: (_c = HTMLAttributes.time) != null ? _c : this.options.time,
|
|
182
|
+
parent: this.options.parent
|
|
183
|
+
});
|
|
184
|
+
if (!embedUrl) {
|
|
185
|
+
return ["div", "Invalid Twitch URL"];
|
|
186
|
+
}
|
|
187
|
+
HTMLAttributes.src = embedUrl;
|
|
188
|
+
return [
|
|
189
|
+
"div",
|
|
190
|
+
{ "data-twitch-video": "" },
|
|
191
|
+
[
|
|
192
|
+
"iframe",
|
|
193
|
+
mergeAttributes(
|
|
194
|
+
this.options.HTMLAttributes,
|
|
195
|
+
{
|
|
196
|
+
width: this.options.width,
|
|
197
|
+
height: this.options.height,
|
|
198
|
+
allowfullscreen: this.options.allowFullscreen,
|
|
199
|
+
scrolling: "no",
|
|
200
|
+
frameborder: "0"
|
|
201
|
+
},
|
|
202
|
+
HTMLAttributes
|
|
203
|
+
)
|
|
204
|
+
]
|
|
205
|
+
];
|
|
206
|
+
},
|
|
207
|
+
...createAtomBlockMarkdownSpec({
|
|
208
|
+
nodeName: "twitch",
|
|
209
|
+
allowedAttributes: ["src", "width", "height", "autoplay", "muted", "time"]
|
|
210
|
+
})
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
// src/index.ts
|
|
214
|
+
var index_default = Twitch;
|
|
215
|
+
export {
|
|
216
|
+
TWITCH_REGEX,
|
|
217
|
+
TWITCH_REGEX_GLOBAL,
|
|
218
|
+
Twitch,
|
|
219
|
+
index_default as default,
|
|
220
|
+
getEmbedUrlFromTwitchUrl,
|
|
221
|
+
getTwitchIdentifier,
|
|
222
|
+
isValidTwitchUrl
|
|
223
|
+
};
|
|
224
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/twitch.ts","../src/utils.ts","../src/index.ts"],"sourcesContent":["import { createAtomBlockMarkdownSpec, mergeAttributes, Node, nodePasteRule } from '@tiptap/core'\n\nimport { getEmbedUrlFromTwitchUrl, isValidTwitchUrl, TWITCH_REGEX_GLOBAL } from './utils.js'\n\nexport interface TwitchOptions {\n /**\n * Controls if the paste handler for Twitch videos should be added.\n * @default true\n * @example false\n */\n addPasteHandler: boolean\n\n /**\n * Controls if the Twitch video should be allowed to go fullscreen.\n * @default true\n * @example false\n */\n allowFullscreen: boolean\n\n /**\n * Controls if the Twitch video should autoplay.\n * @default false\n * @example true\n */\n autoplay: boolean\n\n /**\n * Controls if the Twitch video should start muted.\n * @default false\n * @example true\n */\n muted: boolean\n\n /**\n * The time in the video where playback starts (format: 1h2m3s).\n * Only works for video embeds, not for clips or channels.\n * @default undefined\n * @example '1h2m3s'\n */\n time?: string\n\n /**\n * The parent domain for the Twitch embed. Required for embed functionality.\n * @default 'localhost'\n * @example 'example.com'\n */\n parent: string\n\n /**\n * The height of the Twitch video.\n * @default 480\n * @example 720\n */\n height: number\n\n /**\n * The width of the Twitch video.\n * @default 640\n * @example 1280\n */\n width: number\n\n /**\n * The HTML attributes for a Twitch video node.\n * @default {}\n * @example { class: 'foo' }\n */\n HTMLAttributes: Record<string, any>\n\n /**\n * Controls if the Twitch node should be inline or not.\n * @default false\n * @example true\n */\n inline: boolean\n}\n\n/**\n * The options for setting a Twitch video.\n */\ntype SetTwitchVideoOptions = {\n src: string\n width?: number\n height?: number\n autoplay?: boolean\n muted?: boolean\n time?: string\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n twitch: {\n /**\n * Insert a Twitch video\n * @param options The Twitch video attributes\n * @example editor.commands.setTwitchVideo({ src: 'https://www.twitch.tv/videos/1234567890' })\n */\n setTwitchVideo: (options: SetTwitchVideoOptions) => ReturnType\n }\n }\n}\n\n/**\n * This extension adds support for Twitch videos.\n * @see https://www.tiptap.dev/api/nodes/twitch\n *\n * @example\n * ```ts\n * import { useEditor, EditorContent } from '@tiptap/react'\n * import { StarterKit } from '@tiptap/starter-kit'\n * import { Twitch } from '@tiptap/extension-twitch'\n *\n * const editor = useEditor({\n * extensions: [\n * StarterKit,\n * Twitch.configure({\n * parent: 'example.com',\n * allowFullscreen: true,\n * }),\n * ],\n * content: '<p>Hello World!</p>',\n * })\n * ```\n */\nexport const Twitch = Node.create<TwitchOptions>({\n name: 'twitch',\n\n addOptions() {\n return {\n addPasteHandler: true,\n allowFullscreen: true,\n autoplay: false,\n muted: false,\n time: undefined,\n parent: 'localhost',\n height: 480,\n width: 640,\n HTMLAttributes: {},\n inline: false,\n }\n },\n\n inline() {\n return this.options.inline\n },\n\n group() {\n return this.options.inline ? 'inline' : 'block'\n },\n\n draggable: true,\n\n addAttributes() {\n return {\n src: {\n default: null,\n },\n width: {\n default: this.options.width,\n },\n height: {\n default: this.options.height,\n },\n autoplay: {\n default: this.options.autoplay,\n },\n muted: {\n default: this.options.muted,\n },\n time: {\n default: this.options.time,\n },\n }\n },\n\n parseHTML() {\n return [\n {\n tag: 'div[data-twitch-video] iframe',\n },\n ]\n },\n\n addCommands() {\n return {\n setTwitchVideo:\n (options: SetTwitchVideoOptions) =>\n ({ commands }) => {\n if (!isValidTwitchUrl(options.src)) {\n return false\n }\n\n return commands.insertContent({\n type: this.name,\n attrs: options,\n })\n },\n }\n },\n\n addPasteRules() {\n if (!this.options.addPasteHandler) {\n return []\n }\n\n return [\n nodePasteRule({\n find: TWITCH_REGEX_GLOBAL,\n type: this.type,\n getAttributes: match => {\n return { src: match.input }\n },\n }),\n ]\n },\n\n renderHTML({ HTMLAttributes }) {\n const embedUrl = getEmbedUrlFromTwitchUrl({\n url: HTMLAttributes.src,\n allowFullscreen: this.options.allowFullscreen,\n autoplay: HTMLAttributes.autoplay ?? this.options.autoplay,\n muted: HTMLAttributes.muted ?? this.options.muted,\n time: HTMLAttributes.time ?? this.options.time,\n parent: this.options.parent,\n })\n\n if (!embedUrl) {\n return ['div', 'Invalid Twitch URL']\n }\n\n HTMLAttributes.src = embedUrl\n\n return [\n 'div',\n { 'data-twitch-video': '' },\n [\n 'iframe',\n mergeAttributes(\n this.options.HTMLAttributes,\n {\n width: this.options.width,\n height: this.options.height,\n allowfullscreen: this.options.allowFullscreen,\n scrolling: 'no',\n frameborder: '0',\n },\n HTMLAttributes,\n ),\n ],\n ]\n },\n\n ...createAtomBlockMarkdownSpec({\n nodeName: 'twitch',\n allowedAttributes: ['src', 'width', 'height', 'autoplay', 'muted', 'time'],\n }),\n})\n","/**\n * Regex patterns for matching Twitch URLs\n * Matches:\n * - https://twitch.tv/videos/1234567890\n * - https://www.twitch.tv/videos/1234567890\n * - https://twitch.tv/examplechannel/clip/ClipName-123\n * - https://www.twitch.tv/examplechannel/clip/ClipName-123\n * - https://clips.twitch.tv/ClipName\n * - https://www.clips.twitch.tv/ClipName\n * - https://twitch.tv/examplechannel (channel)\n * - https://www.twitch.tv/examplechannel\n */\nexport const TWITCH_REGEX =\n /^(https?:\\/\\/)?(www\\.)?(twitch\\.tv|clips\\.twitch\\.tv)\\/(?:videos\\/(\\d+)|(\\w+)\\/clip\\/([\\w-]+)|([\\w-]+)(?:\\/)?)?(\\?.*)?$/\n\nexport const TWITCH_REGEX_GLOBAL =\n /^(https?:\\/\\/)?(www\\.)?(twitch\\.tv|clips\\.twitch\\.tv)\\/(?:videos\\/(\\d+)|(\\w+)\\/clip\\/([\\w-]+)|([\\w-]+)(?:\\/)?)?(\\?.*)?$/g\n\n/**\n * Validates if a URL is a valid Twitch video, clip or channel URL\n *\n * @param url - The URL to validate\n * @returns true if the URL is a valid Twitch URL\n *\n * @example\n * ```ts\n * isValidTwitchUrl('https://www.twitch.tv/videos/1234567890') // true\n * isValidTwitchUrl('https://www.twitch.tv/examplechannel/clip/ExampleClipName') // true\n * isValidTwitchUrl('https://www.twitch.tv/examplechannel') // true\n * isValidTwitchUrl('https://clips.twitch.tv/ExampleClipName') // true\n * isValidTwitchUrl('invalid') // false\n * ```\n */\nexport const isValidTwitchUrl = (url: string) => {\n return url.match(TWITCH_REGEX)\n}\n\nexport interface GetEmbedUrlOptions {\n url: string\n allowFullscreen?: boolean\n autoplay?: boolean\n muted?: boolean\n time?: string\n parent?: string\n}\n\n/**\n * Extracts the video, clip or channel identifier from a Twitch URL\n *\n * @param url - The Twitch URL\n * @returns Object containing type ('video', 'clip' or 'channel') and ID, or null if invalid\n *\n * @example\n * ```ts\n * getTwitchIdentifier('https://www.twitch.tv/videos/1234567890')\n * // { type: 'video', id: '1234567890' }\n *\n * getTwitchIdentifier('https://www.twitch.tv/examplechannel/clip/ExampleClipName-ABC123')\n * // { type: 'clip', id: 'ExampleClipName-ABC123' }\n *\n * getTwitchIdentifier('https://www.twitch.tv/examplechannel')\n * // { type: 'channel', id: 'examplechannel' }\n * ```\n */\nexport const getTwitchIdentifier = (url: string): { type: 'video' | 'clip' | 'channel'; id: string } | null => {\n if (!isValidTwitchUrl(url)) {\n return null\n }\n\n // Remove query parameters\n const cleanUrl = url.split('?')[0]\n\n // Handle clip URLs from clips.twitch.tv\n if (cleanUrl.includes('clips.twitch.tv/')) {\n const clipRegex = /clips\\.twitch\\.tv\\/([\\w-]+)/\n const match = cleanUrl.match(clipRegex)\n return match ? { type: 'clip', id: match[1] } : null\n }\n\n // Handle twitch.tv URLs\n if (cleanUrl.includes('twitch.tv/')) {\n // Check if it's a video URL (videos/ID)\n const videoRegex = /twitch\\.tv\\/videos\\/(\\d+)/\n const videoMatch = cleanUrl.match(videoRegex)\n if (videoMatch) {\n return { type: 'video', id: videoMatch[1] }\n }\n\n // Check if it's a clip URL (channel/clip/clipname)\n const channelClipRegex = /twitch\\.tv\\/([\\w-]+)\\/clip\\/([\\w-]+)/\n const clipMatch = cleanUrl.match(channelClipRegex)\n if (clipMatch) {\n return { type: 'clip', id: clipMatch[2] }\n }\n\n // Otherwise it's a channel URL\n const channelRegex = /twitch\\.tv\\/([\\w-]+)(?:\\/)?$/\n const channelMatch = cleanUrl.match(channelRegex)\n if (channelMatch) {\n return { type: 'channel', id: channelMatch[1] }\n }\n }\n\n return null\n}\n\n/**\n * Generates an embed URL from a Twitch video, clip or channel URL with optional parameters\n *\n * @param options - The embed URL options\n * @returns The embed URL or null if invalid\n *\n * @example\n * ```ts\n * getEmbedUrlFromTwitchUrl({\n * url: 'https://www.twitch.tv/videos/1234567890',\n * parent: 'example.com',\n * muted: true,\n * time: '1h2m3s'\n * })\n * // Returns: https://player.twitch.tv/?video=1234567890&parent=example.com&muted=true&time=1h2m3s\n *\n * getEmbedUrlFromTwitchUrl({\n * url: 'https://www.twitch.tv/examplechannel/clip/ExampleClipName-ABC123',\n * parent: 'example.com',\n * autoplay: true,\n * muted: true\n * })\n * // Returns: https://clips.twitch.tv/embed?clip=ExampleClipName-ABC123&parent=example.com&autoplay=true&muted=true\n *\n * getEmbedUrlFromTwitchUrl({\n * url: 'https://www.twitch.tv/examplechannel',\n * parent: 'example.com'\n * })\n * // Returns: https://player.twitch.tv/?channel=examplechannel&parent=example.com\n * ```\n */\nexport const getEmbedUrlFromTwitchUrl = (options: GetEmbedUrlOptions): string | null => {\n const { url, allowFullscreen = true, autoplay = false, muted = false, time, parent } = options\n\n const identifier = getTwitchIdentifier(url)\n\n if (!identifier) {\n return null\n }\n\n const parentDomain = parent || 'localhost'\n\n // Handle clip URLs\n if (identifier.type === 'clip') {\n const clipUrl = new URL('https://clips.twitch.tv/embed')\n clipUrl.searchParams.set('clip', identifier.id)\n clipUrl.searchParams.set('parent', parentDomain)\n\n if (autoplay) {\n clipUrl.searchParams.set('autoplay', 'true')\n }\n\n if (muted) {\n clipUrl.searchParams.set('muted', 'true')\n }\n\n return clipUrl.toString()\n }\n\n // Handle video URLs\n if (identifier.type === 'video') {\n const videoUrl = new URL('https://player.twitch.tv/')\n videoUrl.searchParams.set('video', identifier.id)\n videoUrl.searchParams.set('parent', parentDomain)\n\n if (allowFullscreen) {\n videoUrl.searchParams.set('allowfullscreen', 'true')\n }\n\n if (autoplay) {\n videoUrl.searchParams.set('autoplay', 'true')\n }\n\n if (muted) {\n videoUrl.searchParams.set('muted', 'true')\n }\n\n if (time) {\n videoUrl.searchParams.set('time', time)\n }\n\n return videoUrl.toString()\n }\n\n // Handle channel URLs\n if (identifier.type === 'channel') {\n const channelUrl = new URL('https://player.twitch.tv/')\n channelUrl.searchParams.set('channel', identifier.id)\n channelUrl.searchParams.set('parent', parentDomain)\n\n if (allowFullscreen) {\n channelUrl.searchParams.set('allowfullscreen', 'true')\n }\n\n if (autoplay) {\n channelUrl.searchParams.set('autoplay', 'true')\n }\n\n if (muted) {\n channelUrl.searchParams.set('muted', 'true')\n }\n\n return channelUrl.toString()\n }\n\n return null\n}\n","import { Twitch } from './twitch.js'\n\nexport * from './twitch.js'\nexport * from './utils.js'\n\nexport default Twitch\n"],"mappings":";AAAA,SAAS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A,QACN,MAAM,KAAK;AAAA,QACX,eAAe,WAAS;AACtB,iBAAO,EAAE,KAAK,MAAM,MAAM;AAAA,QAC5B;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AAAA,EAEA,WAAW,EAAE,eAAe,GAAG;AAxNjC;AAyNI,UAAM,WAAW,yBAAyB;AAAA,MACxC,KAAK,eAAe;AAAA,MACpB,iBAAiB,KAAK,QAAQ;AAAA,MAC9B,WAAU,oBAAe,aAAf,YAA2B,KAAK,QAAQ;AAAA,MAClD,QAAO,oBAAe,UAAf,YAAwB,KAAK,QAAQ;AAAA,MAC5C,OAAM,oBAAe,SAAf,YAAuB,KAAK,QAAQ;AAAA,MAC1C,QAAQ,KAAK,QAAQ;AAAA,IACvB,CAAC;AAED,QAAI,CAAC,UAAU;AACb,aAAO,CAAC,OAAO,oBAAoB;AAAA,IACrC;AAEA,mBAAe,MAAM;AAErB,WAAO;AAAA,MACL;AAAA,MACA,EAAE,qBAAqB,GAAG;AAAA,MAC1B;AAAA,QACE;AAAA,QACA;AAAA,UACE,KAAK,QAAQ;AAAA,UACb;AAAA,YACE,OAAO,KAAK,QAAQ;AAAA,YACpB,QAAQ,KAAK,QAAQ;AAAA,YACrB,iBAAiB,KAAK,QAAQ;AAAA,YAC9B,WAAW;AAAA,YACX,aAAa;AAAA,UACf;AAAA,UACA;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAAA,EAEA,GAAG,4BAA4B;AAAA,IAC7B,UAAU;AAAA,IACV,mBAAmB,CAAC,OAAO,SAAS,UAAU,YAAY,SAAS,MAAM;AAAA,EAC3E,CAAC;AACH,CAAC;;;AE3PD,IAAO,gBAAQ;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tiptap/extension-twitch",
|
|
3
|
+
"description": "a twitch embed extension for tiptap",
|
|
4
|
+
"version": "3.0.0",
|
|
5
|
+
"homepage": "https://tiptap.dev",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"tiptap",
|
|
8
|
+
"tiptap extension"
|
|
9
|
+
],
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"funding": {
|
|
12
|
+
"type": "github",
|
|
13
|
+
"url": "https://github.com/sponsors/ueberdosis"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": {
|
|
19
|
+
"import": "./dist/index.d.ts",
|
|
20
|
+
"require": "./dist/index.d.cts"
|
|
21
|
+
},
|
|
22
|
+
"import": "./dist/index.js",
|
|
23
|
+
"require": "./dist/index.cjs"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"main": "dist/index.cjs",
|
|
27
|
+
"module": "dist/index.js",
|
|
28
|
+
"types": "dist/index.d.ts",
|
|
29
|
+
"files": [
|
|
30
|
+
"src",
|
|
31
|
+
"dist"
|
|
32
|
+
],
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@tiptap/core": "^3.13.0"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"@tiptap/core": "^3.13.0"
|
|
38
|
+
},
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "https://github.com/ueberdosis/tiptap",
|
|
42
|
+
"directory": "packages/extension-twitch"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "tsup",
|
|
46
|
+
"lint": "prettier ./src/ --check && eslint --cache --quiet --no-error-on-unmatched-pattern ./src/"
|
|
47
|
+
}
|
|
48
|
+
}
|