Skip to content

Commit 73f409d

Browse files
authored
[Docs Site] various typescript improvements (#18006)
* fix: various typescript improvements * fix: mdxTextExpression comparison
1 parent b33ba08 commit 73f409d

File tree

8 files changed

+49
-21
lines changed

8 files changed

+49
-21
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ jobs:
3737
NODE_OPTIONS: "--max-old-space-size=4192"
3838
RUN_LINK_CHECK: true
3939

40+
- run: npm run check:worker
41+
4042
- uses: actions/cache/save@v4
4143
with:
4244
path: |

.prettierrc.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-check
12
/** @type {import("prettier").Config} */
23
export default {
34
plugins: ["prettier-plugin-astro"],

ec.config.mjs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-check
12
import darkTheme from "solarflare-theme/themes/cloudflare-dark-color-theme.json" with { type: "json" };
23
import lightTheme from "solarflare-theme/themes/cloudflare-light-color-theme.json" with { type: "json" };
34

@@ -6,6 +7,9 @@ import { h } from "@expressive-code/core/hast";
67

78
import lzstring from "lz-string";
89

10+
/**
11+
* @param {string} code
12+
*/
913
export function serialiseWorker(code) {
1014
const formData = new FormData();
1115

@@ -29,6 +33,9 @@ export function serialiseWorker(code) {
2933
return formData;
3034
}
3135

36+
/**
37+
* @param {FormData} worker
38+
*/
3239
export async function compressWorker(worker) {
3340
const serialisedWorker = new Response(worker);
3441
return lzstring.compressToEncodedURIComponent(
@@ -92,7 +99,10 @@ function outputCodeblocks() {
9299
},
93100
postprocessRenderedBlock: async (context) => {
94101
if (!context.codeBlock.meta.includes("output")) return;
95-
context.renderData.blockAst.properties.className.push("code-output");
102+
context.renderData.blockAst.properties.className ??= [];
103+
if (Array.isArray(context.renderData.blockAst.properties.className)) {
104+
context.renderData.blockAst.properties.className.push("code-output");
105+
}
96106
context.addStyles(`
97107
div.expressive-code:has(figure.code-output) {
98108
margin-top: 0 !important;

package-lock.json

Lines changed: 8 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"check": "npm run check:functions && npm run check:astro",
1111
"check:astro": "npm run sync && astro check",
1212
"check:functions": "npx tsc --noEmit -p ./functions/tsconfig.json",
13+
"check:worker": "npx tsc --noEmit -p ./worker/tsconfig.json",
1314
"dev": "npx astro dev",
1415
"format": "npx prettier --write \"**/*.{js,jsx,ts,tsx,mjs,astro,css,json,yaml,yml}\"",
1516
"postinstall": "npx patch-package && npm run sync",
@@ -35,6 +36,7 @@
3536
"@codingheads/sticky-header": "^1.0.2",
3637
"@stoplight/json-schema-tree": "^4.0.0",
3738
"@types/dompurify": "^3.0.5",
39+
"@types/hast": "^3.0.4",
3840
"@types/he": "^1.2.3",
3941
"@types/node": "^22.8.7",
4042
"@types/react": "^18.3.12",
@@ -56,6 +58,7 @@
5658
"instantsearch.js": "^4.75.4",
5759
"lz-string": "^1.5.0",
5860
"marked": "^14.1.3",
61+
"mdast-util-mdx-expression": "^2.0.1",
5962
"mermaid": "^11.4.0",
6063
"node-html-parser": "^6.1.13",
6164
"patch-package": "^8.0.0",
@@ -83,7 +86,7 @@
8386
"typescript": "^5.6.3",
8487
"unist-util-visit": "^5.0.0",
8588
"vitest": "2.1.4",
86-
"wrangler": "^3.78.10",
89+
"wrangler": "^3.84.1",
8790
"yaml": "^2.6.0"
8891
},
8992
"engines": {

plugins/rehype/heading-slugs.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
11
import { toString } from "hast-util-to-string";
22
import { visit } from "unist-util-visit";
33
import GithubSlugger from "github-slugger";
4+
import type { Root } from "hast";
5+
import type { MdxTextExpression } from "mdast-util-mdx-expression";
46

57
const slugs = new GithubSlugger();
68

79
// # foo {/*bar*/} = <a id="bar">foo</a>
810
export default function () {
9-
return function (tree: any) {
11+
return function (tree: Root) {
1012
slugs.reset();
1113

12-
visit(tree, "element", function (element: any) {
14+
visit(tree, "element", function (element) {
1315
if (/^h[1-6]$/.test(element.tagName)) {
1416
const last = element.children.at(-1);
1517

16-
if (
17-
last.type === "mdxTextExpression" &&
18-
last.value.startsWith("/*") &&
19-
last.value.endsWith("*/")
20-
) {
21-
const id = last.value.slice(2, -2).trim();
22-
element.properties.id = slugs.slug(id);
18+
// @ts-expect-error this is added by mdast-util-mdx-expression
19+
if (last.type === "mdxTextExpression") {
20+
const lastElement = last as MdxTextExpression;
21+
if (
22+
lastElement.value.startsWith("/*") &&
23+
lastElement.value.endsWith("*/")
24+
) {
25+
const id = lastElement.value.slice(2, -2).trim();
26+
element.properties.id = slugs.slug(id);
2327

24-
const text = element.children.at(-2);
25-
text.value = text.value.trimEnd();
26-
element.children.with(-2, text);
27-
} else {
28-
if (!element.properties.id) {
29-
element.properties.id = slugs.slug(toString(element));
28+
const text = element.children.at(-2) as MdxTextExpression;
29+
text.value = text.value.trimEnd();
30+
element.children.with(-2, text);
31+
} else {
32+
if (!element.properties.id) {
33+
element.properties.id = slugs.slug(toString(element));
34+
}
3035
}
3136
}
3237
}

tailwind.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-check
12
import starlightPlugin from "@astrojs/starlight-tailwind";
23

34
const gray = {

worker/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default class extends WorkerEntrypoint<Env> {
2323
}
2424

2525
try {
26+
// @ts-expect-error Ignore Fetcher type mismatch
2627
const redirect = await redirectsEvaluator(request, this.env.ASSETS);
2728
if (redirect) {
2829
return redirect;
@@ -38,6 +39,7 @@ export default class extends WorkerEntrypoint<Env> {
3839
);
3940
const redirect = await redirectsEvaluator(
4041
new Request(forceTrailingSlashURL, request),
42+
// @ts-expect-error Ignore Fetcher type mismatch
4143
this.env.ASSETS,
4244
);
4345
if (redirect) {

0 commit comments

Comments
 (0)