Skip to content

Commit

Permalink
feat: init retext plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianCataldo committed Aug 26, 2022
1 parent 5fa65f9 commit 29f650d
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,47 @@
* See LICENSE in the project root. *
/* —————————————————————————————————————————————————————————————————————————— */

import abbreviates from 'case-police/dict/abbreviates.json' assert { type: 'json' };
import brands from 'case-police/dict/brands.json' assert { type: 'json' };
import general from 'case-police/dict/general.json' assert { type: 'json' };
import products from 'case-police/dict/products.json' assert { type: 'json' };
import softwares from 'case-police/dict/softwares.json' assert { type: 'json' };
/* ·········································································· */
import type { Plugin } from 'unified';
import { search } from 'nlcst-search';
import type { Root } from 'nlcst-search';
import { toString } from 'nlcst-to-string';
/* ·········································································· */
// TODO: Add URL reference to message / hint like `remark-lint` rules
// import { homepage } from './package.json' assert { type: 'json' };
/* —————————————————————————————————————————————————————————————————————————— */

export type Dict = Record<string, string>;
const casePoliceDicts: Dict = {
...abbreviates,
...brands,
...general,
...products,
...softwares,
};
const casePoliceKeys = Object.entries(casePoliceDicts).map(([key]) => key);

const retextCasePolice: Plugin<[], Root> = () => (tree, file) => {
search(tree, casePoliceKeys, (nodes) => {
const original = toString(nodes);
const originalLowercased = original.toLowerCase();
const correctOne = casePoliceDicts[originalLowercased];

if (original !== correctOne) {
if (nodes[0].type === 'WordNode') {
file.message(
`"${original}" must be spelled "${correctOne}"`,
nodes[0].children[0],
`retext-case-police:${correctOne}`,
);
}
}
});
};

export default retextCasePolice;

0 comments on commit 29f650d

Please sign in to comment.