Repository: graphql-markdown/graphql-markdown
Pull request #2255 (2025-06-25 16:48:40)

:bug:  fix prettier support for mdx with config
# Description

User `mdx` parser instead of `markdown` when calling prettier, and add support for local config file.

# Checklist

- [x] My changes follow the [contributing guidelines](https://github.com/graphql-markdown/graphql-markdown/blob/main/CONTRIBUTING.md) of this project.
- [x] I have performed a self-review of my code.
- [ ] I have made corresponding changes to the documentation.
- [x] I have added tests that prove my fix is effective or that my changes work.
- [x] New and existing unit tests pass locally with my changes.


sonarqubecloud[bot]: ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=graphql-markdown_printer-legacy&pullRequest=2255) **Quality Gate passed**  
Issues  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=graphql-markdown_printer-legacy&pullRequest=2255&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=graphql-markdown_printer-legacy&pullRequest=2255&issueStatuses=ACCEPTED)

Measures  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=graphql-markdown_printer-legacy&pullRequest=2255&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=graphql-markdown_printer-legacy&pullRequest=2255&metric=new_coverage&view=list)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=graphql-markdown_printer-legacy&pullRequest=2255&metric=new_duplicated_lines_density&view=list)  
  
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=graphql-markdown_printer-legacy&pullRequest=2255)


sonarqubecloud[bot]: ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=graphql-markdown_core&pullRequest=2255) **Quality Gate passed**  
Issues  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=graphql-markdown_core&pullRequest=2255&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=graphql-markdown_core&pullRequest=2255&issueStatuses=ACCEPTED)

Measures  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=graphql-markdown_core&pullRequest=2255&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=graphql-markdown_core&pullRequest=2255&metric=new_coverage&view=list)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=graphql-markdown_core&pullRequest=2255&metric=new_duplicated_lines_density&view=list)  
  
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=graphql-markdown_core&pullRequest=2255)


sonarqubecloud[bot]: ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=graphql-markdown_diff&pullRequest=2255) **Quality Gate passed**  
Issues  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=graphql-markdown_diff&pullRequest=2255&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=graphql-markdown_diff&pullRequest=2255&issueStatuses=ACCEPTED)

Measures  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=graphql-markdown_diff&pullRequest=2255&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=graphql-markdown_diff&pullRequest=2255&metric=new_coverage&view=list)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=graphql-markdown_diff&pullRequest=2255&metric=new_duplicated_lines_density&view=list)  
  
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=graphql-markdown_diff&pullRequest=2255)


sonarqubecloud[bot]: ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=graphql-markdown_printer-legacy&pullRequest=2255) **Quality Gate passed**  
Issues  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=graphql-markdown_printer-legacy&pullRequest=2255&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=graphql-markdown_printer-legacy&pullRequest=2255&issueStatuses=ACCEPTED)

Measures  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=graphql-markdown_printer-legacy&pullRequest=2255&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=graphql-markdown_printer-legacy&pullRequest=2255&metric=new_coverage&view=list)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=graphql-markdown_printer-legacy&pullRequest=2255&metric=new_duplicated_lines_density&view=list)  
  
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=graphql-markdown_printer-legacy&pullRequest=2255)


sonarqubecloud[bot]: ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=graphql-markdown_core&pullRequest=2255) **Quality Gate passed**  
Issues  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=graphql-markdown_core&pullRequest=2255&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=graphql-markdown_core&pullRequest=2255&issueStatuses=ACCEPTED)

Measures  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=graphql-markdown_core&pullRequest=2255&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=graphql-markdown_core&pullRequest=2255&metric=new_coverage&view=list)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=graphql-markdown_core&pullRequest=2255&metric=new_duplicated_lines_density&view=list)  
  
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=graphql-markdown_core&pullRequest=2255)


sonarqubecloud[bot]: ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=graphql-markdown_logger&pullRequest=2255) **Quality Gate passed**  
Issues  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=graphql-markdown_logger&pullRequest=2255&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=graphql-markdown_logger&pullRequest=2255&issueStatuses=ACCEPTED)

Measures  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=graphql-markdown_logger&pullRequest=2255&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=graphql-markdown_logger&pullRequest=2255&metric=new_coverage&view=list)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=graphql-markdown_logger&pullRequest=2255&metric=new_duplicated_lines_density&view=list)  
  
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=graphql-markdown_logger&pullRequest=2255)


sonarqubecloud[bot]: ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=graphql-markdown_graphql&pullRequest=2255) **Quality Gate passed**  
Issues  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=graphql-markdown_graphql&pullRequest=2255&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=graphql-markdown_graphql&pullRequest=2255&issueStatuses=ACCEPTED)

