From b80388a890e2ce50055563001516267c4b682c72 Mon Sep 17 00:00:00 2001 From: Ivan Gagarinov Date: Thu, 11 Jul 2024 17:03:09 +0500 Subject: [PATCH] add workspace config --- src/index.js | 17 +++++++++++------ src/utils.js | 12 ++++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index 51667bf..a57753c 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,6 @@ -import axios from 'axios'; -import fs from 'fs'; -import path from 'path'; + +import fs from 'node:fs'; +import path from 'node:path'; import _ from 'lodash'; import { fromMarkdown } from 'mdast-util-from-markdown'; import { toMarkdown } from 'mdast-util-to-markdown' @@ -10,6 +10,7 @@ import { formatMessage, parseCheckedResult, printFixResult, + getIgnoredWordsInWorkspace, } from './utils.js'; import { check } from './languageToolApi.js'; @@ -22,11 +23,13 @@ const filterBlocks = [ const errorDelimeter = '\n------------------------\n'; +/* TODO перенести получение слов для фильтра */ const filterWordsContent = fs.readFileSync('ignore_dictionary.txt', 'utf-8'); const filterWords = filterWordsContent.split(/\n/).map((word) => word.toLowerCase()); -const isFiltered = (word) => { - if (filterWords.includes(word.toLowerCase())) { + +const isFiltered = (word, additionalWords = []) => { + if ([...filterWords, ...additionalWords].includes(word.toLowerCase())) { return true; } @@ -141,6 +144,8 @@ const fix = async (dirPath, language, rules = []) => { const getErrors = async (dirPath, language, rules = []) => { const filePaths = await getFilePaths(dirPath); + const wrongWords = getIgnoredWordsInWorkspace('/content/.vscode/settings.json'); + const promises = filePaths.map(async (fullpath) => { const sourceContent = fs.readFileSync(fullpath, 'utf-8'); @@ -158,7 +163,7 @@ const getErrors = async (dirPath, language, rules = []) => { const lineNumber = leftPart.split('\n').length; const word = content.slice(offset, offset + length); - if (isFiltered(word)) { + if (isFiltered(word, wrongWords)) { return null; } diff --git a/src/utils.js b/src/utils.js index f5a30e4..2dff1c7 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,5 +1,6 @@ // @ts-check +import fs from 'node:fs'; import clc from 'cli-color'; import _ from 'lodash'; import { glob } from 'glob'; @@ -59,4 +60,15 @@ export const printFixResult = (previousContent, currentContent, fileName) => { console.log(`-------------------${fileName} done -----------------`); }; +export const getIgnoredWordsInWorkspace = (path) => { + if (!fs.existsSync(path)) { + return []; + } + + const content = fs.readFileSync(path, 'utf-8'); + const parsed = JSON.parse(content); + + return parsed['languageToolLinter.languageTool.ignoredWordsInWorkspace'] || []; +}; + export const getLanguageToolVersion = '6.4';