generated from ryansonshine/typescript-npm-package-template
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from sillsdev/60-change-video-embed
fix: Use video player only for embedded videos (#60)
- Loading branch information
Showing
8 changed files
with
153 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { setLogLevel } from "../log"; | ||
import { NotionBlock } from "../types"; | ||
import { standardVideoTransformer } from "./VideoTransformer"; | ||
import { blocksToMarkdown } from "./pluginTestRun"; | ||
|
||
test("youtube embedded", async () => { | ||
const config = { plugins: [standardVideoTransformer] }; | ||
const result = await blocksToMarkdown(config, [ | ||
{ | ||
object: "block", | ||
type: "video", | ||
video: { | ||
caption: [ | ||
{ | ||
type: "text", | ||
text: { | ||
content: "A video about editing in Notion", | ||
link: null, | ||
}, | ||
plain_text: "A video about editing in Notion", | ||
href: null, | ||
}, | ||
], | ||
type: "external", | ||
external: { url: "https://www.youtube.com/watch?v=FXIrojSK3Jo" }, | ||
}, | ||
} as unknown as NotionBlock, | ||
]); | ||
expect(result).toContain(`import ReactPlayer from "react-player";`); | ||
expect(result).toContain( | ||
`<ReactPlayer controls url="https://www.youtube.com/watch?v=FXIrojSK3Jo" />` | ||
); | ||
}); | ||
|
||
test("vimeo embedded", async () => { | ||
setLogLevel("verbose"); | ||
const config = { plugins: [standardVideoTransformer] }; | ||
const result = await blocksToMarkdown(config, [ | ||
{ | ||
object: "block", | ||
type: "video", | ||
video: { | ||
caption: [], | ||
type: "external", | ||
external: { url: "https://vimeo.com/4613611xx" }, | ||
}, | ||
} as unknown as NotionBlock, | ||
]); | ||
expect(result).toContain(`import ReactPlayer from "react-player";`); | ||
expect(result).toContain( | ||
`<ReactPlayer controls url="https://vimeo.com/4613611xx" />` | ||
); | ||
}); | ||
|
||
test("video link, not embedded", async () => { | ||
setLogLevel("verbose"); | ||
const config = { plugins: [standardVideoTransformer] }; | ||
const result = await blocksToMarkdown(config, [ | ||
{ | ||
object: "block", | ||
type: "paragraph", | ||
paragraph: { | ||
rich_text: [ | ||
{ | ||
type: "text", | ||
text: { | ||
content: "https://vimeo.com/4613611xx", | ||
link: { | ||
url: "https://vimeo.com/4613611xx", | ||
}, | ||
}, | ||
annotations: { | ||
code: false, | ||
}, | ||
plain_text: "https://vimeo.com/4613611xx", | ||
href: "https://vimeo.com/4613611xx", | ||
}, | ||
], | ||
color: "default", | ||
}, | ||
} as unknown as NotionBlock, | ||
]); | ||
expect(result).toContain( | ||
"[https://vimeo.com/4613611xx](https://vimeo.com/4613611xx)" | ||
); | ||
expect(result).not.toContain(`import`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { VideoBlockObjectResponse } from "@notionhq/client/build/src/api-endpoints"; | ||
import { IDocuNotionContext, IPlugin } from "./pluginTypes"; | ||
import { warning } from "../log"; | ||
import { NotionBlock } from "../types"; | ||
|
||
export const standardVideoTransformer: IPlugin = { | ||
name: "video", | ||
notionToMarkdownTransforms: [ | ||
{ | ||
type: "video", | ||
getStringFromBlock: ( | ||
context: IDocuNotionContext, | ||
block: NotionBlock | ||
): string => { | ||
const video = (block as VideoBlockObjectResponse).video; | ||
if (video.type === "external") { | ||
if (!context.imports) context.imports = []; | ||
context.imports.push(`import ReactPlayer from "react-player";`); | ||
return `<ReactPlayer controls url="${video.external.url}" />`; | ||
} else { | ||
warning( | ||
`[standardVideoTransformer] Found Notion "video" block with type ${video.type}. The best docu-notion can do for now is ignore it.` | ||
); | ||
} | ||
return ""; | ||
}, | ||
}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters