@tiptap/extension-twitch 3.22.5 → 3.23.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/dist/index.cjs CHANGED
@@ -24,6 +24,7 @@ __export(index_exports, {
24
24
  TWITCH_REGEX_GLOBAL: () => TWITCH_REGEX_GLOBAL,
25
25
  Twitch: () => Twitch,
26
26
  default: () => index_default,
27
+ getAttributesFromTwitchEmbedUrl: () => getAttributesFromTwitchEmbedUrl,
27
28
  getEmbedUrlFromTwitchUrl: () => getEmbedUrlFromTwitchUrl,
28
29
  getTwitchIdentifier: () => getTwitchIdentifier,
29
30
  isValidTwitchUrl: () => isValidTwitchUrl
@@ -122,8 +123,78 @@ var getEmbedUrlFromTwitchUrl = (options) => {
122
123
  }
123
124
  return null;
124
125
  };
126
+ var getAttributesFromTwitchEmbedUrl = (url) => {
127
+ let parsedUrl;
128
+ try {
129
+ parsedUrl = new URL(url);
130
+ } catch {
131
+ return null;
132
+ }
133
+ const hostname = parsedUrl.hostname.replace(/^www\./, "");
134
+ const result = {
135
+ src: ""
136
+ };
137
+ if (hostname === "player.twitch.tv") {
138
+ const videoId = parsedUrl.searchParams.get("video");
139
+ const channelName = parsedUrl.searchParams.get("channel");
140
+ if (videoId) {
141
+ result.src = `https://www.twitch.tv/videos/${videoId}`;
142
+ } else if (channelName) {
143
+ result.src = `https://www.twitch.tv/${channelName}`;
144
+ } else {
145
+ return null;
146
+ }
147
+ } else if (hostname === "clips.twitch.tv" && parsedUrl.pathname === "/embed") {
148
+ const clipId = parsedUrl.searchParams.get("clip");
149
+ if (!clipId) {
150
+ return null;
151
+ }
152
+ result.src = `https://clips.twitch.tv/${clipId}`;
153
+ } else {
154
+ return null;
155
+ }
156
+ if (parsedUrl.searchParams.get("autoplay") === "true") {
157
+ result.autoplay = true;
158
+ }
159
+ if (parsedUrl.searchParams.get("muted") === "true") {
160
+ result.muted = true;
161
+ }
162
+ const time = parsedUrl.searchParams.get("time");
163
+ if (time) {
164
+ result.time = time;
165
+ }
166
+ if (!isValidTwitchUrl(result.src)) {
167
+ return null;
168
+ }
169
+ return result;
170
+ };
125
171
 
126
172
  // src/twitch.ts
173
+ var getParsedDimension = (value) => {
174
+ if (!value) {
175
+ return null;
176
+ }
177
+ const parsedValue = Number.parseInt(value, 10);
178
+ return Number.isNaN(parsedValue) ? null : parsedValue;
179
+ };
180
+ var getParsedTwitchAttributes = (element) => {
181
+ const src = element.getAttribute("src");
182
+ if (!src) {
183
+ return null;
184
+ }
185
+ const embedAttributes = getAttributesFromTwitchEmbedUrl(src);
186
+ if (embedAttributes) {
187
+ return embedAttributes;
188
+ }
189
+ if (isValidTwitchUrl(src)) {
190
+ return {
191
+ src
192
+ };
193
+ }
194
+ return {
195
+ src
196
+ };
197
+ };
127
198
  var Twitch = import_core.Node.create({
128
199
  name: "twitch",
129
200
  addOptions() {
@@ -150,22 +221,40 @@ var Twitch = import_core.Node.create({
150
221
  addAttributes() {
151
222
  return {
152
223
  src: {
153
- default: null
224
+ default: null,
225
+ parseHTML: (element) => {
226
+ var _a;
227
+ return (_a = getParsedTwitchAttributes(element)) == null ? void 0 : _a.src;
228
+ }
154
229
  },
155
230
  width: {
156
- default: this.options.width
231
+ default: this.options.width,
232
+ parseHTML: (element) => getParsedDimension(element.getAttribute("width"))
157
233
  },
158
234
  height: {
159
- default: this.options.height
235
+ default: this.options.height,
236
+ parseHTML: (element) => getParsedDimension(element.getAttribute("height"))
160
237
  },
161
238
  autoplay: {
162
- default: this.options.autoplay
239
+ default: this.options.autoplay,
240
+ parseHTML: (element) => {
241
+ var _a;
242
+ return (_a = getParsedTwitchAttributes(element)) == null ? void 0 : _a.autoplay;
243
+ }
163
244
  },
164
245
  muted: {
165
- default: this.options.muted
246
+ default: this.options.muted,
247
+ parseHTML: (element) => {
248
+ var _a;
249
+ return (_a = getParsedTwitchAttributes(element)) == null ? void 0 : _a.muted;
250
+ }
166
251
  },
167
252
  time: {
168
- default: this.options.time
253
+ default: this.options.time,
254
+ parseHTML: (element) => {
255
+ var _a;
256
+ return (_a = getParsedTwitchAttributes(element)) == null ? void 0 : _a.time;
257
+ }
169
258
  }
170
259
  };
171
260
  },
@@ -249,6 +338,7 @@ var index_default = Twitch;
249
338
  TWITCH_REGEX,
250
339
  TWITCH_REGEX_GLOBAL,
251
340
  Twitch,
341
+ getAttributesFromTwitchEmbedUrl,
252
342
  getEmbedUrlFromTwitchUrl,
253
343
  getTwitchIdentifier,
254
344
  isValidTwitchUrl
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/twitch.ts","../src/utils.ts"],"sourcesContent":["import { Twitch } from './twitch.js'\n\nexport * from './twitch.js'\nexport * from './utils.js'\n\nexport default Twitch\n","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"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,kBAAkF;;;ACY3E,IAAM,eACX;AAEK,IAAM,sBACX;AAiBK,IAAM,mBAAmB,CAAC,QAAgB;AAC/C,SAAO,IAAI,MAAM,YAAY;AAC/B;AA6BO,IAAM,sBAAsB,CAAC,QAA2E;AAC7G,MAAI,CAAC,iBAAiB,GAAG,GAAG;AAC1B,WAAO;AAAA,EACT;AAGA,QAAM,WAAW,IAAI,MAAM,GAAG,EAAE,CAAC;AAGjC,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A2B,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,YACA;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,OAAG,yCAA4B;AAAA,IAC7B,UAAU;AAAA,IACV,mBAAmB,CAAC,OAAO,SAAS,UAAU,YAAY,SAAS,MAAM;AAAA,EAC3E,CAAC;AACH,CAAC;;;AD3PD,IAAO,gBAAQ;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts","../src/twitch.ts","../src/utils.ts"],"sourcesContent":["import { Twitch } from './twitch.js'\n\nexport * from './twitch.js'\nexport * from './utils.js'\n\nexport default Twitch\n","import { createAtomBlockMarkdownSpec, mergeAttributes, Node, nodePasteRule } from '@tiptap/core'\n\nimport {\n getAttributesFromTwitchEmbedUrl,\n getEmbedUrlFromTwitchUrl,\n isValidTwitchUrl,\n TWITCH_REGEX_GLOBAL,\n} from './utils.js'\n\nconst getParsedDimension = (value: string | null) => {\n if (!value) {\n return null\n }\n\n const parsedValue = Number.parseInt(value, 10)\n\n return Number.isNaN(parsedValue) ? null : parsedValue\n}\n\nconst getParsedTwitchAttributes = (element: HTMLElement) => {\n const src = element.getAttribute('src')\n\n if (!src) {\n return null\n }\n\n const embedAttributes = getAttributesFromTwitchEmbedUrl(src)\n\n if (embedAttributes) {\n return embedAttributes\n }\n\n if (isValidTwitchUrl(src)) {\n return {\n src,\n }\n }\n\n return {\n src,\n }\n}\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 parseHTML: element => getParsedTwitchAttributes(element)?.src,\n },\n width: {\n default: this.options.width,\n parseHTML: element => getParsedDimension(element.getAttribute('width')),\n },\n height: {\n default: this.options.height,\n parseHTML: element => getParsedDimension(element.getAttribute('height')),\n },\n autoplay: {\n default: this.options.autoplay,\n parseHTML: element => getParsedTwitchAttributes(element)?.autoplay,\n },\n muted: {\n default: this.options.muted,\n parseHTML: element => getParsedTwitchAttributes(element)?.muted,\n },\n time: {\n default: this.options.time,\n parseHTML: element => getParsedTwitchAttributes(element)?.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\nexport interface TwitchEmbedAttributes {\n src: string\n autoplay?: boolean\n muted?: boolean\n time?: 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\n/**\n * Parses a Twitch embed URL into canonical Twitch node attributes.\n *\n * @param url - The Twitch embed URL\n * @returns Canonical node attributes or null if the URL is not a supported Twitch embed URL\n *\n * @example\n * ```ts\n * getAttributesFromTwitchEmbedUrl('https://player.twitch.tv/?video=1234567890&parent=example.com&muted=true')\n * // Returns: { src: 'https://www.twitch.tv/videos/1234567890', muted: true }\n *\n * getAttributesFromTwitchEmbedUrl('https://clips.twitch.tv/embed?clip=ExampleClipName-ABC123&parent=example.com')\n * // Returns: { src: 'https://clips.twitch.tv/ExampleClipName-ABC123' }\n * ```\n */\nexport const getAttributesFromTwitchEmbedUrl = (url: string): TwitchEmbedAttributes | null => {\n let parsedUrl: URL\n\n try {\n parsedUrl = new URL(url)\n } catch {\n return null\n }\n\n const hostname = parsedUrl.hostname.replace(/^www\\./, '')\n const result: TwitchEmbedAttributes = {\n src: '',\n }\n\n if (hostname === 'player.twitch.tv') {\n const videoId = parsedUrl.searchParams.get('video')\n const channelName = parsedUrl.searchParams.get('channel')\n\n if (videoId) {\n result.src = `https://www.twitch.tv/videos/${videoId}`\n } else if (channelName) {\n result.src = `https://www.twitch.tv/${channelName}`\n } else {\n return null\n }\n } else if (hostname === 'clips.twitch.tv' && parsedUrl.pathname === '/embed') {\n const clipId = parsedUrl.searchParams.get('clip')\n\n if (!clipId) {\n return null\n }\n\n result.src = `https://clips.twitch.tv/${clipId}`\n } else {\n return null\n }\n\n if (parsedUrl.searchParams.get('autoplay') === 'true') {\n result.autoplay = true\n }\n\n if (parsedUrl.searchParams.get('muted') === 'true') {\n result.muted = true\n }\n\n const time = parsedUrl.searchParams.get('time')\n\n if (time) {\n result.time = time\n }\n\n if (!isValidTwitchUrl(result.src)) {\n return null\n }\n\n return result\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,kBAAkF;;;ACY3E,IAAM,eACX;AAEK,IAAM,sBACX;AAiBK,IAAM,mBAAmB,CAAC,QAAgB;AAC/C,SAAO,IAAI,MAAM,YAAY;AAC/B;AAoCO,IAAM,sBAAsB,CAAC,QAA2E;AAC7G,MAAI,CAAC,iBAAiB,GAAG,GAAG;AAC1B,WAAO;AAAA,EACT;AAGA,QAAM,WAAW,IAAI,MAAM,GAAG,EAAE,CAAC;AAGjC,MAAI,SAAS,SAAS,kBAAkB,GAAG;AACzC,UAAM,YAAY;AAClB,UAAM,QAAQ,SAAS,MAAM,SAAS;AACtC,WAAO,QAAQ,EAAE,MAAM,QAAQ,IAAI,MAAM,CAAC,EAAE,IAAI;AAAA,EAClD;AAGA,MAAI,SAAS,SAAS,YAAY,GAAG;AAEnC,UAAM,aAAa;AACnB,UAAM,aAAa,SAAS,MAAM,UAAU;AAC5C,QAAI,YAAY;AACd,aAAO,EAAE,MAAM,SAAS,IAAI,WAAW,CAA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string;
150
150
  }
151
+ interface TwitchEmbedAttributes {
152
+ src: string;
153
+ autoplay?: boolean;
154
+ muted?: boolean;
155
+ time?: string;
156
+ }
151
157
  /**
152
158
  * Extracts the video, clip or channel identifier from a Twitch URL
153
159
  *
@@ -202,5 +208,21 @@ declare const getTwitchIdentifier: (url: string) => {
202
208
  * ```
203
209
  */
204
210
  declare const getEmbedUrlFromTwitchUrl: (options: GetEmbedUrlOptions) => string | null;
211
+ /**
212
+ * Parses a Twitch embed URL into canonical Twitch node attributes.
213
+ *
214
+ * @param url - The Twitch embed URL
215
+ * @returns Canonical node attributes or null if the URL is not a supported Twitch embed URL
216
+ *
217
+ * @example
218
+ * ```ts
219
+ * getAttributesFromTwitchEmbedUrl('https://player.twitch.tv/?video=1234567890&parent=example.com&muted=true')
220
+ * // Returns: { src: 'https://www.twitch.tv/videos/1234567890', muted: true }
221
+ *
222
+ * getAttributesFromTwitchEmbedUrl('https://clips.twitch.tv/embed?clip=ExampleClipName-ABC123&parent=example.com')
223
+ * // Returns: { src: 'https://clips.twitch.tv/ExampleClipName-ABC123' }
224
+ * ```
225
+ */
226
+ declare const getAttributesFromTwitchEmbedUrl: (url: string) => TwitchEmbedAttributes | null;
205
227
 
206
- export { type GetEmbedUrlOptions, TWITCH_REGEX, TWITCH_REGEX_GLOBAL, Twitch, type TwitchOptions, Twitch as default, getEmbedUrlFromTwitchUrl, getTwitchIdentifier, isValidTwitchUrl };
228
+ export { type GetEmbedUrlOptions, TWITCH_REGEX, TWITCH_REGEX_GLOBAL, Twitch, type TwitchEmbedAttributes, type TwitchOptions, Twitch as default, getAttributesFromTwitchEmbedUrl, getEmbedUrlFromTwitchUrl, getTwitchIdentifier, isValidTwitchUrl };
package/dist/index.d.ts CHANGED
@@ -148,6 +148,12 @@ interface GetEmbedUrlOptions {
148
148
  time?: string;
149
149
  parent?: string;
150
150
  }
151
+ interface TwitchEmbedAttributes {
152
+ src: string;
153
+ autoplay?: boolean;
154
+ muted?: boolean;
155
+ time?: string;
156
+ }
151
157
  /**
152
158
  * Extracts the video, clip or channel identifier from a Twitch URL
153
159
  *
@@ -202,5 +208,21 @@ declare const getTwitchIdentifier: (url: string) => {
202
208
  * ```
203
209
  */
204
210
  declare const getEmbedUrlFromTwitchUrl: (options: GetEmbedUrlOptions) => string | null;
211
+ /**
212
+ * Parses a Twitch embed URL into canonical Twitch node attributes.
213
+ *
214
+ * @param url - The Twitch embed URL
215
+ * @returns Canonical node attributes or null if the URL is not a supported Twitch embed URL
216
+ *
217
+ * @example
218
+ * ```ts
219
+ * getAttributesFromTwitchEmbedUrl('https://player.twitch.tv/?video=1234567890&parent=example.com&muted=true')
220
+ * // Returns: { src: 'https://www.twitch.tv/videos/1234567890', muted: true }
221
+ *
222
+ * getAttributesFromTwitchEmbedUrl('https://clips.twitch.tv/embed?clip=ExampleClipName-ABC123&parent=example.com')
223
+ * // Returns: { src: 'https://clips.twitch.tv/ExampleClipName-ABC123' }
224
+ * ```
225
+ */
226
+ declare const getAttributesFromTwitchEmbedUrl: (url: string) => TwitchEmbedAttributes | null;
205
227
 
206
- export { type GetEmbedUrlOptions, TWITCH_REGEX, TWITCH_REGEX_GLOBAL, Twitch, type TwitchOptions, Twitch as default, getEmbedUrlFromTwitchUrl, getTwitchIdentifier, isValidTwitchUrl };
228
+ export { type GetEmbedUrlOptions, TWITCH_REGEX, TWITCH_REGEX_GLOBAL, Twitch, type TwitchEmbedAttributes, type TwitchOptions, Twitch as default, getAttributesFromTwitchEmbedUrl, getEmbedUrlFromTwitchUrl, getTwitchIdentifier, isValidTwitchUrl };
package/dist/index.js CHANGED
@@ -90,8 +90,78 @@ var getEmbedUrlFromTwitchUrl = (options) => {
90
90
  }
91
91
  return null;
92
92
  };
93
+ var getAttributesFromTwitchEmbedUrl = (url) => {
94
+ let parsedUrl;
95
+ try {
96
+ parsedUrl = new URL(url);
97
+ } catch {
98
+ return null;
99
+ }
100
+ const hostname = parsedUrl.hostname.replace(/^www\./, "");
101
+ const result = {
102
+ src: ""
103
+ };
104
+ if (hostname === "player.twitch.tv") {
105
+ const videoId = parsedUrl.searchParams.get("video");
106
+ const channelName = parsedUrl.searchParams.get("channel");
107
+ if (videoId) {
108
+ result.src = `https://www.twitch.tv/videos/${videoId}`;
109
+ } else if (channelName) {
110
+ result.src = `https://www.twitch.tv/${channelName}`;
111
+ } else {
112
+ return null;
113
+ }
114
+ } else if (hostname === "clips.twitch.tv" && parsedUrl.pathname === "/embed") {
115
+ const clipId = parsedUrl.searchParams.get("clip");
116
+ if (!clipId) {
117
+ return null;
118
+ }
119
+ result.src = `https://clips.twitch.tv/${clipId}`;
120
+ } else {
121
+ return null;
122
+ }
123
+ if (parsedUrl.searchParams.get("autoplay") === "true") {
124
+ result.autoplay = true;
125
+ }
126
+ if (parsedUrl.searchParams.get("muted") === "true") {
127
+ result.muted = true;
128
+ }
129
+ const time = parsedUrl.searchParams.get("time");
130
+ if (time) {
131
+ result.time = time;
132
+ }
133
+ if (!isValidTwitchUrl(result.src)) {
134
+ return null;
135
+ }
136
+ return result;
137
+ };
93
138
 
94
139
  // src/twitch.ts
140
+ var getParsedDimension = (value) => {
141
+ if (!value) {
142
+ return null;
143
+ }
144
+ const parsedValue = Number.parseInt(value, 10);
145
+ return Number.isNaN(parsedValue) ? null : parsedValue;
146
+ };
147
+ var getParsedTwitchAttributes = (element) => {
148
+ const src = element.getAttribute("src");
149
+ if (!src) {
150
+ return null;
151
+ }
152
+ const embedAttributes = getAttributesFromTwitchEmbedUrl(src);
153
+ if (embedAttributes) {
154
+ return embedAttributes;
155
+ }
156
+ if (isValidTwitchUrl(src)) {
157
+ return {
158
+ src
159
+ };
160
+ }
161
+ return {
162
+ src
163
+ };
164
+ };
95
165
  var Twitch = Node.create({
96
166
  name: "twitch",
97
167
  addOptions() {
@@ -118,22 +188,40 @@ var Twitch = Node.create({
118
188
  addAttributes() {
119
189
  return {
120
190
  src: {
121
- default: null
191
+ default: null,
192
+ parseHTML: (element) => {
193
+ var _a;
194
+ return (_a = getParsedTwitchAttributes(element)) == null ? void 0 : _a.src;
195
+ }
122
196
  },
123
197
  width: {
124
- default: this.options.width
198
+ default: this.options.width,
199
+ parseHTML: (element) => getParsedDimension(element.getAttribute("width"))
125
200
  },
126
201
  height: {
127
- default: this.options.height
202
+ default: this.options.height,
203
+ parseHTML: (element) => getParsedDimension(element.getAttribute("height"))
128
204
  },
129
205
  autoplay: {
130
- default: this.options.autoplay
206
+ default: this.options.autoplay,
207
+ parseHTML: (element) => {
208
+ var _a;
209
+ return (_a = getParsedTwitchAttributes(element)) == null ? void 0 : _a.autoplay;
210
+ }
131
211
  },
132
212
  muted: {
133
- default: this.options.muted
213
+ default: this.options.muted,
214
+ parseHTML: (element) => {
215
+ var _a;
216
+ return (_a = getParsedTwitchAttributes(element)) == null ? void 0 : _a.muted;
217
+ }
134
218
  },
135
219
  time: {
136
- default: this.options.time
220
+ default: this.options.time,
221
+ parseHTML: (element) => {
222
+ var _a;
223
+ return (_a = getParsedTwitchAttributes(element)) == null ? void 0 : _a.time;
224
+ }
137
225
  }
138
226
  };
139
227
  },
@@ -217,6 +305,7 @@ export {
217
305
  TWITCH_REGEX_GLOBAL,
218
306
  Twitch,
219
307
  index_default as default,
308
+ getAttributesFromTwitchEmbedUrl,
220
309
  getEmbedUrlFromTwitchUrl,
221
310
  getTwitchIdentifier,
222
311
  isValidTwitchUrl
package/dist/index.js.map CHANGED
@@ -1 +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,6BAA6B,iBAAiB,MAAM,qBAAqB;;;ACY3E,IAAM,eACX;AAEK,IAAM,sBACX;AAiBK,IAAM,mBAAmB,CAAC,QAAgB;AAC/C,SAAO,IAAI,MAAM,YAAY;AAC/B;AA6BO,IAAM,sBAAsB,CAAC,QAA2E;AAC7G,MAAI,CAAC,iBAAiB,GAAG,GAAG;AAC1B,WAAO;AAAA,EACT;AAGA,QAAM,WAAW,IAAI,MAAM,GAAG,EAAE,CAAC;AAGjC,MAAI,SAAS,SAAS,kBAAkB,GAAG;AACzC,UAAM,YAAY;AAClB,UAAM,QAAQ,SAAS,MAAM,SAAS;AACtC,WAAO,QAAQ,EAAE,MAAM,QAAQ,IAAI,MAAM,CAAC,EAAE,IAAI;AAAA,EAClD;AAGA,MAAI,SAAS,SAAS,YAAY,GAAG;AAEnC,UAAM,aAAa;AACnB,UAAM,aAAa,SAAS,MAAM,UAAU;AAC5C,QAAI,YAAY;AACd,aAAO,EAAE,MAAM,SAAS,IAAI,WAAW,CAAC,EAAE;AAAA,IAC5C;AAGA,UAAM,mBAAmB;AACzB,UAAM,YAAY,SAAS,MAAM,gBAAgB;AACjD,QAAI,WAAW;AACb,aAAO,EAAE,MAAM,QAAQ,IAAI,UAAU,CAAC,EAAE;AAAA,IAC1C;AAGA,UAAM,eAAe;AACrB,UAAM,eAAe,SAAS,MAAM,YAAY;AAChD,QAAI,cAAc;AAChB,aAAO,EAAE,MAAM,WAAW,IAAI,aAAa,CAAC,EAAE;AAAA,IAChD;AAAA,EACF;AAEA,SAAO;AACT;AAiCO,IAAM,2BAA2B,CAAC,YAA+C;AACtF,QAAM,EAAE,KAAK,kBAAkB,MAAM,WAAW,OAAO,QAAQ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}
1
+ {"version":3,"sources":["../src/twitch.ts","../src/utils.ts","../src/index.ts"],"sourcesContent":["import { createAtomBlockMarkdownSpec, mergeAttributes, Node, nodePasteRule } from '@tiptap/core'\n\nimport {\n getAttributesFromTwitchEmbedUrl,\n getEmbedUrlFromTwitchUrl,\n isValidTwitchUrl,\n TWITCH_REGEX_GLOBAL,\n} from './utils.js'\n\nconst getParsedDimension = (value: string | null) => {\n if (!value) {\n return null\n }\n\n const parsedValue = Number.parseInt(value, 10)\n\n return Number.isNaN(parsedValue) ? null : parsedValue\n}\n\nconst getParsedTwitchAttributes = (element: HTMLElement) => {\n const src = element.getAttribute('src')\n\n if (!src) {\n return null\n }\n\n const embedAttributes = getAttributesFromTwitchEmbedUrl(src)\n\n if (embedAttributes) {\n return embedAttributes\n }\n\n if (isValidTwitchUrl(src)) {\n return {\n src,\n }\n }\n\n return {\n src,\n }\n}\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 parseHTML: element => getParsedTwitchAttributes(element)?.src,\n },\n width: {\n default: this.options.width,\n parseHTML: element => getParsedDimension(element.getAttribute('width')),\n },\n height: {\n default: this.options.height,\n parseHTML: element => getParsedDimension(element.getAttribute('height')),\n },\n autoplay: {\n default: this.options.autoplay,\n parseHTML: element => getParsedTwitchAttributes(element)?.autoplay,\n },\n muted: {\n default: this.options.muted,\n parseHTML: element => getParsedTwitchAttributes(element)?.muted,\n },\n time: {\n default: this.options.time,\n parseHTML: element => getParsedTwitchAttributes(element)?.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\nexport interface TwitchEmbedAttributes {\n src: string\n autoplay?: boolean\n muted?: boolean\n time?: 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\n/**\n * Parses a Twitch embed URL into canonical Twitch node attributes.\n *\n * @param url - The Twitch embed URL\n * @returns Canonical node attributes or null if the URL is not a supported Twitch embed URL\n *\n * @example\n * ```ts\n * getAttributesFromTwitchEmbedUrl('https://player.twitch.tv/?video=1234567890&parent=example.com&muted=true')\n * // Returns: { src: 'https://www.twitch.tv/videos/1234567890', muted: true }\n *\n * getAttributesFromTwitchEmbedUrl('https://clips.twitch.tv/embed?clip=ExampleClipName-ABC123&parent=example.com')\n * // Returns: { src: 'https://clips.twitch.tv/ExampleClipName-ABC123' }\n * ```\n */\nexport const getAttributesFromTwitchEmbedUrl = (url: string): TwitchEmbedAttributes | null => {\n let parsedUrl: URL\n\n try {\n parsedUrl = new URL(url)\n } catch {\n return null\n }\n\n const hostname = parsedUrl.hostname.replace(/^www\\./, '')\n const result: TwitchEmbedAttributes = {\n src: '',\n }\n\n if (hostname === 'player.twitch.tv') {\n const videoId = parsedUrl.searchParams.get('video')\n const channelName = parsedUrl.searchParams.get('channel')\n\n if (videoId) {\n result.src = `https://www.twitch.tv/videos/${videoId}`\n } else if (channelName) {\n result.src = `https://www.twitch.tv/${channelName}`\n } else {\n return null\n }\n } else if (hostname === 'clips.twitch.tv' && parsedUrl.pathname === '/embed') {\n const clipId = parsedUrl.searchParams.get('clip')\n\n if (!clipId) {\n return null\n }\n\n result.src = `https://clips.twitch.tv/${clipId}`\n } else {\n return null\n }\n\n if (parsedUrl.searchParams.get('autoplay') === 'true') {\n result.autoplay = true\n }\n\n if (parsedUrl.searchParams.get('muted') === 'true') {\n result.muted = true\n }\n\n const time = parsedUrl.searchParams.get('time')\n\n if (time) {\n result.time = time\n }\n\n if (!isValidTwitchUrl(result.src)) {\n return null\n }\n\n return result\n}\n","import { Twitch } from './twitch.js'\n\nexport * from './twitch.js'\nexport * from './utils.js'\n\nexport default Twitch\n"],"mappings":";AAAA,SAAS,6BAA6B,iBAAiB,MAAM,qBAAqB;;;ACY3E,IAAM,eACX;AAEK,IAAM,sBACX;AAiBK,IAAM,mBAAmB,CAAC,QAAgB;AAC/C,SAAO,IAAI,MAAM,YAAY;AAC/B;AAoCO,IAAM,sBAAsB,CAAC,QAA2E;AAC7G,MAAI,CAAC,iBAAiB,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ACpB,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;;;AExSD,IAAO,gBAAQ;","names":[]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tiptap/extension-twitch",
3
3
  "description": "a twitch embed extension for tiptap",
4
- "version": "3.22.5",
4
+ "version": "3.23.2",
5
5
  "homepage": "https://tiptap.dev",
6
6
  "keywords": [
7
7
  "tiptap",
@@ -31,10 +31,10 @@
31
31
  "dist"
32
32
  ],
33
33
  "devDependencies": {
34
- "@tiptap/core": "^3.22.5"
34
+ "@tiptap/core": "^3.23.2"
35
35
  },
36
36
  "peerDependencies": {
37
- "@tiptap/core": "3.22.5"
37
+ "@tiptap/core": "3.23.2"
38
38
  },
39
39
  "repository": {
40
40
  "type": "git",
package/src/twitch.ts CHANGED
@@ -1,6 +1,45 @@
1
1
  import { createAtomBlockMarkdownSpec, mergeAttributes, Node, nodePasteRule } from '@tiptap/core'
2
2
 
3
- import { getEmbedUrlFromTwitchUrl, isValidTwitchUrl, TWITCH_REGEX_GLOBAL } from './utils.js'
3
+ import {
4
+ getAttributesFromTwitchEmbedUrl,
5
+ getEmbedUrlFromTwitchUrl,
6
+ isValidTwitchUrl,
7
+ TWITCH_REGEX_GLOBAL,
8
+ } from './utils.js'
9
+
10
+ const getParsedDimension = (value: string | null) => {
11
+ if (!value) {
12
+ return null
13
+ }
14
+
15
+ const parsedValue = Number.parseInt(value, 10)
16
+
17
+ return Number.isNaN(parsedValue) ? null : parsedValue
18
+ }
19
+
20
+ const getParsedTwitchAttributes = (element: HTMLElement) => {
21
+ const src = element.getAttribute('src')
22
+
23
+ if (!src) {
24
+ return null
25
+ }
26
+
27
+ const embedAttributes = getAttributesFromTwitchEmbedUrl(src)
28
+
29
+ if (embedAttributes) {
30
+ return embedAttributes
31
+ }
32
+
33
+ if (isValidTwitchUrl(src)) {
34
+ return {
35
+ src,
36
+ }
37
+ }
38
+
39
+ return {
40
+ src,
41
+ }
42
+ }
4
43
 
5
44
  export interface TwitchOptions {
6
45
  /**
@@ -154,21 +193,27 @@ export const Twitch = Node.create<TwitchOptions>({
154
193
  return {
155
194
  src: {
156
195
  default: null,
196
+ parseHTML: element => getParsedTwitchAttributes(element)?.src,
157
197
  },
158
198
  width: {
159
199
  default: this.options.width,
200
+ parseHTML: element => getParsedDimension(element.getAttribute('width')),
160
201
  },
161
202
  height: {
162
203
  default: this.options.height,
204
+ parseHTML: element => getParsedDimension(element.getAttribute('height')),
163
205
  },
164
206
  autoplay: {
165
207
  default: this.options.autoplay,
208
+ parseHTML: element => getParsedTwitchAttributes(element)?.autoplay,
166
209
  },
167
210
  muted: {
168
211
  default: this.options.muted,
212
+ parseHTML: element => getParsedTwitchAttributes(element)?.muted,
169
213
  },
170
214
  time: {
171
215
  default: this.options.time,
216
+ parseHTML: element => getParsedTwitchAttributes(element)?.time,
172
217
  },
173
218
  }
174
219
  },
package/src/utils.ts CHANGED
@@ -44,6 +44,13 @@ export interface GetEmbedUrlOptions {
44
44
  parent?: string
45
45
  }
46
46
 
47
+ export interface TwitchEmbedAttributes {
48
+ src: string
49
+ autoplay?: boolean
50
+ muted?: boolean
51
+ time?: string
52
+ }
53
+
47
54
  /**
48
55
  * Extracts the video, clip or channel identifier from a Twitch URL
49
56
  *
@@ -211,3 +218,76 @@ export const getEmbedUrlFromTwitchUrl = (options: GetEmbedUrlOptions): string |
211
218
 
212
219
  return null
213
220
  }
221
+
222
+ /**
223
+ * Parses a Twitch embed URL into canonical Twitch node attributes.
224
+ *
225
+ * @param url - The Twitch embed URL
226
+ * @returns Canonical node attributes or null if the URL is not a supported Twitch embed URL
227
+ *
228
+ * @example
229
+ * ```ts
230
+ * getAttributesFromTwitchEmbedUrl('https://player.twitch.tv/?video=1234567890&parent=example.com&muted=true')
231
+ * // Returns: { src: 'https://www.twitch.tv/videos/1234567890', muted: true }
232
+ *
233
+ * getAttributesFromTwitchEmbedUrl('https://clips.twitch.tv/embed?clip=ExampleClipName-ABC123&parent=example.com')
234
+ * // Returns: { src: 'https://clips.twitch.tv/ExampleClipName-ABC123' }
235
+ * ```
236
+ */
237
+ export const getAttributesFromTwitchEmbedUrl = (url: string): TwitchEmbedAttributes | null => {
238
+ let parsedUrl: URL
239
+
240
+ try {
241
+ parsedUrl = new URL(url)
242
+ } catch {
243
+ return null
244
+ }
245
+
246
+ const hostname = parsedUrl.hostname.replace(/^www\./, '')
247
+ const result: TwitchEmbedAttributes = {
248
+ src: '',
249
+ }
250
+
251
+ if (hostname === 'player.twitch.tv') {
252
+ const videoId = parsedUrl.searchParams.get('video')
253
+ const channelName = parsedUrl.searchParams.get('channel')
254
+
255
+ if (videoId) {
256
+ result.src = `https://www.twitch.tv/videos/${videoId}`
257
+ } else if (channelName) {
258
+ result.src = `https://www.twitch.tv/${channelName}`
259
+ } else {
260
+ return null
261
+ }
262
+ } else if (hostname === 'clips.twitch.tv' && parsedUrl.pathname === '/embed') {
263
+ const clipId = parsedUrl.searchParams.get('clip')
264
+
265
+ if (!clipId) {
266
+ return null
267
+ }
268
+
269
+ result.src = `https://clips.twitch.tv/${clipId}`
270
+ } else {
271
+ return null
272
+ }
273
+
274
+ if (parsedUrl.searchParams.get('autoplay') === 'true') {
275
+ result.autoplay = true
276
+ }
277
+
278
+ if (parsedUrl.searchParams.get('muted') === 'true') {
279
+ result.muted = true
280
+ }
281
+
282
+ const time = parsedUrl.searchParams.get('time')
283
+
284
+ if (time) {
285
+ result.time = time
286
+ }
287
+
288
+ if (!isValidTwitchUrl(result.src)) {
289
+ return null
290
+ }
291
+
292
+ return result
293
+ }