@tiptap/extension-youtube 2.11.7 → 3.0.0-beta.1

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.d.ts CHANGED
@@ -1,4 +1,164 @@
1
- import { Youtube } from './youtube.js';
2
- export * from './youtube.js';
3
- export default Youtube;
4
- //# sourceMappingURL=index.d.ts.map
1
+ import { Node } from '@tiptap/core';
2
+
3
+ interface YoutubeOptions {
4
+ /**
5
+ * Controls if the paste handler for youtube videos should be added.
6
+ * @default true
7
+ * @example false
8
+ */
9
+ addPasteHandler: boolean;
10
+ /**
11
+ * Controls if the youtube video should be allowed to go fullscreen.
12
+ * @default true
13
+ * @example false
14
+ */
15
+ allowFullscreen: boolean;
16
+ /**
17
+ * Controls if the youtube video should autoplay.
18
+ * @default false
19
+ * @example true
20
+ */
21
+ autoplay: boolean;
22
+ /**
23
+ * The language of the captions shown in the youtube video.
24
+ * @default undefined
25
+ * @example 'en'
26
+ */
27
+ ccLanguage?: string;
28
+ /**
29
+ * Controls if the captions should be shown in the youtube video.
30
+ * @default undefined
31
+ * @example true
32
+ */
33
+ ccLoadPolicy?: boolean;
34
+ /**
35
+ * Controls if the controls should be shown in the youtube video.
36
+ * @default true
37
+ * @example false
38
+ */
39
+ controls: boolean;
40
+ /**
41
+ * Controls if the keyboard controls should be disabled in the youtube video.
42
+ * @default false
43
+ * @example true
44
+ */
45
+ disableKBcontrols: boolean;
46
+ /**
47
+ * Controls if the iframe api should be enabled in the youtube video.
48
+ * @default false
49
+ * @example true
50
+ */
51
+ enableIFrameApi: boolean;
52
+ /**
53
+ * The end time of the youtube video.
54
+ * @default 0
55
+ * @example 120
56
+ */
57
+ endTime: number;
58
+ /**
59
+ * The height of the youtube video.
60
+ * @default 480
61
+ * @example 720
62
+ */
63
+ height: number;
64
+ /**
65
+ * The language of the youtube video.
66
+ * @default undefined
67
+ * @example 'en'
68
+ */
69
+ interfaceLanguage?: string;
70
+ /**
71
+ * Controls if the video annotations should be shown in the youtube video.
72
+ * @default 0
73
+ * @example 1
74
+ */
75
+ ivLoadPolicy: number;
76
+ /**
77
+ * Controls if the youtube video should loop.
78
+ * @default false
79
+ * @example true
80
+ */
81
+ loop: boolean;
82
+ /**
83
+ * Controls if the youtube video should show a small youtube logo.
84
+ * @default false
85
+ * @example true
86
+ */
87
+ modestBranding: boolean;
88
+ /**
89
+ * The HTML attributes for a youtube video node.
90
+ * @default {}
91
+ * @example { class: 'foo' }
92
+ */
93
+ HTMLAttributes: Record<string, any>;
94
+ /**
95
+ * Controls if the youtube node should be inline or not.
96
+ * @default false
97
+ * @example true
98
+ */
99
+ inline: boolean;
100
+ /**
101
+ * Controls if the youtube video should be loaded from youtube-nocookie.com.
102
+ * @default false
103
+ * @example true
104
+ */
105
+ nocookie: boolean;
106
+ /**
107
+ * The origin of the youtube video.
108
+ * @default ''
109
+ * @example 'https://tiptap.dev'
110
+ */
111
+ origin: string;
112
+ /**
113
+ * The playlist of the youtube video.
114
+ * @default ''
115
+ * @example 'PLQg6GaokU5CwiVmsZ0dZm6VeIg0V5z1tK'
116
+ */
117
+ playlist: string;
118
+ /**
119
+ * The color of the youtube video progress bar.
120
+ * @default undefined
121
+ * @example 'red'
122
+ */
123
+ progressBarColor?: string;
124
+ /**
125
+ * The width of the youtube video.
126
+ * @default 640
127
+ * @example 1280
128
+ */
129
+ width: number;
130
+ /**
131
+ * Controls if the related youtube videos at the end are from the same channel.
132
+ * @default 1
133
+ * @example 0
134
+ */
135
+ rel: number;
136
+ }
137
+ /**
138
+ * The options for setting a youtube video.
139
+ */
140
+ type SetYoutubeVideoOptions = {
141
+ src: string;
142
+ width?: number;
143
+ height?: number;
144
+ start?: number;
145
+ };
146
+ declare module '@tiptap/core' {
147
+ interface Commands<ReturnType> {
148
+ youtube: {
149
+ /**
150
+ * Insert a youtube video
151
+ * @param options The youtube video attributes
152
+ * @example editor.commands.setYoutubeVideo({ src: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ' })
153
+ */
154
+ setYoutubeVideo: (options: SetYoutubeVideoOptions) => ReturnType;
155
+ };
156
+ }
157
+ }
158
+ /**
159
+ * This extension adds support for youtube videos.
160
+ * @see https://www.tiptap.dev/api/nodes/youtube
161
+ */
162
+ declare const Youtube: Node<YoutubeOptions, any>;
163
+
164
+ export { Youtube, type YoutubeOptions, Youtube as default };
package/dist/index.js CHANGED
@@ -1,194 +1,240 @@
1
- import { Node, nodePasteRule, mergeAttributes } from '@tiptap/core';
1
+ // src/youtube.ts
2
+ import { mergeAttributes, Node, nodePasteRule } from "@tiptap/core";
2
3
 
