Skip to content

Commit 0fafea8

Browse files
committed
Update plugins to ts
1 parent 3f3dfe3 commit 0fafea8

File tree

2 files changed

+31
-41
lines changed

2 files changed

+31
-41
lines changed

web/src/remark/auto-js-code.js renamed to web/src/remark/auto-js-code.ts

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// @ts-check
2-
31
/*
42
This file defines a plugin for the unified library that processes code blocks
53
in Markdown documents. It looks for code blocks with a specific meta flag
@@ -37,21 +35,21 @@ Output:
3735
3836
*/
3937

40-
const assert = require('assert/strict')
41-
const { visitParents } = require('unist-util-visit-parents')
42-
const { default: tsBlankSpace } = require('ts-blank-space')
43-
const prettier = require('prettier')
44-
const path = require('path')
38+
import assert from 'assert/strict'
39+
import { Code, Parents, Root, RootContent } from 'mdast'
40+
import * as path from 'path'
41+
import * as prettier from 'prettier'
42+
import tsBlankSpace from 'ts-blank-space'
43+
import { Plugin } from 'unified'
44+
import { visitParents } from 'unist-util-visit-parents'
4545

4646
// Wrapped in \b to denote a word boundary
4747
const META_FLAG_REGEX = /\bauto-js\b/
4848
const SUPPORTED_LANGS = new Set(['ts', 'tsx'])
4949

50-
/** @type {import("unified").Plugin<[], import("mdast").Root>} */
51-
const autoJSCodePlugin = () => {
50+
const autoJSCodePlugin: Plugin<[], Root> = () => {
5251
return async (tree, file) => {
53-
/** @type {Set<{ node: import("mdast").Code, ancestors: import("mdast").Parents[] }>} */
54-
const nodesToProcess = new Set()
52+
const nodesToProcess = new Set<{ node: Code; ancestors: Parents[] }>()
5553

5654
visitParents(tree, 'code', (node, ancestors) => {
5755
if (node.meta && META_FLAG_REGEX.test(node.meta)) {
@@ -81,8 +79,7 @@ const autoJSCodePlugin = () => {
8179
location: file.path,
8280
})
8381

84-
/** @type {import("mdast").RootContent[]} */
85-
const newNodes = [
82+
const newNodes: RootContent[] = [
8683
{
8784
// @ts-expect-error This is an MDX extension
8885
type: 'jsx',
@@ -111,17 +108,17 @@ const autoJSCodePlugin = () => {
111108
}
112109
}
113110

114-
module.exports = autoJSCodePlugin
111+
export default autoJSCodePlugin
115112

116113
// Taken from Docusaurus
117114
// https://github.com/facebook/docusaurus/blob/v2.4.3/packages/docusaurus-theme-common/src/utils/codeBlockUtils.ts
118115
const CODE_BLOCK_TITLE_REGEX = /title=(?<quote>["'])(?<title>.*?)\1/
119116

120117
async function makeJsCodeBlock(
121-
/** @type {string} */ metaString,
122-
/** @type {import('mdast').Code} */ node,
123-
/** @type {{ location: string }} */ { location }
124-
) {
118+
metaString: string,
119+
node: Code,
120+
{ location }: { location: string }
121+
): Promise<RootContent> {
125122
// Find the `title=` meta param and change the extension
126123
const meta = metaString.replace(
127124
CODE_BLOCK_TITLE_REGEX,
@@ -136,36 +133,33 @@ async function makeJsCodeBlock(
136133
location,
137134
})
138135

139-
return /** @type {import("mdast").RootContent} */ ({
136+
return {
140137
type: 'code',
141138
value: code,
142139
lang: lang,
143140
meta: meta,
144-
})
141+
}
145142
}
146143

147144
async function makeTsCodeBlock(
148-
/** @type {string} */ metaString,
149-
/** @type {import('mdast').Code} */ node,
150-
/** @type {{ location: string }} */ { location }
151-
) {
145+
metaString: string,
146+
node: Code,
147+
{ location }: { location: string }
148+
): Promise<RootContent> {
152149
const lang = node.lang
153150
const code = await format(node.value, { parser: 'babel-ts', location })
154151

155-
return /** @type {import("mdast").RootContent} */ ({
152+
return {
156153
type: 'code',
157154
value: code,
158155
lang: lang,
159156
meta: metaString,
160-
})
157+
}
161158
}
162159

163160
async function format(
164-
/** @type {string} */ code,
165-
/** @type {{ parser: prettier.Options["parser"], location: string }} */ {
166-
parser,
167-
location,
168-
}
161+
code: string,
162+
{ parser, location }: { parser: prettier.Options['parser']; location: string }
169163
) {
170164
const config = await prettier.resolveConfig(location, {
171165
useCache: true,
@@ -175,10 +169,7 @@ async function format(
175169
return await prettier.format(code, { ...config, parser })
176170
}
177171

178-
function transformExt(
179-
/** @type {string} */ inPath,
180-
/** @type {(ext: string) => string} */ fn
181-
) {
172+
function transformExt(inPath: string, fn: (ext: string) => string) {
182173
const inExt = path.extname(inPath)
183174
const outExt = fn(inExt)
184175
const outPath = inPath.slice(0, -inExt.length) + outExt

web/src/remark/code-with-hole.js renamed to web/src/remark/code-with-hole.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// @ts-check
2-
31
/*
42
This file defines a plugin for the unified library that processes code blocks
53
in Markdown documents. It looks for code blocks with a specific meta flag
@@ -24,7 +22,9 @@ Output:
2422
2523
*/
2624

27-
const { visitParents } = require('unist-util-visit-parents')
25+
import { Root } from 'mdast'
26+
import { Plugin } from 'unified'
27+
import { visitParents } from 'unist-util-visit-parents'
2828

2929
// Wrapped in \b to denote a word boundary
3030
const META_FLAG_REGEX = /\bwith-hole\b/
@@ -33,8 +33,7 @@ const HOLE_REPLACEMENT = '/* ... */'
3333

3434
const SUPPORTED_LANGS = new Set(['js', 'jsx', 'ts', 'tsx'])
3535

36-
/** @type {import("unified").Plugin<[], import("mdast").Root>} */
37-
const codeWithHolePlugin = () => {
36+
const codeWithHolePlugin: Plugin<[], Root> = () => {
3837
return (tree) => {
3938
visitParents(tree, 'code', (node) => {
4039
if (node.meta && META_FLAG_REGEX.test(node.meta)) {
@@ -52,4 +51,4 @@ const codeWithHolePlugin = () => {
5251
}
5352
}
5453

55-
module.exports = codeWithHolePlugin
54+
export default codeWithHolePlugin

0 commit comments

Comments
 (0)