Skip to content

Commit

Permalink
feat: add callback support for test parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Sep 5, 2024
1 parent 910f126 commit 1aa4b48
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type RehypeVideoOptions = {
* URL suffix verification.
* @default /\/(.*)(.mp4|.mov)$/
*/
test?: RegExp;
test?: RegExp | ((url: string) => boolean);
/**
* Support `<details>` tag to wrap <video>.
* @default true
Expand Down Expand Up @@ -65,10 +65,14 @@ function reElement(node: Element, details: boolean, track: boolean, href: string
}

const RehypeVideo: Plugin<[RehypeVideoOptions?], Root> = (options) => {
const { test = /\/(.*)(.mp4|.mov)$/, details = true, track = true } = options || {};
const { test = /\/(.*)(.mp4|.mov)$/i, details = true, track = true } = options || {};
return (tree) => {
visit(tree, 'element', (node, index, parent) => {
const isChecked = (str: string) => test.test(str.replace(/(\?|!|\#|$).+/g, '').toLocaleLowerCase())
const isChecked = (str: string) => {
if (test instanceof RegExp) return test.test(str.replace(/(\?|!|\#|$).+/g, '').toLocaleLowerCase())
if (typeof test === 'function') return test(str)
return false;
}
const child = node.children[0];

if (node.tagName === 'p' && node.children.length === 1) {
Expand Down
32 changes: 32 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,37 @@ import remarkParse from 'remark-parse';
import stringify from 'rehype-stringify';
import rehypeVideo from '../src/index.js';

it('rehype-video `test:((url: string) => boolean)` test case', () => {
const mrkStr = `https://github.com/user-attachments/assets/0d808e2e-84c7-46ca-a220-440fa9f34118?title=rehype-video&rehype=video`;
const expected = `<details open class=\"octicon octicon-video\"><summary><svg aria-hidden height=\"16\" width=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" data-view-component class=\"octicon octicon-device-camera-video\"><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></svg><span aria-label=\"Video description rehype-video\">rehype-video</span><span class=\"dropdown-caret\"></span></summary><video muted controls style=\"max-height:640px;\" src=\"https://github.com/user-attachments/assets/0d808e2e-84c7-46ca-a220-440fa9f34118?title=rehype-video&#x26;rehype=video\"></video></details>`
const htmlStr = unified()
.use(remarkParse)
.use(remark2rehype, { allowDangerousHtml: true })
.use(rehypeVideo, {
test: (url) => {
return /\.(mp4|mov)|[?&]rehype=video/i.test(url);
}
})
.use(stringify)
.processSync(mrkStr)
.toString();
expect(htmlStr).toEqual(expected);
});

it('rehype-video `test:((url: string) => boolean)` boundary test case', () => {
const mrkStr = `https://github.com/user-attachments/assets/0d808e2e-84c7-46ca-a220-440fa9f34118?title=rehype-video&rehype=video`;
const htmlStr2 = unified()
.use(remarkParse)
.use(remark2rehype, { allowDangerousHtml: true })
// This is a boundary test, not the correct `rehypeVideo` option.
// @ts-ignore
.use(rehypeVideo, { test: false })
.use(stringify)
.processSync(mrkStr)
.toString();
expect(htmlStr2).toEqual("<p>https://github.com/user-attachments/assets/0d808e2e-84c7-46ca-a220-440fa9f34118?title=rehype-video&#x26;rehype=video</p>");
});

it('rehype-video title test case', () => {
const mrkStr = `https://github.com/002.mp4?title=rehype-video`;
const expected = `<details open class=\"octicon octicon-video\"><summary><svg aria-hidden height=\"16\" width=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" data-view-component class=\"octicon octicon-device-camera-video\"><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></svg><span aria-label=\"Video description rehype-video\">rehype-video</span><span class=\"dropdown-caret\"></span></summary><video muted controls style=\"max-height:640px;\" src=\"https://github.com/002.mp4?title=rehype-video\"></video></details>`
Expand All @@ -19,6 +50,7 @@ it('rehype-video title test case', () => {
expect(htmlStr).toEqual(expected);
});


it('rehype-video 1 test case', () => {
const mrkStr = `
https://github.com/001.mp4 hi!
Expand Down

0 comments on commit 1aa4b48

Please sign in to comment.