Skip to content
Closed
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
2 changes: 2 additions & 0 deletions .remarkrc.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import remarkLintNoBlockedCharacters from './utils/plugins/remark/remark-lint-no-blocked-characters.mjs'
import remarkLintSentenceCaseHeaders from './utils/plugins/remark/sentence-case-headers.mjs'

export default {
plugins: [
Expand All @@ -10,6 +11,7 @@ export default {
"remark-lint-table-cell-padding",
"remark-lint-table-pipe-alignment",
"remark-lint-table-pipes",
remarkLintSentenceCaseHeaders,
"@double-great/remark-lint-alt-text",
[
"remark-lint-heading-style",
Expand Down
47 changes: 47 additions & 0 deletions utils/plugins/remark/sentence-case-headers.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { visit } from 'unist-util-visit';
import { lintRule } from 'unified-lint-rule';
import { fileURLToPath } from 'url';
import { dirname } from 'path';

// Get the current directory
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const remarkLintSentenceCaseHeaders = lintRule(
{
url: 'https://github.com/ethereum-optimism/docs', // Update with your actual URL
origin: 'remark-lint:sentence-case-headers'
},
(tree, file) => {
console.log("Linting headers...");

function sentenceCaseHeader(header) {
const text = header.children.map(node => node.value).join('').trim();
const words = text.split(' ');

// Capitalize the first word, make all others lowercase
const formatted = words.map((word, index) => {
if (index === 0) {
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase(); // First word capitalized
}
return word.toLowerCase(); // All other words lowercase
}).join(' ');

return formatted !== text ? formatted : null; // Return the formatted text if different
}

visit(tree, 'heading', (node) => {
const headerText = node.children.map(child => child.value).join('');
console.log(`Checking header: ${headerText}`);

const fixedText = sentenceCaseHeader(node);
if (fixedText) {
file.message('Header should be in sentence case', node);
node.children[0].value = fixedText; // Update the header text
file.message('Header text has been updated to sentence case', node);
}
});
}
);

export default remarkLintSentenceCaseHeaders;
Loading