rehype-video 2.0.1 → 2.1.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/README.md CHANGED
@@ -141,6 +141,39 @@ Output:
141
141
  </details>
142
142
  ```
143
143
 
144
+ ## \<track>
145
+
146
+ We will reference all two of the relevant VTT files by adding [\<track>](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/track) elements inside our HTML [\<video>](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video) element:
147
+
148
+ ```js
149
+ const mrkStr = `https://github.com/sintel-short.mp4?!#track['en']=captions/vtt/sintel-en.vtt&track['en:label']=English&track['en:kind']=subtitles&track['en:default']=true&track['de']=captions/vtt/sintel-de.vtt&track['de:label']=Deutsch&track['de:kind']=subtitles`;
150
+ const htmlStr = unified()
151
+ .use(remarkParse)
152
+ .use(remark2rehype, { allowDangerousHtml: true })
153
+ .use(rehypeVideo, { })
154
+ .use(stringify)
155
+ .processSync(mrkStr)
156
+ .toString();
157
+ ```
158
+
159
+ Output:
160
+
161
+ ```html
162
+ <details open class="octicon octicon-video">
163
+ <summary>
164
+ <svg aria-hidden height="16" width="16" viewBox="0 0 16 16" version="1.1" data-view-component class="octicon octicon-device-camera-video">
165
+ <path fill-rule="evenodd" d="M16 3.75a.75.75 0 00-1.136-.643L11 5.425V4.75A1.75 1.75 0 009.25 3h-7.5A1.75 1.75 0 000 4.75v6.5C0 12.216.784 13 1.75 13h7.5A1.75 1.75 0 0011 11.25v-.675l3.864 2.318A.75.75 0 0016 12.25v-8.5zm-5 5.075l3.5 2.1v-5.85l-3.5 2.1v1.65zM9.5 6.75v-2a.25.25 0 00-.25-.25h-7.5a.25.25 0 00-.25.25v6.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-4.5z"></path>
166
+ </svg>
167
+ <span aria-label="Video description sintel-short.mp4">sintel-short.mp4</span>
168
+ <span class="dropdown-caret"></span>
169
+ </summary>
170
+ <video muted controls style="max-height:640px;" src="https://github.com/sintel-short.mp4?!#track[&#x27;en&#x27;]=captions/vtt/sintel-en.vtt&#x26;track[&#x27;en:label&#x27;]=English&#x26;track[&#x27;en:kind&#x27;]=subtitles&#x26;track[&#x27;en:default&#x27;]=true&#x26;track[&#x27;de&#x27;]=captions/vtt/sintel-de.vtt&#x26;track[&#x27;de:label&#x27;]=Deutsch&#x26;track[&#x27;de:kind&#x27;]=subtitles">
171
+ <track kind="subtitles" src="captions/vtt/sintel-en.vtt" label="English" default>
172
+ <track kind="subtitles" src="captions/vtt/sintel-de.vtt" label="Deutsch">
173
+ </video>
174
+ </details>
175
+ ```
176
+
144
177
  ## Options
145
178
 
146
179
  ```ts
@@ -155,6 +188,11 @@ export declare type RehypeVideoOptions = {
155
188
  * @default true
156
189
  */
157
190
  details?: boolean;
191
+ /**
192
+ * Support `<track>` tag to wrap <video>.
193
+ * @default true
194
+ */
195
+ track?: boolean;
158
196
  };
