Skip to content

Commit 4fe6cee

Browse files
committed
feedback from Benjie
1 parent 195e440 commit 4fe6cee

File tree

4 files changed

+31
-23
lines changed

4 files changed

+31
-23
lines changed

.eslintrc.cjs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-env node */
22

3-
const CODE_EXT = "js,jsx,cjs,mjs,ts,tsx,cts,mts";
4-
const MARKDOWN_EXT = "md,mdx";
3+
const CODE_EXT = "js,jsx,cjs,mjs,ts,tsx,cts,mts"
4+
const MARKDOWN_EXT = "md,mdx"
55

66
module.exports = {
77
root: true,
@@ -78,7 +78,7 @@ module.exports = {
7878
"@graphql-eslint/no-unused-fragments": "warn",
7979
"@graphql-eslint/no-duplicate-fragment-names": "error",
8080
"@graphql-eslint/no-undefined-variables": "error",
81-
"@graphql-eslint/unique-variable-names": "error"
81+
"@graphql-eslint/unique-variable-names": "error",
8282
},
8383
},
8484
{
@@ -98,4 +98,4 @@ module.exports = {
9898
},
9999
},
100100
],
101-
};
101+
}

.github/workflows/docs-validation.yml

+15-14
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,22 @@ jobs:
3131
- name: Check links
3232
run: lychee --verbose --no-progress './src/pages/learn/**/*.mdx' --base https://graphql.org
3333

34-
code-validate:
35-
name: Code snippet and GraphQL validation
36-
runs-on: ubuntu-latest
37-
steps:
38-
- uses: actions/checkout@v4
34+
code-validate:
35+
name: Code snippet and GraphQL validation
36+
runs-on: ubuntu-latest
37+
steps:
38+
- uses: actions/checkout@v4
3939

40-
- uses: actions/setup-node@v4
41-
with:
42-
node-version: "20"
40+
- uses: actions/setup-node@v4
41+
with:
42+
node-version: "20"
43+
cache: "pnpm"
4344

44-
- name: Install dependencies
45-
run: npm ci
45+
- name: Install dependencies
46+
run: pnpm install --frozen-lockfile
4647

47-
- name: Run validation w/ annotations
48-
run: npm run lint:docs:ci
48+
- name: Run validation w/ annotations
49+
run: pnpm lint:docs:ci
4950

50-
- name: Validate code snippets
51-
run: npm run validate:snippets
51+
- name: Validate code snippets
52+
run: pnpm validate:snippets

scripts/validate-snippets.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
#!/usr/bin/env node
22

33
import fs from "node:fs"
4+
import path from "node:path"
45
import glob from "glob"
56
import { parse } from "graphql"
67
import chalk from "chalk"
8+
import { fileURLToPath } from "node:url"
9+
10+
const __filename = fileURLToPath(import.meta.url)
11+
const __dirname = path.dirname(__filename)
12+
const projectRoot = path.resolve(__dirname, "../")
713

814
const MDX_GLOB = "./src/pages/learn/**/*.mdx"
9-
const CODE_BLOCK_REGEX = /```(\w+)\s*\r?\n([\s\S]*?)\r?\n```/g
15+
const CODE_BLOCK_REGEX = /^(`{3,})(\w+)\s*\n([\s\S]*?)\r?\n\1$/gm
1016
const IGNORE_COMMENT = "snippet-ignore"
1117

1218
let totalFiles = 0
@@ -49,7 +55,7 @@ function extractSnippets(content, filePath) {
4955
let match
5056

5157
while ((match = CODE_BLOCK_REGEX.exec(content)) !== null) {
52-
const [fullMatch, lang, code] = match
58+
const [fullMatch, openingBackticks, lang, code] = match
5359
const beforeBlock = content.slice(0, match.index)
5460
const lineNumber = beforeBlock.split(/\r?\n/).length
5561

@@ -93,9 +99,9 @@ async function validateSnippet(snippet) {
9399
}
94100

95101
async function main() {
96-
console.log(`Validating code snippets in: ${MDX_GLOB}`)
102+
console.log(`Validating code snippets in: ${projectRoot}/${MDX_GLOB}`)
97103

98-
const files = glob.sync(MDX_GLOB)
104+
const files = glob.sync(MDX_GLOB, { cwd: projectRoot })
99105
totalFiles = files.length
100106

101107
if (totalFiles === 0) {

src/pages/learn/response.mdx

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ In addition to the `data` key, the GraphQL specification outlines how [errors](h
4141

4242
Request errors typically occur because the client made a mistake. For example, there may be a _syntax error_ in the document, such as a missing bracket or the use of an unknown root operation type keyword:
4343

44+
<!-- snippet-ignore -->
4445
```graphql
4546
# { "graphiql": true }
46-
mutation {
47+
operation {
4748
updateHumanName(id: "1000", name: "Luke Starkiller" ) {
4849
id
4950
name

0 commit comments

Comments
 (0)