Measures  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=graphql-markdown_graphql&pullRequest=2255&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=graphql-markdown_graphql&pullRequest=2255&metric=new_coverage&view=list)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=graphql-markdown_graphql&pullRequest=2255&metric=new_duplicated_lines_density&view=list)  
  
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=graphql-markdown_graphql&pullRequest=2255)


sonarqubecloud[bot]: ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=graphql-markdown_docusaurus&pullRequest=2255) **Quality Gate passed**  
Issues  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=graphql-markdown_docusaurus&pullRequest=2255&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=graphql-markdown_docusaurus&pullRequest=2255&issueStatuses=ACCEPTED)

Measures  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=graphql-markdown_docusaurus&pullRequest=2255&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=graphql-markdown_docusaurus&pullRequest=2255&metric=new_coverage&view=list)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=graphql-markdown_docusaurus&pullRequest=2255&metric=new_duplicated_lines_density&view=list)  
  
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=graphql-markdown_docusaurus&pullRequest=2255)



Patch:
diff --git a/packages/utils/src/prettier.ts b/packages/utils/src/prettier.ts
index 0b6053e8..1cc49e48 100644
--- a/packages/utils/src/prettier.ts
+++ b/packages/utils/src/prettier.ts
@@ -27,13 +27,23 @@ export const prettify = async (
   parser: string,
 ): Promise<string | undefined> => {
   try {
-    const { format } = await import("prettier");
-    return await format(content, { parser });
+    const { resolveConfigFile, resolveConfig, format } = await import(
+      "prettier"
+    );
+    const file = await resolveConfigFile();
+
+    let options: Record<string, unknown> = {};
+    if (file) {
+      options = (await resolveConfig(file)) ?? {};
+    }
+
+    return await format(content, { ...options, parser });
   } catch {
+    const message = `Prettier is not found or not configured. Please install it or disable the "pretty" option.`;
     if ("logger" in global && global.logger) {
-      (global.logger as LoggerType)._log("Prettier is not found");
+      (global.logger as LoggerType)._log(message);
     } else {
-      global.console.log("Prettier is not found");
+      global.console.log(message);
     }
     return undefined;
   }
@@ -57,5 +67,5 @@ export const prettify = async (
 export const prettifyMarkdown = async (
   content: string,
 ): Promise<string | undefined> => {
-  return prettify(content, "markdown");
+  return prettify(content, "mdx");
 };


Test patch:
diff --git a/packages/utils/tests/unit/prettier.test.ts b/packages/utils/tests/unit/prettier.test.ts
index 3d22960f..c157a378 100644
--- a/packages/utils/tests/unit/prettier.test.ts
+++ b/packages/utils/tests/unit/prettier.test.ts
@@ -1,3 +1,4 @@
+/* eslint-disable @typescript-eslint/explicit-function-return-type */
 // packages/utils/tests/unit/prettier.test.ts
 import { prettify, prettifyMarkdown } from "../../src/prettier";
 
@@ -10,27 +11,51 @@ describe("prettier", () => {
     test("formats content using the specified parser", async () => {
       expect.assertions(1);
 
-      const result = await prettify("test content", "markdown");
+      jest.mock(
+        "prettier",
+        () => {
+          return {
+            resolveConfigFile: async () => {
+              return null;
+            },
+            resolveConfig: async () => {
+              return {};
+            },
+            format: async (content: string) => {
+              return `prettified:${content}`;
+            },
+          };
+        },
+        { virtual: true },
+      );
+
+      const result = await prettify("test content", "mdx");
       expect(result).toBe("prettified:test content");
     });
 
-    test.skip("logs error and returns undefined when prettier is not available", async () => {
+    test("logs error and returns undefined when prettier is not available", async () => {
       expect.assertions(2);
 
       jest.mock(
         "prettier",
         () => {
-          throw new Error("Prettier is not found");
+          throw new Error(
+            'Prettier is not found or not configured. Please install it or disable the "pretty" option.',
+          );
         },
         { virtual: true },
       );
 
-      const consoleSpy = jest.spyOn(global.console, "log").mockImplementation();
+      const consoleSpy = jest
+        .spyOn(global.console, "log")
+        .mockImplementation(() => {});
 
-      const result = await prettify("test content", "markdown");
+      const result = await prettify("test content", "mdx");
 
       expect(result).toBeUndefined();
-      expect(consoleSpy).toHaveBeenCalledWith("Prettier is not found");
+      expect(consoleSpy).toHaveBeenCalledWith(
+        'Prettier is not found or not configured. Please install it or disable the "pretty" option.',
+      );
 
       jest.unmock("prettier");
     });
@@ -45,10 +70,7 @@ describe("prettier", () => {
 
       await prettifyMarkdown("# Markdown content");
 
-      expect(prettifySpy).toHaveBeenCalledWith(
-        "# Markdown content",
-        "markdown",
-      );
+      expect(prettifySpy).toHaveBeenCalledWith("# Markdown content", "mdx");
     });
   });
 });