Skip to content

Commit dbed0a0

Browse files
committed
Extract prettier config
1 parent f7f3d69 commit dbed0a0

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

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

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,8 @@ const autoJSCodePlugin: Plugin<[], md.Root> = () => async (tree, file) => {
6161
// Remove our flag from the meta so other plugins don't trip up
6262
const newMeta = node.meta.replace(META_FLAG_REGEX, '')
6363

64-
const jsCodeBlock = await makeJsCodeBlock(newMeta, node, {
65-
location: file.path,
66-
})
67-
const tsCodeBlock = await makeTsCodeBlock(newMeta, node, {
68-
location: file.path,
69-
})
64+
const jsCodeBlock = await makeJsCodeBlock(newMeta, node)
65+
const tsCodeBlock = await makeTsCodeBlock(newMeta, node)
7066

7167
// The specific structure of the new node was retrieved by copy-pasting
7268
// an example into the MDX playground and inspecting the AST.
@@ -115,8 +111,7 @@ const CODE_BLOCK_TITLE_REGEX = /title=(?<quote>["'])(?<title>.*?)\1/
115111

116112
async function makeJsCodeBlock(
117113
metaString: string,
118-
node: md.Code,
119-
{ location }: { location: string }
114+
node: md.Code
120115
): Promise<md.Code> {
121116
// Find the `title=` meta param and change the extension
122117
const meta = metaString.replace(
@@ -130,7 +125,6 @@ async function makeJsCodeBlock(
130125
const isJsx = node.lang.endsWith('x')
131126
const code = await format(convertToJs(node.value, { isJsx }), {
132127
parser: 'babel',
133-
location,
134128
})
135129

136130
return {
@@ -143,11 +137,10 @@ async function makeJsCodeBlock(
143137

144138
async function makeTsCodeBlock(
145139
metaString: string,
146-
node: md.Code,
147-
{ location }: { location: string }
140+
node: md.Code
148141
): Promise<md.Code> {
149142
const lang = node.lang
150-
const code = await format(node.value, { parser: 'babel-ts', location })
143+
const code = await format(node.value, { parser: 'babel-ts' })
151144

152145
return {
153146
type: 'code',
@@ -173,16 +166,17 @@ function convertToJs(code: string, { isJsx }: { isJsx: boolean }) {
173166
return blankSourceFile(sourceFile)
174167
}
175168

169+
let prettierConfig: prettier.Options | undefined
170+
176171
async function format(
177172
code: string,
178-
{ parser, location }: { parser: prettier.Options['parser']; location: string }
173+
{ parser }: { parser: prettier.Options['parser'] }
179174
) {
180-
const config = await prettier.resolveConfig(location, {
175+
prettierConfig ??= await prettier.resolveConfig(__dirname, {
181176
useCache: true,
182177
editorconfig: true,
183178
})
184-
185-
return prettier.format(code, { ...config, parser })
179+
return prettier.format(code, { ...prettierConfig, parser })
186180
}
187181

188182
function transformExt(inPath: string, fn: (ext: string) => string) {

0 commit comments

Comments
 (0)