159
197
  ```
160
198
 
@@ -163,11 +201,7 @@ export declare type RehypeVideoOptions = {
163
201
  - [`rehype-attr`](https://github.com/jaywcjlove/rehype-attr) New syntax to add attributes to Markdown.
164
202
  - [`rehype-rewrite`](https://github.com/jaywcjlove/rehype-rewrite) Rewrite element with rehype.
165
203
  - [`rehype-ignore`](https://github.com/jaywcjlove/rehype-ignore) Ignore content display via HTML comments, Shown in GitHub readme, excluded in HTML.
166
- - [`rehypejs`](https://github.com/rehypejs/rehype) HTML processor powered by plugins part of the [@unifiedjs](https://github.com/unifiedjs) collective
167
- - [`remark-parse`](https://www.npmjs.com/package/remark-parse) remark plugin to parse Markdown
168
- - [`remark-rehype`](https://www.npmjs.com/package/remark-rehype) remark plugin to transform to rehype
169
- - [`rehype-raw`](https://www.npmjs.com/package/rehype-raw) rehype plugin to reparse the tree (and raw nodes)
170
- - [`rehype-stringify`](https://www.npmjs.com/package/rehype-stringify) rehype plugin to serialize HTML
204
+ - [`remark-github-blockquote-alert`](https://github.com/jaywcjlove/remark-github-blockquote-alert) Remark plugin to add support for [GitHub Alert](https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#alerts)
171
205
 
172
206
  ## License
173
207
 
package/lib/index.d.ts CHANGED
@@ -11,6 +11,11 @@ export type RehypeVideoOptions = {
11
11
  * @default true
12
12
  */
13
13
  details?: boolean;
14
+ /**
15
+ * Support `<track>` tag to wrap <video>.
16
+ * @default true
17
+ */
18
+ track?: boolean;
14
19
  };
15
20
  declare const RehypeVideo: Plugin<[RehypeVideoOptions?], Root>;
16
21
  export default RehypeVideo;
package/lib/index.js CHANGED
@@ -1,13 +1,20 @@
1
1
  import { visit } from 'unist-util-visit';
2
2
  import { detailsNode } from './detailsNode.js';
3
+ import { trackNode } from './trackNode.js';
4
+ const delimiter = /((?:https?:\/\/)(?:(?:[a-z0-9]?(?:[a-z0-9\-]{1,61}[a-z0-9])?\.[^\.|\s])+[a-z\.]*[a-z]+|(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3})(?::\d{1,5})*[a-z0-9.,_\/~#&=;%+?\-\\(\\)]*)/g;
3
5
  const properties = { muted: 'muted', controls: 'controls', style: 'max-height:640px;' };
4
6
  const queryStringToObject = (url) => [...new URLSearchParams(url.split('?!#')[1])].reduce((a, [k, v]) => ((a[k] = v), a), {});
5
- function reElement(node, details, href) {
6
- const filename = href.split('/').pop()?.replace(/(\?|!|\#|$).+/, '');
7
+ function reElement(node, details, track, href) {
8
+ let url = /https?/.test(href) ? new URL(href).pathname : href;
9
+ const filename = url.split('/').pop()?.replace(/(\?|!|\#|$).+/, '');
7
10
  node.properties = { ...properties, src: href };
8
11
  node.tagName = 'video';
9
12
  node.children = [];
10
- const { title = filename } = queryStringToObject(href);
13
+ const { title = filename, ...other } = queryStringToObject(href);
14
+ if (track) {
15
+ const trNode = trackNode(other);
16
+ trNode.forEach((tr) => node.children.push({ ...tr }));
17
+ }
11
18
  if (details) {
12
19
  const reNode = detailsNode(title);
13
20
  reNode.children.push({ ...node });
@@ -17,19 +24,17 @@ function reElement(node, details, href) {
17
24
  }
18
25
  }
19
26
  const RehypeVideo = (options) => {
20
- const { test = /\/(.*)(.mp4|.mov)$/, details = true } = options || {};
27
+ const { test = /\/(.*)(.mp4|.mov)$/, details = true, track = true } = options || {};
21
28
  return (tree) => {
22
29
  visit(tree, 'element', (node, index, parent) => {
23
30
  const isChecked = (str) => test.test(str.replace(/(\?|!|\#|$).+/g, '').toLocaleLowerCase());
24
31
  const child = node.children[0];
25
- const delimiter = /((?:https?:\/\/)(?:(?:[a-z0-9]?(?:[a-z0-9\-]{1,61}[a-z0-9])?\.[^\.|\s])+[a-z\.]*[a-z]+|(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3})(?::\d{1,5})*[a-z0-9.,_\/~#&=;%+?\-\\(\\)]*)/g;
26
- // const delimiter = /((?:https?:\/\/)?(?:(?:[a-z0-9]?(?:[a-z0-9\-]{1,61}[a-z0-9])?\.[^\.|\s])+[a-z\.]*[a-z]+|(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3})(?::\d{1,5})*[a-z0-9.,_\/~#&=;%+?\-\\(\\)]*)/g;
27
32
  if (node.tagName === 'p' && node.children.length === 1) {
28
33
  if (child.type === 'text' && delimiter.test(child.value) && isChecked(child.value)) {
29
- reElement(node, details, child.value);
34
+ reElement(node, details, track, child.value);
30
35
  }
31
36
  if (child.type === 'element' && child.tagName === 'a' && child.properties && typeof child.properties.href === 'string' && isChecked(child.properties.href)) {
32
- reElement(node, details, child.properties.href);
37
+ reElement(node, details, track, child.properties.href);
33
38
  }
34
39
  }
35
40
  });
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index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,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,gBAAgB,EAAE,EAAE,CAAC,CAAC,iBAAiB,EAAE,CAAC,CAAA;YACnG,MAAM,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;YAE/B,IAAI,IAAI,CAAC,OAAO,KAAK,GAAG,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBACvD,IAAI,KAAK,CAAC,IAAI,KAAK,MAAM,IAAI,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,SAAS,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;oBACnF,SAAS,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;gBAC/C,CAAC;gBACD,IAAI,KAAK,CAAC,IAAI,KAAK,SAAS,IAAI,KAAK,CAAC,OAAO,KAAK,GAAG,IAAI,KAAK,CAAC,UAAU,IAAI,OAAO,KAAK,CAAC,UAAU,CAAC,IAAI,KAAK,QAAQ,IAAI,SAAS,CAAC,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;oBAC3J,SAAS,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;gBACzD,CAAC;YACH,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAA;AACH,CAAC,CAAA;AAED,eAAe,WAAW,CAAC"}
@@ -0,0 +1,2 @@
1
+ import type { Element } from 'hast';
2
+ export declare function trackNode(query?: Record<string, string>): Element[];
@@ -0,0 +1,29 @@
1
+ export function trackNode(query = {}) {
2
+ let result = {};
3
+ let resultElement = [];
4
+ Object.keys(query).forEach((key) => {
5
+ let keyMatch = key.match(/track\[['"](\w+:?\w+)['"]\]/i);
6
+ let queryKey = keyMatch ? keyMatch[1] : null;
7
+ if (queryKey) {
8
+ let [lang, keyString] = queryKey.split(':');
9
+ if (!result[lang])
10
+ result[lang] = {};
11
+ const value = query[key];
12
+ result[lang][keyString ?? "src"] = value;
13
+ }
14
+ });
15
+ Object.keys(result).forEach((lang) => {
16
+ const track = result[lang];
17
+ resultElement.push({
18
+ type: 'element',
19
+ tagName: 'track',
20
+ properties: {
21
+ kind: track.kind || 'subtitles',
22
+ ...track
23
+ },
24
+ children: []
25
+ });
26
+ });
27
+ return resultElement;
28
+ }
29
+ //# sourceMappingURL=trackNode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trackNode.js","sourceRoot":"","sources":["../src/trackNode.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,SAAS,CAAC,QAAgC,EAAE;IAC1D,IAAI,MAAM,GAA2C,EAAE,CAAA;IACvD,IAAI,aAAa,GAAc,EAAE,CAAA;IACjC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;QACjC,IAAI,QAAQ,GAAG,GAAG,CAAC,KAAK,CAAC,8BAA8B,CAAC,CAAA;QACxD,IAAI,QAAQ,GAAG,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAA;QAC5C,IAAI,QAAQ,EAAE,CAAC;YACb,IAAI,CAAC,IAAI,EAAE,SAAS,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;YAC3C,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC;gBAAE,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAA;YACpC,MAAM,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,CAAA;YACxB,MAAM,CAAC,IAAI,CAAC,CAAC,SAAS,IAAI,KAAK,CAAC,GAAG,KAAK,CAAA;QAC1C,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;QACnC,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,CAAA;QAC1B,aAAa,CAAC,IAAI,CAAC;YACjB,IAAI,EAAE,SAAS;YACf,OAAO,EAAE,OAAO;YAChB,UAAU,EAAE;gBACV,IAAI,EAAE,KAAK,CAAC,IAAI,IAAI,WAAW;gBAC/B,GAAG,KAAK;aACT;YACD,QAAQ,EAAE,EAAE;SACb,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;IACF,OAAO,aAAa,CAAA;AACtB,CAAC"}
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "rehype-video",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "description": "Markdown supports video play with `.mp4` and `.mov` URLs.",
5
5
  "homepage": "https://jaywcjlove.github.io/rehype-video",
6
+ "funding": "https://jaywcjlove.github.io/#/sponsor",
6
7
  "sideEffects": false,
7
8
  "type": "module",
8
9
  "typings": "./lib/index.d.ts",
package/src/index.ts CHANGED
@@ -2,6 +2,7 @@ import type { Plugin } from 'unified';
2
2
  import type { Root, Element } from 'hast';
3
3
  import { visit } from 'unist-util-visit';
4
4
  import { detailsNode } from './detailsNode.js';
5
+ import { trackNode } from './trackNode.js';
5
6
 
6
7
  export type RehypeVideoOptions = {
7
8
  /**
@@ -14,8 +15,14 @@ export type RehypeVideoOptions = {
14
15
  * @default true
15
16
  */
16
17
  details?: boolean;
18
+ /**
19
+ * Support `<track>` tag to wrap <video>.
20
+ * @default true
21
+ */
22
+ track?: boolean;
17
23
  }
18
24
 
25
+ const delimiter = /((?:https?:\/\/)(?:(?:[a-z0-9]?(?:[a-z0-9\-]{1,61}[a-z0-9])?\.[^\.|\s])+[a-z\.]*[a-z]+|(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3})(?::\d{1,5})*[a-z0-9.,_\/~#&=;%+?\-\\(\\)]*)/g;
19
26
  const properties = { muted: 'muted', controls: 'controls', style: 'max-height:640px;' };
20
27
  const queryStringToObject = (url: string) =>
21
28
  [...new URLSearchParams(url.split('?!#')[1])].reduce(
@@ -23,12 +30,19 @@ const queryStringToObject = (url: string) =>
23
30
  {}
24
31
  );
25
32
 
26
- function reElement(node: Element, details: boolean, href: string) {
27
- const filename = href.split('/').pop()?.replace(/(\?|!|\#|$).+/, '');
33
+ function reElement(node: Element, details: boolean, track: boolean, href: string) {
34
+ let url = /https?/.test(href) ? new URL(href).pathname : href;
35
+ const filename = url.split('/').pop()?.replace(/(\?|!|\#|$).+/, '');
28
36
  node.properties = { ...properties, src: href };
29
37
  node.tagName = 'video';
30
38
  node.children = [];
31
- const { title = filename }= queryStringToObject(href);
39
+ const { title = filename, ...other }= queryStringToObject(href);
40
+
41
+ if (track) {
42
+ const trNode = trackNode(other);
43
+ trNode.forEach((tr) => node.children.push({ ...tr }));
44
+ }
45
+
32
46
  if (details) {
33
47
  const reNode = detailsNode(title);
34
48
  reNode.children.push({ ...node });
@@ -39,20 +53,18 @@ function reElement(node: Element, details: boolean, href: string) {
39
53
  }
40
54
 
41
55
  const RehypeVideo: Plugin<[RehypeVideoOptions?], Root> = (options) => {
42
- const { test = /\/(.*)(.mp4|.mov)$/, details = true } = options || {};
56
+ const { test = /\/(.*)(.mp4|.mov)$/, details = true, track = true } = options || {};
43
57
  return (tree) => {
44
58
  visit(tree, 'element', (node, index, parent) => {
45
59
  const isChecked = (str: string) => test.test(str.replace(/(\?|!|\#|$).+/g, '').toLocaleLowerCase())
46
60
  const child = node.children[0];
47
- const delimiter = /((?:https?:\/\/)(?:(?:[a-z0-9]?(?:[a-z0-9\-]{1,61}[a-z0-9])?\.[^\.|\s])+[a-z\.]*[a-z]+|(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3})(?::\d{1,5})*[a-z0-9.,_\/~#&=;%+?\-\\(\\)]*)/g;
48
- // const delimiter = /((?:https?:\/\/)?(?:(?:[a-z0-9]?(?:[a-z0-9\-]{1,61}[a-z0-9])?\.[^\.|\s])+[a-z\.]*[a-z]+|(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3})(?::\d{1,5})*[a-z0-9.,_\/~#&=;%+?\-\\(\\)]*)/g;
49
61
 
50
62
  if (node.tagName === 'p' && node.children.length === 1) {
51
63
  if (child.type === 'text' && delimiter.test(child.value) && isChecked(child.value)) {
52
- reElement(node, details, child.value);
64
+ reElement(node, details, track, child.value);
53
65
  }
54
66
  if (child.type === 'element' && child.tagName === 'a' && child.properties && typeof child.properties.href === 'string' && isChecked(child.properties.href)) {
55
- reElement(node, details, child.properties.href);
67
+ reElement(node, details, track, child.properties.href);
56
68
  }
57
69
  }
58
70
  });
@@ -0,0 +1,30 @@
1
+ import type { Element } from 'hast';
2
+
3
+ export function trackNode(query: Record<string, string> = {}): Element[] {
4
+ let result: Record<string, Record<string, string>> = {}
5
+ let resultElement: Element[] = []
6
+ Object.keys(query).forEach((key) => {
7
+ let keyMatch = key.match(/track\[['"](\w+:?\w+)['"]\]/i)
8
+ let queryKey = keyMatch ? keyMatch[1] : null
9
+ if (queryKey) {
10
+ let [lang, keyString] = queryKey.split(':')
11
+ if (!result[lang]) result[lang] = {}
12
+ const value = query[key]
13
+ result[lang][keyString ?? "src"] = value
14
+ }
15
+ });
16
+
17
+ Object.keys(result).forEach((lang) => {
18
+ const track = result[lang]
19
+ resultElement.push({
20
+ type: 'element',
21
+ tagName: 'track',
22
+ properties: {
23
+ kind: track.kind || 'subtitles',
24
+ ...track
25
+ },
26
+ children: []
27
+ })
28
+ })
29
+ return resultElement
30
+ }