Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create script to check formatting of titles #2053

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
- run: npm i
- run: npm run lint:formatting
- run: npm run lint:check-links
- run: npm run lint:check-frontmatter

check_quickstarts:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion docs/components/authentication/sign-in.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: '`<SignIn />` component'
title: `<SignIn />` component
description: Clerk's <SignIn /> component renders a UI for signing in users.
---

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"format": "prettier . --write",
"lint:check-links": "node ./scripts/check-links.mjs",
"lint:formatting": "prettier . --check",
"lint:check-quickstarts": "node ./scripts/check-quickstarts.mjs"
"lint:check-quickstarts": "node ./scripts/check-quickstarts.mjs",
"lint:check-frontmatter": "node ./scripts/check-frontmatter.mjs"
},
"devDependencies": {
"concurrently": "^8.2.2",
Expand Down
79 changes: 79 additions & 0 deletions scripts/check-frontmatter.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import fs from 'node:fs'
import readdirp from 'readdirp'
import { remark } from 'remark'
import remarkFrontmatter from 'remark-frontmatter'
import { visit } from 'unist-util-visit'
import reporter from 'vfile-reporter'

const ERRORS = {
MISSING_TITLE(file) {
return 'Missing title in frontmatter'
},
INVALID_TITLE_FORMAT(file, title) {
return 'Title starting or ending with backticks must be wrapped in quotes.'
},
}

// Plugin to check frontmatter
const remarkPluginCheckFrontmatter = () => (tree, file) => {
visit(tree, 'yaml', (node) => {
try {
// Parse the YAML content by splitting on newlines and looking for title
const frontmatter = node.value.split('\n').reduce((acc, line) => {
const [key, ...values] = line.split(':')
if (key && values.length) {
acc[key.trim()] = values.join(':').trim()
}
return acc
}, {})

if (!frontmatter.title) {
file.message(ERRORS.MISSING_TITLE(file.path), node)
return
}

const title = frontmatter.title
if ((title.startsWith('`') || title.endsWith('`')) && !(title.startsWith("'") && title.endsWith("'"))) {
file.message(ERRORS.INVALID_TITLE_FORMAT(file.path, title), node)
}
} catch (error) {
file.message(`Error parsing frontmatter: ${error.message}`, node)
}
})
}

const processor = remark().use(remarkFrontmatter).use(remarkPluginCheckFrontmatter)

async function main() {
console.log('🔎 Checking frontmatter titles...')

const files = readdirp('docs', {
fileFilter: '*.mdx',
type: 'files',
})

const checkedFiles = []

for await (const file of files) {
const contents = await fs.promises.readFile(file.fullPath, 'utf8')
const result = await processor.process({
path: file.path,
value: contents,
})

if (result.messages.length > 0) {
checkedFiles.push(result)
}
}

const output = reporter(checkedFiles, { quiet: true })

if (output) {
console.log(output)
process.exitCode = 1
} else {
console.log('✅ All frontmatter titles are properly formatted!')
}
}

main()
Loading