Skip to content

🎥 Make videos programmatically with React

fix or suppress all errors

Jonny Burger64b578a590e6
diff --git a/packages/docusaurus-plugin/package.json b/packages/docusaurus-plugin/package.json
index 0cc955d9ce..d464fa7c89 100644
--- a/packages/docusaurus-plugin/package.json
+++ b/packages/docusaurus-plugin/package.json
@@ -4,8 +4,9 @@
"unified": "^11.0.5",
"unist-util-visit": "^5.0.0",
"fenceparser": "^2.2.0",
- "shiki": "0.14.3",
+ "shiki": "0.10.1",
"@typescript/twoslash": "3.2.1",
- "@docusaurus/types": "3.4.0"
+ "@docusaurus/types": "3.4.0",
+ "@types/unist": "3.0.2"
}
}
diff --git a/packages/docusaurus-plugin/src/exceptionMessageDOM.ts b/packages/docusaurus-plugin/src/exceptionMessageDOM.ts
index 32122dbbb2..c9751620f9 100644
--- a/packages/docusaurus-plugin/src/exceptionMessageDOM.ts
+++ b/packages/docusaurus-plugin/src/exceptionMessageDOM.ts
@@ -1,10 +1,15 @@
-const {TwoslashError} = require('@typescript/twoslash');
+import {TwoslashError} from '@typescript/twoslash';
+import {Node} from './unist-types';
export function escapeHtml(html: string) {
return html.replace(/</g, '&lt;').replace(/>/g, '&gt;');
}
-export const setupNodeForTwoslashException = (code: string, node, error) => {
+export const setupNodeForTwoslashException = (
+ code: string,
+ node: Node,
+ error: Error,
+) => {
const css = `<style>
@import url('http://fonts.cdnfonts.com/css/caslon-os');
@@ -83,7 +88,7 @@ export const setupNodeForTwoslashException = (code: string, node, error) => {
}
</style>`;
- const bodyFromTwoslashError = (err) => {
+ const bodyFromTwoslashError = (err: TwoslashError) => {
return `
<h3>${escapeHtml(err.title)}</h3>
<p>${escapeHtml(err.description).replace(/(?:\r\n|\r|\n)/g, '<br>')}</p>
@@ -94,7 +99,7 @@ export const setupNodeForTwoslashException = (code: string, node, error) => {
`;
};
- const bodyFromError = (err) => {
+ const bodyFromError = (err: Error) => {
return `<pre><code>${err.message.split('## Code')[0]}</code></pre>`;
};
@@ -140,7 +145,8 @@ export const setupNodeForTwoslashException = (code: string, node, error) => {
<div class='twoslash-exception-message'>${body}${codeSample}</div>`;
node.type = 'html';
+ // @ts-expect-error
node.value = "<div id='twoslash-error'>" + css + html + '</div>';
-
+ // @ts-expect-error
node.children = [];
};
diff --git a/packages/docusaurus-plugin/src/includes.ts b/packages/docusaurus-plugin/src/includes.ts
index f67a801d07..1a91feecc7 100644
--- a/packages/docusaurus-plugin/src/includes.ts
+++ b/packages/docusaurus-plugin/src/includes.ts
@@ -1,5 +1,9 @@
-export const addIncludes = (map, name, code) => {
- const lines = [];
+export const addIncludes = (
+ map: Map<string, string>,
+ name: string,
+ code: string,
+) => {
+ const lines: string[] = [];
code.split('\n').forEach((l) => {
const trimmed = l.trim();
@@ -14,7 +18,10 @@ export const addIncludes = (map, name, code) => {
map.set(name, lines.join('\n'));
};
-export const replaceIncludesInCode = (_map, code) => {
+export const replaceIncludesInCode = (
+ _map: Map<string, string>,
+ code: string,
+) => {
const includes = /\/\/ @include: (.*)$/gm;
// Basically run a regex over the code replacing any // @include: thing with
@@ -47,7 +54,9 @@ export const replaceIncludesInCode = (_map, code) => {
// Go backwards through the found changes so that we can retain index position
toReplace.reverse().forEach((r) => {
newCode =
- newCode.substring(0, r[0]) + r[2] + newCode.substring(r[0] + r[1]);
+ newCode.substring(0, Number(r[0])) +
+ r[2] +
+ newCode.substring(Number(r[0]) + Number(r[1]));
});
return newCode;
};
diff --git a/packages/docusaurus-plugin/src/shiki.ts b/packages/docusaurus-plugin/src/shiki.ts
index 9d39ab5574..9ab9b51c37 100644
--- a/packages/docusaurus-plugin/src/shiki.ts
+++ b/packages/docusaurus-plugin/src/shiki.ts
@@ -3,7 +3,10 @@ import {TwoslashError} from '@typescript/twoslash';
import {lex, parse} from 'fenceparser';
import {Highlighter, getHighlighter} from 'shiki';
import {UserConfigSettings, renderCodeToHTML} from 'shiki-twoslash';
-import {Visitor, visit} from 'unist-util-visit';
+import {Node} from 'unist';
+import {visit} from 'unist-util-visit';
+import {BuildVisitor, UnistNode} from 'unist-util-visit/lib';
+import {cachedTwoslashCall} from './caching';
const {setupNodeForTwoslashException} = require('./exceptionMessageDOM');
const {addIncludes, replaceIncludesInCode} = require('./includes');
@@ -21,17 +24,15 @@ const includes = new Map();
function getHTML(
code: string,
fence: Fence,
- highlighters: any[],
+ highlighters: Highlighter[],
twoslash: any,
twoslashSettings: UserConfigSettings,
) {
// Shiki doesn't respect json5 as an input, so switch it
// to json, which can handle comments in the syntax highlight
- const replacer = {
- json5: 'json',
- };
-
- if (replacer[fence.lang]) fence.lang = replacer[fence.lang];
+ if (fence.lang === 'json5') {
+ fence.lang = 'json';
+ }
let results;
// Support 'twoslash' includes
@@ -49,6 +50,7 @@ function getHTML(
} else {
// All good, get each highlighter and render the shiki output for it
const output = highlighters.map((highlighter) => {
+ // @ts-expect-error
const themeName = highlighter.customName
.split('/')
.pop()
@@ -75,7 +77,11 @@ function getHTML(
* Runs twoslash across an AST node, switching out the text content, and lang
* and adding a `twoslash` property to the node.
*/
-export const runTwoSlashOnNode = (code, {lang, meta}, settings = {}) => {
+export const runTwoSlashOnNode = (
+ code: string,
+ {lang, meta}: {lang: string; meta: Record<string, string>},
+ settings = {},
+) => {
// Offer a way to do high-perf iterations, this is less useful
// given that we cache the results of twoslash in the file-system
const shouldDisableTwoslash =
@@ -97,7 +103,9 @@ export const runTwoSlashOnNode = (code, {lang, meta}, settings = {}) => {
const highlighterCache = new Map();
/** Sets up the highlighters, and cache's for recalls */
-export const highlightersFromSettings = (settings): Promise<Highlighter[]> => {
+export const highlightersFromSettings = (
+ settings: UserConfigSettings,
+): Promise<Highlighter[]> => {
// console.log("i should only log once per theme")
// ^ uncomment this to debug if required
const themes =
@@ -106,13 +114,13 @@ export const highlightersFromSettings = (settings): Promise<Highlighter[]> => {
return Promise.all(
themes.map(async (theme) => {
// You can put a string, a path, or the JSON theme obj
- const themeName = theme.name || theme;
+ const themeName = typeof theme === 'string' ? theme : theme.name;
const highlighter = await getHighlighter({
...settings,
theme,
themes: undefined,
});
-
+ // @ts-expect-error
highlighter.customName = themeName;
return highlighter;
}),
@@ -149,12 +157,12 @@ const parseFence = (fence: string): Fence => {
* Synchronous outer function, async inner function, which is how the remark
* async API works.
*/
-export function remarkTwoslash(settings = {}) {
+export function remarkTwoslash(settings: UserConfigSettings = {}) {
if (!highlighterCache.has(settings)) {
highlighterCache.set(settings, highlightersFromSettings(settings));
}
- const transform = async (markdownAST) => {
+ const transform = async (markdownAST: UnistNode) => {
const highlighters = await highlighterCache.get(settings);
parsingNewFile();
visit(markdownAST, 'code', remarkVisitor(highlighters, settings));
@@ -167,12 +175,16 @@ export function remarkTwoslash(settings = {}) {
* The function doing the work of transforming any codeblock samples in a remark AST.
*/
const remarkVisitor =
- (highlighters, twoslashSettings = {}): Visitor =>
+ (
+ highlighters: Highlighter[],
+ twoslashSettings: UserConfigSettings = {},
+ ): BuildVisitor<Node, 'code'> =>
(node) => {
const code = node;
let fence;
try {
+ // @ts-expect-error
fence = parseFence([node.lang, node.meta].filter(Boolean).join(' '));
} catch (error) {
const twoslashError = new TwoslashError(
@@ -187,7 +199,6 @@ const remarkVisitor =
// Do nothing if the node has an attribute to ignore
if (
Object.keys(fence.meta).filter((key) =>
- // @ts-expect-error
(twoslashSettings.ignoreCodeblocksWithCodefenceMeta || []).includes(
key,
),
@@ -200,6 +211,7 @@ const remarkVisitor =
try {
// By allowing node.twoslash to already exist you can set it up yourself in a browser
twoslash =
+ // @ts-expect-error
node.twoslash || runTwoSlashOnNode(code, fence, twoslashSettings);
} catch (error) {
const shouldAlwaysRaise =
@@ -209,7 +221,6 @@ const remarkVisitor =
if (
(shouldAlwaysRaise && yeahButNotInTests) ||
- // @ts-expect-error
twoslashSettings.alwayRaiseForTwoslashExceptions
) {
throw error;
@@ -219,20 +230,27 @@ const remarkVisitor =
}
if (twoslash) {
+ // @ts-expect-error
node.value = twoslash.code;
+ // @ts-expect-error
node.lang = twoslash.extension;
+ // @ts-expect-error
node.twoslash = twoslash;
}
const shikiHTML = getHTML(
+ // @ts-expect-error
node.value,
fence,
highlighters,
twoslash,
twoslashSettings,
);
+ // @ts-expect-error
node.type = 'html';
+ // @ts-expect-error
node.value = shikiHTML;
+ // @ts-expect-error
node.children = [];
};
@@ -243,7 +261,7 @@ const remarkVisitor =
export const transformAttributesToHTML = (
code: string,
fenceString: string,
- highlighters,
+ highlighters: Highlighter[],
settings: UserConfigSettings,
) => {
const fence = parseFence(fenceString);
diff --git a/packages/docusaurus-plugin/src/unist-types.ts b/packages/docusaurus-plugin/src/unist-types.ts
new file mode 100644
index 0000000000..271b42cab7
--- /dev/null
+++ b/packages/docusaurus-plugin/src/unist-types.ts
@@ -0,0 +1,119 @@
+// ## Interfaces
+
+/**
+ * Info associated with nodes by the ecosystem.
+ *
+ * This space is guaranteed to never be specified by unist or specifications
+ * implementing unist.
+ * But you can use it in utilities and plugins to store data.
+ *
+ * This type can be augmented to register custom data.
+ * For example:
+ *
+ * ```ts
+ * declare module 'unist' {
+ * interface Data {
+ * // `someNode.data.myId` is typed as `number | undefined`
+ * myId?: number | undefined
+ * }
+ * }
+ * ```
+ */
+export interface Data {}
+
+/**
+ * One place in a source file.
+ */
+export interface Point {
+ /**
+ * Line in a source file (1-indexed integer).
+ */
+ line: number;
+
+ /**
+ * Column in a source file (1-indexed integer).
+ */
+ column: number;
+ /**
+ * Character in a source file (0-indexed integer).
+ */
+ offset?: number | undefined;
+}
+
+/**
+ * Position of a node in a source document.
+ *
+ * A position is a range between two points.
+ */
+export interface Position {
+ /**
+ * Place of the first character of the parsed source region.
+ */
+ start: Point;
+
+ /**
+ * Place of the first character after the parsed source region.
+ */
+ end: Point;
+}
+
+// ## Abstract nodes
+
+/**
+ * Abstract unist node that contains the smallest possible value.
+ *
+ * This interface is supposed to be extended.
+ *
+ * For example, in HTML, a `text` node is a leaf that contains text.
+ */
+export interface Literal extends Node {
+ /**
+ * Plain value.
+ */
+ value: unknown;
+}
+
+/**
+ * Abstract unist node.
+ *
+ * The syntactic unit in unist syntax trees are called nodes.
+ *
+ * This interface is supposed to be extended.
+ * If you can use {@link Literal} or {@link Parent}, you should.
+ * But for example in markdown, a `thematicBreak` (`***`), is neither literal
+ * nor parent, but still a node.
+ */
+export interface Node {
+ /**
+ * Node type.
+ */
+ type: string;
+
+ /**
+ * Info from the ecosystem.
+ */
+ data?: Data | undefined;
+
+ /**
+ * Position of a node in a source document.
+ *
+ * Nodes that are generated (not in the original source document) must not
+ * have a position.
+ */
+ position?: Position | undefined;
+}
+
+/**
+ * Abstract unist node that contains other nodes (*children*).
+ *
+ * This interface is supposed to be extended.
+ *
+ * For example, in XML, an element is a parent of different things, such as
+ * comments, text, and further elements.
+ */
+export interface Parent extends Node {
+ /**
+ * List of children.
+ */
+ children: Node[];
+}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 9ef5f8ce8f..cac5523f29 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -610,6 +610,9 @@ importers:
'@docusaurus/types':
specifier: 3.4.0
+ '@types/unist':
+ specifier: 3.0.2
+ version: 3.0.2
'@typescript/twoslash':
specifier: 3.2.1
version: 3.2.1
@@ -617,8 +620,8 @@ importers:
specifier: ^2.2.0
version: 2.2.0
shiki:
- specifier: 0.14.3
- version: 0.14.3
+ specifier: 0.10.1
+ version: 0.10.1
shiki-twoslash:
specifier: 3.1.2
version: 3.1.2([email protected])
@@ -10956,10 +10959,6 @@ packages:
engines: {node: '>=12'}
dev: false
- resolution: {integrity: sha512-vJXt3yiaUL4UU546s3rPXlsry/RnM730G1+HkpKE012AN0sx1eOrxSu95oKDIonskeLTijMgqWZ3uDEe3NFvyg==}
- dev: false
-
resolution: {integrity: sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==}
engines: {node: '>=4'}
@@ -21632,15 +21631,6 @@ packages:
vscode-textmate: 6.0.0
dev: false
- resolution: {integrity: sha512-U3S/a+b0KS+UkTyMjoNojvTgrBHjgp7L6ovhFVZsXmBGnVdQ4K4U9oK0z63w538S91ATngv1vXigHCSWOwnr+g==}
- dependencies:
- ansi-sequence-parser: 1.1.1
- jsonc-parser: 3.2.0
- vscode-oniguruma: 1.7.0
- vscode-textmate: 8.0.0
- dev: false
-
resolution: {integrity: sha512-fDW/EZ6Q9RiO8eFG8Hj+7u/oW+XrPTIChwCOM2+th2A6OblDtYYIpve9m+KvI9Z4C9qSEXlaGR6bTEYHReuglA==}
engines: {node: '>= 0.4'}
@@ -23851,10 +23841,6 @@ packages:
resolution: {integrity: sha512-gu73tuZfJgu+mvCSy4UZwd2JXykjK9zAZsfmDeut5dx/1a7FeTk0XwJsSuqQn+cuMCGVbIBfl+s53X4T19DnzQ==}
dev: false
- resolution: {integrity: sha512-AFbieoL7a5LMqcnOF04ji+rpXadgOXnZsxQr//r83kLPr7biP7am3g9zbaZIaBGwBRWeSvoMD4mgPdX3e4NWBg==}
- dev: false
-
resolution: {integrity: sha512-8TEXQxlldWAuIODdukIb+TR5s+9Ds40eSJrw+1iDDA9IFORPjMELarNQE3myz5XIkWWpdprmJjm1/SxMlWOC8A==}
dev: false