Skip to content
Merged
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
11 changes: 11 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ jobs:
node-version: '12.x'
- run: npm install -g markdownlint-cli@0.25.0
- run: markdownlint '**/*.md' --ignore node_modules
frontmatter:
name: 🍒 Frontmatter
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3.1.0
- name: 🚀 Use Node.js
uses: actions/setup-node@v3
with:
node-version: '16.x'
- run: yarn install --frozen-lockfile --ignore-scripts
- run: yarn lint:frontmatter
yamllint:
name: 🍏 YAML
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"serve:doc": "yarn workspace doc docusaurus serve",
"serve:blog:zh": "yarn workspace blog docusaurus serve zh",
"serve:blog:en": "yarn workspace blog docusaurus serve en",
"lint:frontmatter": "node scripts/lint-frontmatter.js",
"build:website:preview:serve": "yarn build:website:preview && yarn serve:website",
"build:doc:preview:serve": "yarn build:doc:preview && yarn serve:doc",
"build:blog:zh:preview:serve": "yarn build:blog:zh:preview && yarn serve:blog:zh",
Expand All @@ -60,6 +61,7 @@
"eslint-plugin-react-hooks": "^4.3.0",
"eslint-plugin-yml": "^0.14.0",
"husky": ">=6",
"js-yaml": "^4.1.0",
"lint-staged": ">=10",
"remark": "^14.0.2",
"remark-cli": "^11.0.0",
Expand Down
70 changes: 70 additions & 0 deletions scripts/lint-frontmatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* eslint-disable no-console */
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');

const ROOT = path.resolve(__dirname, '..');
const IGNORED_DIRS = new Set([
'.git',
'build',
'dist',
'node_modules',
]);
const MARKDOWN_EXTENSIONS = new Set(['.md', '.mdx']);

function walk(dir, files = []) {
fs.readdirSync(dir, { withFileTypes: true }).forEach((entry) => {
if (entry.isDirectory()) {
if (!IGNORED_DIRS.has(entry.name)) {
walk(path.join(dir, entry.name), files);
}
return;
}

if (entry.isFile() && MARKDOWN_EXTENSIONS.has(path.extname(entry.name))) {
files.push(path.join(dir, entry.name));
}
});

return files;
}

function extractFrontmatter(content) {
if (!content.startsWith('---\n') && !content.startsWith('---\r\n')) {
return null;
}

const lines = content.split(/\r?\n/);
const closingIndex = lines.slice(1).findIndex((line) => line.trim() === '---');
if (closingIndex >= 0) {
return lines.slice(1, closingIndex + 1).join('\n');
}

throw new Error('Missing closing frontmatter delimiter');
}

function main() {
const failures = [];

walk(ROOT).forEach((file) => {
const relativePath = path.relative(ROOT, file);
const content = fs.readFileSync(file, 'utf8');

try {
const frontmatter = extractFrontmatter(content);
if (frontmatter !== null) {
yaml.load(frontmatter);
}
} catch (error) {
failures.push(`${relativePath}: ${error.message}`);
}
});

if (failures.length > 0) {
console.error('Invalid Markdown frontmatter found:\n');
failures.forEach((failure) => console.error(`- ${failure}`));
process.exit(1);
}
}

main();
Loading