Skip to content

Commit

Permalink
fourth work session, nice log
Browse files Browse the repository at this point in the history
  • Loading branch information
trueberryless committed Jan 6, 2025
1 parent 9fccff9 commit 6d99f32
Showing 1 changed file with 45 additions and 19 deletions.
64 changes: 45 additions & 19 deletions packages/starlight-spell-checker/libs/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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 { $, bgGreen, black, blue, dim, green, red } from "kleur/colors";

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

Expand All @@ -26,6 +26,10 @@ import { ensureTrailingSlash, stripLeadingSlash } from "./path";
import { reporter } from "vfile-reporter";
import { getValidationData } from "./remark";

export const ValidationErrorType = {
Misspelling: "Misspelling",
} as const;

export async function validateTexts(
pages: PageData[],
outputDir: URL,
Expand All @@ -37,24 +41,31 @@ export async function validateTexts(

const { contents } = getValidationData();

const errors = new Map();
const errors: ValidationErrors = new Map();

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(retextSpell, {
dictionary,
});
// .use(retextReadability, { age: 22 })
// .use(retextIndefiniteArticle);

try {
const file = await retextProcessor.process(content);

if (file.messages.length > 0) {
errors.set(filePath, reporter(file));
let fileErrors: ValidationError[] = [];

for (const error of file.messages.values()) {
fileErrors.push({
word: error.actual ?? "",
type: ValidationErrorType.Misspelling,
suggestions: error.expected ?? [],
});
}

errors.set(filePath, fileErrors);
} catch (err) {
console.error(`Error processing file ${filePath}:`, err);
}
Expand Down Expand Up @@ -90,15 +101,19 @@ export function logErrors(
for (const [file, validationErrors] of errors) {
logger.info(`${red("▶")} ${blue(file)}`);

logger.info(validationErrors);

// for (const [index, validationError] of validationErrors.entries()) {
// logger.info(
// ` ${blue(`${index < validationErrors.length - 1 ? "├" : "└"}─`)} ${
// validationError.link
// }${dim(` - ${validationError.type}`)}`
// );
// }
for (const [index, validationError] of validationErrors.entries()) {
logger.info(
` ${blue(`${index < validationErrors.length - 1 ? "├" : "└"}─`)} ${
validationError.word
}${dim(` - ${validationError.type}`)}${
validationError.suggestions
? validationError.suggestions.length > 0
? ` (${validationError.suggestions.join(", ")})`
: " no suggestions"
: ""
}`
);
}
}

process.stdout.write("\n");
Expand Down Expand Up @@ -140,6 +155,17 @@ function pluralize(count: number, singular: string) {
// return picomatch(exclude)(page);
// }

type ValidationErrors = Map<string, ValidationError[]>;

export type ValidationErrorType =
(typeof ValidationErrorType)[keyof typeof ValidationErrorType];

interface ValidationError {
word: string;
type: ValidationErrorType;
suggestions?: string[];
}

interface PageData {
pathname: string;
}
Expand Down

0 comments on commit 6d99f32

Please sign in to comment.