Skip to content

Commit

Permalink
fifth work session, build dynamic, more plugins!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
trueberryless committed Jan 6, 2025
1 parent 6d99f32 commit 3719756
Show file tree
Hide file tree
Showing 3 changed files with 560 additions and 23 deletions.
107 changes: 84 additions & 23 deletions packages/starlight-spell-checker/libs/validation.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,56 @@
import { fileURLToPath } from "node:url";
import { statSync } from "node:fs";
import { posix } from "node:path";

import type { StarlightUserConfig as StarlightUserConfigWithPlugins } from "@astrojs/starlight/types";
import type { AstroConfig, AstroIntegrationLogger } from "astro";
import { $, bgGreen, black, blue, dim, green, red } from "kleur/colors";

import type { StarlightSpellCheckerConfig } from "../libs/config";

import { unified } from "unified";
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import rehypeStringify from "rehype-stringify";
import { retext } from "retext";
import retextSpell from "retext-spell";
import dictionaryEn from "dictionary-en";
import retextReadability from "retext-readability";
import retextIndefiniteArticle from "retext-indefinite-article";
import { visit } from "unist-util-visit";
import { promises as fs } from "fs";
import path from "path";
import type { Root } from "mdast";
import { getLocaleConfig, getLocaleDictionary } from "./i18n";
import { ensureTrailingSlash, stripLeadingSlash } from "./path";
import { reporter } from "vfile-reporter";
import { getLocaleDictionary } from "./i18n";
import { stripLeadingSlash } from "./path";
import { getValidationData } from "./remark";

import retextAssuming from "retext-assuming";
import retextCasePolice from "retext-case-police";
import retextCliches from "retext-cliches";
import retextContractions from "retext-contractions";
import retextDiacritics from "retext-diacritics";
import retextEquality from "retext-equality";
import retextIndefiniteArticle from "retext-indefinite-article";
import retextIntensify from "retext-intensify";
import retextOveruse from "retext-overuse";
import retextPassive from "retext-passive";
import retextProfanities from "retext-profanities";
import retextReadability from "retext-readability";
import retextRedundantAcronyms from "retext-redundant-acronyms";
import retextRepeatedWords from "retext-repeated-words";
import retextSimplify from "retext-simplify";
import retextSpell from "retext-spell";
import retextUsage from "retext-usage";
import retextQuotes from "retext-quotes";

export const ValidationErrorType = {
Misspelling: "Misspelling",
Assuming: "unhelpful phrase",
CasePolice: "popular names casing",
Cliches: "cliché",
Contractions: "apostrophe use in contractions",
Diacritics: "diacritics",
Equality: "insensitive, inconsiderate language",
IndefiniteArticle: "indefinite article",
Intensify: "weak, mitigating wording",
Overuse: "overused word",
Passive: "passive voice",
Profanities: "profane, vulgar wording",
Readability: "readability",
RedundantAcronyms: "redundant acronym",
RepeatedWords: "repeated word",
Simplify: "simpler alternative",
Spell: "misspelled word",
Usage: "incorrect English usage",
Quotes: "quote and apostrophe usage",
Other: "other",
} as const;

export async function validateTexts(
Expand All @@ -46,11 +69,27 @@ export async function validateTexts(
for (const [filePath, content] of contents) {
let dictionary = getLocaleDictionary(filePath, starlightConfig);

let retextProcessor = retext().use(retextSpell, {
dictionary,
});
// .use(retextReadability, { age: 22 })
// .use(retextIndefiniteArticle);
let retextProcessor = retext()
.use(retextAssuming)
.use(retextCasePolice)
.use(retextCliches)
.use(retextContractions)
.use(retextDiacritics)
.use(retextEquality)
.use(retextIndefiniteArticle)
.use(retextIntensify)
.use(retextOveruse)
.use(retextPassive)
.use(retextProfanities)
.use(retextReadability)
.use(retextRedundantAcronyms)
.use(retextRepeatedWords)
.use(retextSimplify)
.use(retextSpell, {
dictionary,
})
.use(retextUsage)
.use(retextQuotes);

try {
const file = await retextProcessor.process(content);
Expand All @@ -60,7 +99,7 @@ export async function validateTexts(
for (const error of file.messages.values()) {
fileErrors.push({
word: error.actual ?? "",
type: ValidationErrorType.Misspelling,
type: validationErrorTypeMapper[error.source ?? "other"],
suggestions: error.expected ?? [],
});
}
Expand Down Expand Up @@ -166,6 +205,28 @@ interface ValidationError {
suggestions?: string[];
}

const validationErrorTypeMapper: Record<string, any> = {
"retext-assuming": ValidationErrorType.Assuming,
"retext-case-police": ValidationErrorType.CasePolice,
"retext-cliches": ValidationErrorType.Cliches,
"retext-contractions": ValidationErrorType.Contractions,
"retext-diacritics": ValidationErrorType.Diacritics,
"retext-equality": ValidationErrorType.Equality,
"retext-indefinite-article": ValidationErrorType.IndefiniteArticle,
"retext-intensify": ValidationErrorType.Intensify,
"retext-overuse": ValidationErrorType.Overuse,
"retext-passive": ValidationErrorType.Passive,
"retext-profanities": ValidationErrorType.Profanities,
"retext-readability": ValidationErrorType.Readability,
"retext-redundant-acronyms": ValidationErrorType.RedundantAcronyms,
"retext-repeated-words": ValidationErrorType.RepeatedWords,
"retext-simplify": ValidationErrorType.Simplify,
"retext-spell": ValidationErrorType.Spell,
"retext-usage": ValidationErrorType.Usage,
"retext-quotes": ValidationErrorType.Quotes,
other: ValidationErrorType.Other,
};

interface PageData {
pathname: string;
}
Expand Down
15 changes: 15 additions & 0 deletions packages/starlight-spell-checker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,25 @@
"remark-rehype": "^11.1.1",
"remark-retext": "^6.0.0",
"retext": "^9.0.0",
"retext-assuming": "^1.0.0",
"retext-case-police": "^1.1.7",
"retext-cliches": "^1.0.0",
"retext-contractions": "^6.0.0",
"retext-diacritics": "^5.0.0",
"retext-equality": "^7.1.0",
"retext-indefinite-article": "^5.0.0",
"retext-intensify": "^7.0.0",
"retext-overuse": "^1.1.1",
"retext-passive": "^5.0.0",
"retext-profanities": "^8.0.0",
"retext-quotes": "^6.0.0",
"retext-readability": "^8.0.0",
"retext-redundant-acronyms": "^5.0.0",
"retext-repeated-words": "^5.0.0",
"retext-simplify": "^8.0.0",
"retext-spell": "^6.1.0",
"retext-stringify": "^4.0.0",
"retext-usage": "^0.5.0",
"unified": "^11.0.5",
"unist-util-visit": "^5.0.0",
"vfile-reporter": "^8.1.1"
Expand Down
Loading

0 comments on commit 3719756

Please sign in to comment.