Skip to content

🎥 Make videos programmatically with React

#6415

Ayushman8ec13183463f
diff --git a/packages/docs/docusaurus-theme-shiki-twoslash/client.js b/packages/docs/docusaurus-theme-shiki-twoslash/client.js
new file mode 100644
index 0000000000..c397535ee5
--- /dev/null
+++ b/packages/docs/docusaurus-theme-shiki-twoslash/client.js
@@ -0,0 +1,44 @@
+if (typeof window !== 'undefined') {
+ const handleCopy = async (e) => {
+ if (e.target.classList.contains('copy-button')) {
+ const button = e.target;
+ const pre = button.closest('pre');
+ if (!pre) return;
+
+ // Logic to extract code text.
+ // Shiki output usually has code > div.line
+ const codeElement = pre.querySelector('code');
+ let text = '';
+
+ if (codeElement) {
+ // Clone to avoid modifying DOM if we need to
+ const clone = codeElement.cloneNode(true);
+ // Remove line numbers if they exist? (Usually shiki doesn't include line numbers in textContent unless they are content)
+ text = clone.textContent;
+ } else {
+ // Fallback
+ text = pre.innerText;
+ // Remove the "Copy" button text from the end if it was included
+ // But button is a child of pre.
+ // clone the pre excluding the button
+ const clone = pre.cloneNode(true);
+ const buttonInClone = clone.querySelector('.copy-button');
+ if (buttonInClone) buttonInClone.remove();
+ text = clone.textContent;
+ }
+
+ try {
+ await navigator.clipboard.writeText(text);
+ const originalText = button.innerText;
+ button.innerText = 'Copied';
+ setTimeout(() => {
+ button.innerText = originalText;
+ }, 2000);
+ } catch (err) {
+ console.error('Failed to copy!', err);
+ }
+ }
+ };
+
+ document.addEventListener('click', handleCopy);
+}
diff --git a/packages/docs/docusaurus-theme-shiki-twoslash/index.js b/packages/docs/docusaurus-theme-shiki-twoslash/index.js
index 9d1de67394..c37e61a51e 100644
--- a/packages/docs/docusaurus-theme-shiki-twoslash/index.js
+++ b/packages/docs/docusaurus-theme-shiki-twoslash/index.js
@@ -1,15 +1,18 @@
-const path = require("path");
+const path = require('path');
/**
* @returns {import("@docusaurus/types").Plugin}
*/
function theme() {
- return {
- name: "docusaurus-theme-shiki-twoslash",
- getThemePath() {
- return path.resolve(__dirname, "./theme");
- },
- };
+ return {
+ name: 'docusaurus-theme-shiki-twoslash',
+ getThemePath() {
+ return path.resolve(__dirname, './theme');
+ },
+ getClientModules() {
+ return [path.resolve(__dirname, './client.js')];
+ },
+ };
}
module.exports = theme;
diff --git a/packages/docs/docusaurus-theme-shiki-twoslash/theme/CodeBlock/index.js b/packages/docs/docusaurus-theme-shiki-twoslash/theme/CodeBlock/index.js
index 4360422c4e..e96ad20a0d 100644
--- a/packages/docs/docusaurus-theme-shiki-twoslash/theme/CodeBlock/index.js
+++ b/packages/docs/docusaurus-theme-shiki-twoslash/theme/CodeBlock/index.js
@@ -1,64 +1,104 @@
// Based on https://github.com/facebook/docusaurus/blob/ed9d2a26f5a7b8096804ae1b3a4fffc504f8f90d/packages/docusaurus-theme-classic/src/theme/CodeBlock/index.tsx
// which is under MIT License as per the banner
-import "./styles.css";
+import './styles.css';
-import Translate, { translate } from "@docusaurus/Translate";
-import copy from "copy-text-to-clipboard";
-import React, { useRef, useState } from "react";
+import Translate, {translate} from '@docusaurus/Translate';
+import copy from 'copy-text-to-clipboard';
+import React, {useRef, useState} from 'react';
-const CodeBlock = ({ children, ...props }) => {
- const pre = useRef(null);
- const [showCopied, setShowCopied] = useState(false);
+const CodeBlock = ({children, ...props}) => {
+ const pre = useRef(null);
+ const [showCopied, setShowCopied] = useState(false);
- const handleCopyCode = () => {
- if (pre.current) {
- const elementToSelect = pre.current.querySelector("code div.line")?.length
- ? "code div.line"
- : "div div.line";
+ const handleCopyCode = () => {
+ if (pre.current) {
+ const elementToSelect = pre.current.querySelector('code div.line')
+ ? 'code div.line'
+ : 'div div.line';
- copy(
- Array.from(pre.current.querySelectorAll(elementToSelect))
- .map((el) => el.textContent)
- .join("\n"),
- );
- }
+ copy(
+ Array.from(pre.current.querySelectorAll(elementToSelect))
+ .map((el) => el.textContent)
+ .join('\n'),
+ );
+ }
- setShowCopied(true);
- setTimeout(() => setShowCopied(false), 2000);
- };
+ setShowCopied(true);
+ setTimeout(() => setShowCopied(false), 2000);
+ };
- return (
- <pre {...props} ref={pre}>
- {children}
- <button
- type="button"
- aria-label={translate({
- id: "theme.CodeBlock.copyButtonAriaLabel",
- message: "Copy code to clipboard",
- description: "The ARIA label for copy code blocks button",
- })}
- className="copy-button"
- onClick={handleCopyCode}
- >
- {showCopied ? (
- <Translate
- id="theme.CodeBlock.copied"
- description="The copied button label on code blocks"
- >
- Copied
- </Translate>
- ) : (
- <Translate
- id="theme.CodeBlock.copy"
- description="The copy button label on code blocks"
- >
- Copy
- </Translate>
- )}
- </button>
- </pre>
- );
+ const {dangerouslySetInnerHTML, ...restProps} = props;
+
+ if (dangerouslySetInnerHTML) {
+ return (
+ <div className="shiki-container" style={{position: 'relative'}}>
+ <pre
+ {...restProps}
+ dangerouslySetInnerHTML={dangerouslySetInnerHTML}
+ ref={pre}
+ />
+ <button
+ type="button"
+ aria-label={translate({
+ id: 'theme.CodeBlock.copyButtonAriaLabel',
+ message: 'Copy code to clipboard',
+ description: 'The ARIA label for copy code blocks button',
+ })}
+ className="copy-button"
+ onClick={handleCopyCode}
+ >
+ {showCopied ? (
+ <Translate
+ id="theme.CodeBlock.copied"
+ description="The copied button label on code blocks"
+ >
+ Copied
+ </Translate>
+ ) : (
+ <Translate
+ id="theme.CodeBlock.copy"
+ description="The copy button label on code blocks"
+ >
+ Copy
+ </Translate>
+ )}
+ </button>
+ </div>
+ );
+ }
+
+ return (
+ <pre {...props} ref={pre}>
+ {children}
+ <button
+ type="button"
+ aria-label={translate({
+ id: 'theme.CodeBlock.copyButtonAriaLabel',
+ message: 'Copy code to clipboard',
+ description: 'The ARIA label for copy code blocks button',
+ })}
+ className="copy-button"
+ onClick={handleCopyCode}
+ >
+ {showCopied ? (
+ <Translate
+ id="theme.CodeBlock.copied"
+ description="The copied button label on code blocks"
+ >
+ Copied
+ </Translate>
+ ) : (
+ <Translate
+ id="theme.CodeBlock.copy"
+ description="The copy button label on code blocks"
+ >
+ Copy
+ </Translate>
+ )}
+ </button>
+ </pre>
+ );
};
export default CodeBlock;
diff --git a/packages/docusaurus-plugin/src/shiki.ts b/packages/docusaurus-plugin/src/shiki.ts
index da4abdc1de..40c105d557 100644
--- a/packages/docusaurus-plugin/src/shiki.ts
+++ b/packages/docusaurus-plugin/src/shiki.ts
@@ -13,7 +13,7 @@ import {setupNodeForTwoslashException} from './exceptionMessageDOM';
import {addIncludes, replaceIncludesInCode} from './includes';
import type {Node} from './unist-types';
-type OBJECT = Record<string, any>;
+type OBJECT = Record<string, unknown>;
type Fence = {
lang: string;
@@ -21,7 +21,7 @@ type Fence = {
};
// A set of includes which can be pulled via a set ID
-const includes = new Map();
+const includes = new Map<string, string>();
function getHTML({
code,
@@ -63,14 +63,18 @@ function getHTML({
.split('/')
.pop()
.replace('.json', '');
- return renderCodeToHTML(
+ const html = renderCodeToHTML(
code,
fence.lang,
- fence.meta,
+ fence.meta as any,
{themeName, ...twoslashSettings},
highlighter,
twoslash,
);
+ return html.replace(
+ '</pre>',
+ '<button class="copy-button" aria-label="Copy code to clipboard">Copy</button></pre>',
+ );
});
results = output.join('\n');
if (highlighters.length > 1 && twoslashSettings.wrapFragments) {
@@ -87,7 +91,7 @@ function getHTML({
*/
export const runTwoSlashOnNode = (
code: string,
- {lang, meta}: {lang: string; meta: Record<string, string>},
+ {lang, meta}: {lang: string; meta: Record<string, unknown>},
settings = {},
) => {
// Only run twoslash when the meta has the attribute twoslash
@@ -222,6 +226,7 @@ const remarkVisitor =
twoslash,
twoslashSettings,
});
+
// @ts-expect-error
node.type = 'mdxJsxFlowElement';
node.name = 'div';