3
- const YOUTUBE_REGEX = /^((?:https?:)?\/\/)?((?:www|m|music)\.)?((?:youtube\.com|youtu.be|youtube-nocookie\.com))(\/(?:[\w-]+\?v=|embed\/|v\/)?)([\w-]+)(\S+)?$/;
4
- const YOUTUBE_REGEX_GLOBAL = /^((?:https?:)?\/\/)?((?:www|m|music)\.)?((?:youtube\.com|youtu.be|youtube-nocookie\.com))(\/(?:[\w-]+\?v=|embed\/|v\/)?)([\w-]+)(\S+)?$/g;
5
- const isValidYoutubeUrl = (url) => {
6
- return url.match(YOUTUBE_REGEX);
4
+ // src/utils.ts
5
+ var YOUTUBE_REGEX = /^((?:https?:)?\/\/)?((?:www|m|music)\.)?((?:youtube\.com|youtu.be|youtube-nocookie\.com))(\/(?:[\w-]+\?v=|embed\/|v\/)?)([\w-]+)(\S+)?$/;
6
+ var YOUTUBE_REGEX_GLOBAL = /^((?:https?:)?\/\/)?((?:www|m|music)\.)?((?:youtube\.com|youtu.be|youtube-nocookie\.com))(\/(?:[\w-]+\?v=|embed\/|v\/)?)([\w-]+)(\S+)?$/g;
7
+ var isValidYoutubeUrl = (url) => {
8
+ return url.match(YOUTUBE_REGEX);
7
9
  };
8
- const getYoutubeEmbedUrl = (nocookie, isPlaylist) => {
9
- if (isPlaylist) {
10
- return 'https://www.youtube-nocookie.com/embed/videoseries?list=';
11
- }
12
- return nocookie ? 'https://www.youtube-nocookie.com/embed/' : 'https://www.youtube.com/embed/';
10
+ var getYoutubeEmbedUrl = (nocookie, isPlaylist) => {
11
+ if (isPlaylist) {
12
+ return "https://www.youtube-nocookie.com/embed/videoseries?list=";
13
+ }
14
+ return nocookie ? "https://www.youtube-nocookie.com/embed/" : "https://www.youtube.com/embed/";
13
15
  };
14
- const getEmbedUrlFromYoutubeUrl = (options) => {
15
- const { url, allowFullscreen, autoplay, ccLanguage, ccLoadPolicy, controls, disableKBcontrols, enableIFrameApi, endTime, interfaceLanguage, ivLoadPolicy, loop, modestBranding, nocookie, origin, playlist, progressBarColor, startAt, rel, } = options;
16
- if (!isValidYoutubeUrl(url)) {
17
- return null;
18
- }
19
- // if is already an embed url, return it
20
- if (url.includes('/embed/')) {
21
- return url;
22
- }
23
- // if is a youtu.be url, get the id after the /
24
- if (url.includes('youtu.be')) {
25
- const id = url.split('/').pop();
26
- if (!id) {
27
- return null;
28
- }
29
- return `${getYoutubeEmbedUrl(nocookie)}${id}`;
30
- }
31
- const videoIdRegex = /(?:(v|list)=|shorts\/)([-\w]+)/gm;
32
- const matches = videoIdRegex.exec(url);
33
- if (!matches || !matches[2]) {
34
- return null;
35
- }
36
- let outputUrl = `${getYoutubeEmbedUrl(nocookie, matches[1] === 'list')}${matches[2]}`;
37
- const params = [];
38
- if (allowFullscreen === false) {
39
- params.push('fs=0');
40
- }
41
- if (autoplay) {
42
- params.push('autoplay=1');
43
- }
44
- if (ccLanguage) {
45
- params.push(`cc_lang_pref=${ccLanguage}`);
46
- }
47
- if (ccLoadPolicy) {
48
- params.push('cc_load_policy=1');
49
- }
50
- if (!controls) {
51
- params.push('controls=0');
52
- }
53
- if (disableKBcontrols) {
54
- params.push('disablekb=1');
55
- }
56
- if (enableIFrameApi) {
57
- params.push('enablejsapi=1');
58
- }
59
- if (endTime) {
60
- params.push(`end=${endTime}`);
61
- }
62
- if (interfaceLanguage) {
63
- params.push(`hl=${interfaceLanguage}`);
16
+ var getEmbedUrlFromYoutubeUrl = (options) => {
17
+ const {
18
+ url,
19
+ allowFullscreen,
20
+ autoplay,
21
+ ccLanguage,
22
+ ccLoadPolicy,
23
+ controls,
24
+ disableKBcontrols,
25
+ enableIFrameApi,
26
+ endTime,
27
+ interfaceLanguage,
28
+ ivLoadPolicy,
29
+ loop,
30
+ modestBranding,
31
+ nocookie,
32
+ origin,
33
+ playlist,
34
+ progressBarColor,
35
+ startAt,
36
+ rel
37
+ } = options;
38
+ if (!isValidYoutubeUrl(url)) {
39
+ return null;
40
+ }
41
+ if (url.includes("/embed/")) {
42
+ return url;
43
+ }
44
+ if (url.includes("youtu.be")) {
45
+ const id = url.split("/").pop();
46
+ if (!id) {
47
+ return null;
64
48
  }
65
- if (ivLoadPolicy) {
66
- params.push(`iv_load_policy=${ivLoadPolicy}`);
67
- }
68
- if (loop) {
69
- params.push('loop=1');
70
- }
71
- if (modestBranding) {
72
- params.push('modestbranding=1');
73
- }
74
- if (origin) {
75
- params.push(`origin=${origin}`);
76
- }
77
- if (playlist) {
78
- params.push(`playlist=${playlist}`);
79
- }
80
- if (startAt) {
81
- params.push(`start=${startAt}`);
82
- }
83
- if (progressBarColor) {
84
- params.push(`color=${progressBarColor}`);
85
- }
86
- if (rel !== undefined) {
87
- params.push(`rel=${rel}`);
88
- }
89
- if (params.length) {
90
- outputUrl += `${matches[1] === 'v' ? '?' : '&'}${params.join('&')}`;
91
- }
92
- return outputUrl;
49
+ return `${getYoutubeEmbedUrl(nocookie)}${id}`;
50
+ }
51
+ const videoIdRegex = /(?:(v|list)=|shorts\/)([-\w]+)/gm;
52
+ const matches = videoIdRegex.exec(url);
53
+ if (!matches || !matches[2]) {
54
+ return null;
55
+ }
56
+ let outputUrl = `${getYoutubeEmbedUrl(nocookie, matches[1] === "list")}${matches[2]}`;
57
+ const params = [];
58
+ if (allowFullscreen === false) {
59
+ params.push("fs=0");
60
+ }
61
+ if (autoplay) {
62
+ params.push("autoplay=1");
63
+ }
64
+ if (ccLanguage) {
65
+ params.push(`cc_lang_pref=${ccLanguage}`);
66
+ }
67
+ if (ccLoadPolicy) {
68
+ params.push("cc_load_policy=1");
69
+ }
70
+ if (!controls) {
71
+ params.push("controls=0");
72
+ }
73
+ if (disableKBcontrols) {
74
+ params.push("disablekb=1");
75
+ }
76
+ if (enableIFrameApi) {
77
+ params.push("enablejsapi=1");
78
+ }
79
+ if (endTime) {
80
+ params.push(`end=${endTime}`);
81
+ }
82
+ if (interfaceLanguage) {
83
+ params.push(`hl=${interfaceLanguage}`);
84
+ }
85
+ if (ivLoadPolicy) {
86
+ params.push(`iv_load_policy=${ivLoadPolicy}`);
87
+ }
88
+ if (loop) {
89
+ params.push("loop=1");
90
+ }
91
+ if (modestBranding) {
92
+ params.push("modestbranding=1");
93
+ }
94
+ if (origin) {
95
+ params.push(`origin=${origin}`);
96
+ }
97
+ if (playlist) {
98
+ params.push(`playlist=${playlist}`);
99
+ }
100
+ if (startAt) {
101
+ params.push(`start=${startAt}`);
102
+ }
103
+ if (progressBarColor) {
104
+ params.push(`color=${progressBarColor}`);
105
+ }
106
+ if (rel !== void 0) {
107
+ params.push(`rel=${rel}`);
108
+ }
109
+ if (params.length) {
110
+ outputUrl += `${matches[1] === "v" ? "?" : "&"}${params.join("&")}`;
111
+ }
112
+ return outputUrl;
93
113
  };
94
114
 
95
- /**
96
- * This extension adds support for youtube videos.
97
- * @see https://www.tiptap.dev/api/nodes/youtube
98
- */
99
- const Youtube = Node.create({
100
- name: 'youtube',
101
- addOptions() {
102
- return {
103
- addPasteHandler: true,
104
- allowFullscreen: true,
105
- autoplay: false,
106
- ccLanguage: undefined,
107
- ccLoadPolicy: undefined,
108
- controls: true,
109
- disableKBcontrols: false,
110
- enableIFrameApi: false,
111
- endTime: 0,
112
- height: 480,
113
- interfaceLanguage: undefined,
114
- ivLoadPolicy: 0,
115
- loop: false,
116
- modestBranding: false,
117
- HTMLAttributes: {},
118
- inline: false,
119
- nocookie: false,
120
- origin: '',
121
- playlist: '',
122
- progressBarColor: undefined,
123
- width: 640,
124
- rel: 1,
125
- };
126
- },
127
- inline() {
128
- return this.options.inline;
129
- },
130
- group() {
131
- return this.options.inline ? 'inline' : 'block';
132
- },
133
- draggable: true,
134
- addAttributes() {
135
- return {
136
- src: {
137
- default: null,
138
- },
139
- start: {
140
- default: 0,
141
- },
142
- width: {
143
- default: this.options.width,
144
- },
145
- height: {
146
- default: this.options.height,
147
- },
148
- };
149
- },
150
- parseHTML() {
151
- return [
152
- {
153
- tag: 'div[data-youtube-video] iframe',
154
- },
155
- ];
156
- },
157
- addCommands() {
158
- return {
159
- setYoutubeVideo: (options) => ({ commands }) => {
160
- if (!isValidYoutubeUrl(options.src)) {
161
- return false;
162
- }
163
- return commands.insertContent({
164
- type: this.name,
165
- attrs: options,
166
- });
167
- },
168
- };
169
- },
170
- addPasteRules() {
171
- if (!this.options.addPasteHandler) {
172
- return [];
115
+ // src/youtube.ts
116
+ var Youtube = Node.create({
117
+ name: "youtube",
118
+ addOptions() {
119
+ return {
120
+ addPasteHandler: true,
121
+ allowFullscreen: true,
122
+ autoplay: false,
123
+ ccLanguage: void 0,
124
+ ccLoadPolicy: void 0,
125
+ controls: true,
126
+ disableKBcontrols: false,
127
+ enableIFrameApi: false,
128
+ endTime: 0,
129
+ height: 480,
130
+ interfaceLanguage: void 0,
131
+ ivLoadPolicy: 0,
132
+ loop: false,
133
+ modestBranding: false,
134
+ HTMLAttributes: {},
135
+ inline: false,
136
+ nocookie: false,
137
+ origin: "",
138
+ playlist: "",
139
+ progressBarColor: void 0,
140
+ width: 640,
141
+ rel: 1
142
+ };
143
+ },
144
+ inline() {
145
+ return this.options.inline;
146
+ },
147
+ group() {
148
+ return this.options.inline ? "inline" : "block";
149
+ },
150
+ draggable: true,
151
+ addAttributes() {
152
+ return {
153
+ src: {
154
+ default: null
155
+ },
156
+ start: {
157
+ default: 0
158
+ },
159
+ width: {
160
+ default: this.options.width
161
+ },
162
+ height: {
163
+ default: this.options.height
164
+ }
165
+ };
166
+ },
167
+ parseHTML() {
168
+ return [
169
+ {
170
+ tag: "div[data-youtube-video] iframe"
171
+ }
172
+ ];
173
+ },
174
+ addCommands() {
175
+ return {
176
+ setYoutubeVideo: (options) => ({ commands }) => {
177
+ if (!isValidYoutubeUrl(options.src)) {
178
+ return false;
173
179
  }
174
- return [
175
- nodePasteRule({
176
- find: YOUTUBE_REGEX_GLOBAL,
177
- type: this.type,
178
- getAttributes: match => {
179
- return { src: match.input };
180
- },
181
- }),
182
- ];
183
- },
184
- renderHTML({ HTMLAttributes }) {
185
- const embedUrl = getEmbedUrlFromYoutubeUrl({
186
- url: HTMLAttributes.src,
187
- allowFullscreen: this.options.allowFullscreen,
180
+ return commands.insertContent({
181
+ type: this.name,
182
+ attrs: options
183
+ });
184
+ }
185
+ };
186
+ },
187
+ addPasteRules() {
188
+ if (!this.options.addPasteHandler) {
189
+ return [];
190
+ }
191
+ return [
192
+ nodePasteRule({
193
+ find: YOUTUBE_REGEX_GLOBAL,
194
+ type: this.type,
195
+ getAttributes: (match) => {
196
+ return { src: match.input };
197
+ }
198
+ })
199
+ ];
200
+ },
201
+ renderHTML({ HTMLAttributes }) {
202
+ const embedUrl = getEmbedUrlFromYoutubeUrl({
203
+ url: HTMLAttributes.src,
204
+ allowFullscreen: this.options.allowFullscreen,
205
+ autoplay: this.options.autoplay,
206
+ ccLanguage: this.options.ccLanguage,
207
+ ccLoadPolicy: this.options.ccLoadPolicy,
208
+ controls: this.options.controls,
209
+ disableKBcontrols: this.options.disableKBcontrols,
210
+ enableIFrameApi: this.options.enableIFrameApi,
211
+ endTime: this.options.endTime,
212
+ interfaceLanguage: this.options.interfaceLanguage,
213
+ ivLoadPolicy: this.options.ivLoadPolicy,
214
+ loop: this.options.loop,
215
+ modestBranding: this.options.modestBranding,
216
+ nocookie: this.options.nocookie,
217
+ origin: this.options.origin,
218
+ playlist: this.options.playlist,
219
+ progressBarColor: this.options.progressBarColor,
220
+ startAt: HTMLAttributes.start || 0,
221
+ rel: this.options.rel
222
+ });
223
+ HTMLAttributes.src = embedUrl;
224
+ return [
225
+ "div",
226
+ { "data-youtube-video": "" },
227
+ [
228
+ "iframe",
229
+ mergeAttributes(
230
+ this.options.HTMLAttributes,
231
+ {
232
+ width: this.options.width,
233
+ height: this.options.height,
234
+ allowfullscreen: this.options.allowFullscreen,
188
235
  autoplay: this.options.autoplay,
189
236
  ccLanguage: this.options.ccLanguage,
190
237
  ccLoadPolicy: this.options.ccLoadPolicy,
191
- controls: this.options.controls,
192
238
  disableKBcontrols: this.options.disableKBcontrols,
193
239
  enableIFrameApi: this.options.enableIFrameApi,
194
240
  endTime: this.options.endTime,
@@ -196,42 +242,22 @@ const Youtube = Node.create({
196
242
  ivLoadPolicy: this.options.ivLoadPolicy,
197
243
  loop: this.options.loop,
198
244
  modestBranding: this.options.modestBranding,
199
- nocookie: this.options.nocookie,
200
245
  origin: this.options.origin,
201
246
  playlist: this.options.playlist,
202
247
  progressBarColor: this.options.progressBarColor,
203
- startAt: HTMLAttributes.start || 0,
204
- rel: this.options.rel,
205
- });
206
- HTMLAttributes.src = embedUrl;
207
- return [
208
- 'div',
209
- { 'data-youtube-video': '' },
210
- [
211
- 'iframe',
212
- mergeAttributes(this.options.HTMLAttributes, {
213
- width: this.options.width,
214
- height: this.options.height,
215
- allowfullscreen: this.options.allowFullscreen,
216
- autoplay: this.options.autoplay,
217
- ccLanguage: this.options.ccLanguage,
218
- ccLoadPolicy: this.options.ccLoadPolicy,
219
- disableKBcontrols: this.options.disableKBcontrols,
220
- enableIFrameApi: this.options.enableIFrameApi,
221
- endTime: this.options.endTime,
222
- interfaceLanguage: this.options.interfaceLanguage,
223
- ivLoadPolicy: this.options.ivLoadPolicy,
224
- loop: this.options.loop,
225
- modestBranding: this.options.modestBranding,
226
- origin: this.options.origin,
227
- playlist: this.options.playlist,
228
- progressBarColor: this.options.progressBarColor,
229
- rel: this.options.rel,
230
- }, HTMLAttributes),
231
- ],
232
- ];
233
- },
248
+ rel: this.options.rel
249
+ },
250
+ HTMLAttributes
251
+ )
252
+ ]
253
+ ];
254
+ }
234
255
  });
235
256
 
236
- export { Youtube, Youtube as default };
237
- //# sourceMappingURL=index.js.map
257
+ // src/index.ts
258
+ var index_default = Youtube;
259
+ export {
260
+ Youtube,
261
+ index_default as default
262
+ };
263
+ //# sourceMappingURL=index.js.map