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
78 changes: 78 additions & 0 deletions eslint-local-rules/frontmatter-title.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import matter from 'gray-matter';

export default {
meta: {
type: 'problem',
fixable: 'code',
messages: {
mismatch: 'Frontmatter title "{{fm}}" does not match H1 "{{h1}}".',
missingFrontmatterTitle: 'Missing frontmatter title.',
missingH1Title: 'Missing H1 title.',
},
},

create(context) {
const { sourceCode } = context;
let h1Node = null;

return {
heading(node) {
if (node.depth === 1 && !h1Node) h1Node = node;
},

'root:exit'() {
const fmTitle = matter(sourceCode.text).data.title;
const h1Title = h1Node
? sourceCode.getText(h1Node).replace(/^#\s+/, '').trim()
: null;

if (!fmTitle && !h1Title) return;

if (fmTitle && h1Title && fmTitle !== h1Title) {
context.report({
node: h1Node,
messageId: 'mismatch',
data: { fm: fmTitle, h1: h1Title },
fix(fixer) {
const match = /^title:.*$/m.exec(sourceCode.text);
if (match) {
return fixer.replaceTextRange(
[match.index, match.index + match[0].length],
`title: ${h1Title}`
);
}
},
});
} else if (!fmTitle && h1Title) {
context.report({
node: h1Node,
messageId: 'missingFrontmatterTitle',
fix(fixer) {
const hasFrontmatter = sourceCode.text.startsWith('---');
if (hasFrontmatter) {
const match = /^---\r?\n/.exec(sourceCode.text);
if (match) {
return fixer.insertTextAfterRange(
[0, match[0].length],
`title: ${h1Title}\n`
);
}
} else {
return fixer.insertTextBeforeRange(
[0, 0],
`---\ntitle: ${h1Title}\n---\n\n`
);
}
},
});
} else if (!h1Title) {
// Missing H1, should put it manually.
context.report({
loc: { line: 1, column: 0 },
messageId: 'missingH1Title',
});
}
},
};
},
};
28 changes: 27 additions & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import js from '@eslint/js';
import globals from 'globals';
import markdown from '@eslint/markdown';
import frontmatterTitle from './eslint-local-rules/frontmatter-title.mjs';

export default [
js.configs.recommended,
{
...js.configs.recommended,
files: ['**/*.js', '**/*.jsx', '**/*.mjs', '**/*.cjs'],
},
{
files: ['**/*.js', '**/*.jsx', '**/*.mjs', '**/*.cjs'],
languageOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
Expand All @@ -28,6 +34,26 @@ export default [
'pages/docs/api',
'pages/docs/loaders',
'pages/docs/plugins',
'pages/about/governance',
'pages/404.md',
'pages/index.md',
'pages/blog/index.md',
'pages/about/sponsors.md',
],
},
{
files: ['pages/**/*.md'],
plugins: {
markdown,
local: {
rules: {
'frontmatter-title': frontmatterTitle,
},
},
},
language: 'markdown/commonmark',
rules: {
'local/frontmatter-title': 'error',
},
},
];
189 changes: 188 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading