1
- // @ts -check
2
-
3
1
/*
4
2
This file defines a plugin for the unified library that processes code blocks
5
3
in Markdown documents. It looks for code blocks with a specific meta flag
@@ -37,21 +35,21 @@ Output:
37
35
38
36
*/
39
37
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'
45
45
46
46
// Wrapped in \b to denote a word boundary
47
47
const META_FLAG_REGEX = / \b a u t o - j s \b /
48
48
const SUPPORTED_LANGS = new Set ( [ 'ts' , 'tsx' ] )
49
49
50
- /** @type {import("unified").Plugin<[], import("mdast").Root> } */
51
- const autoJSCodePlugin = ( ) => {
50
+ const autoJSCodePlugin : Plugin < [ ] , Root > = ( ) => {
52
51
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 [ ] } > ( )
55
53
56
54
visitParents ( tree , 'code' , ( node , ancestors ) => {
57
55
if ( node . meta && META_FLAG_REGEX . test ( node . meta ) ) {
@@ -81,8 +79,7 @@ const autoJSCodePlugin = () => {
81
79
location : file . path ,
82
80
} )
83
81
84
- /** @type {import("mdast").RootContent[] } */
85
- const newNodes = [
82
+ const newNodes : RootContent [ ] = [
86
83
{
87
84
// @ts -expect-error This is an MDX extension
88
85
type : 'jsx' ,
@@ -111,17 +108,17 @@ const autoJSCodePlugin = () => {
111
108
}
112
109
}
113
110
114
- module . exports = autoJSCodePlugin
111
+ export default autoJSCodePlugin
115
112
116
113
// Taken from Docusaurus
117
114
// https://github.com/facebook/docusaurus/blob/v2.4.3/packages/docusaurus-theme-common/src/utils/codeBlockUtils.ts
118
115
const CODE_BLOCK_TITLE_REGEX = / t i t l e = (?< quote > [ " ' ] ) (?< title > .* ?) \1/
119
116
120
117
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 > {
125
122
// Find the `title=` meta param and change the extension
126
123
const meta = metaString . replace (
127
124
CODE_BLOCK_TITLE_REGEX ,
@@ -136,36 +133,33 @@ async function makeJsCodeBlock(
136
133
location,
137
134
} )
138
135
139
- return /** @type { import("mdast").RootContent } */ ( {
136
+ return {
140
137
type : 'code' ,
141
138
value : code ,
142
139
lang : lang ,
143
140
meta : meta ,
144
- } )
141
+ }
145
142
}
146
143
147
144
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 > {
152
149
const lang = node . lang
153
150
const code = await format ( node . value , { parser : 'babel-ts' , location } )
154
151
155
- return /** @type { import("mdast").RootContent } */ ( {
152
+ return {
156
153
type : 'code' ,
157
154
value : code ,
158
155
lang : lang ,
159
156
meta : metaString ,
160
- } )
157
+ }
161
158
}
162
159
163
160
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 }
169
163
) {
170
164
const config = await prettier . resolveConfig ( location , {
171
165
useCache : true ,
@@ -175,10 +169,7 @@ async function format(
175
169
return await prettier . format ( code , { ...config , parser } )
176
170
}
177
171
178
- function transformExt (
179
- /** @type {string } */ inPath ,
180
- /** @type {(ext: string) => string } */ fn
181
- ) {
172
+ function transformExt ( inPath : string , fn : ( ext : string ) => string ) {
182
173
const inExt = path . extname ( inPath )
183
174
const outExt = fn ( inExt )
184
175
const outPath = inPath . slice ( 0 , - inExt . length ) + outExt
0 commit comments