Skip to content

Commit

Permalink
chore: Adjust the format of the README (#3965)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S authored Dec 30, 2024
1 parent 7dec35a commit b50cabe
Show file tree
Hide file tree
Showing 8 changed files with 944 additions and 394 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ dictionaries/npm/src/npm/.npm-packages-info.json
node_modules/
pnpm-lock.yaml
samples
static/dictionary-packages.json
5 changes: 4 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion scripts/jsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"module": "ESNext",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"target": "ESNext"
},
"exclude": ["node_modules"]
Expand Down
37 changes: 25 additions & 12 deletions scripts/lib/dictionaryInfo.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ import fs from 'node:fs/promises';
import path from 'node:path/posix';

import bundledWithCSpell from '@cspell/cspell-bundled-dicts';
import { readConfigFile, resolveConfigFileImports } from 'cspell-lib';
import json5 from 'json5';

const rootUrl = new URL('../../', import.meta.url);

/**
* @typedef {Awaited<ReturnType<import('cspell-lib').readConfigFile>>} CSpellConfigFile
*/

/**
* Dictionary information.
* @typedef {object} DictionaryInfo
Expand All @@ -23,10 +28,12 @@ const rootUrl = new URL('../../', import.meta.url);
* @property {string} name The name of the dictionary.
* @property {string} packageName The name of the package.
* @property {string} dir The directory containing the dictionary package.
* @property {boolean} bundled The dictionary package is bundled with cspell.
* @property {boolean} cspell The dictionary package is bundled with cspell.
* @property {string} description The description of the package.
* @property {string[]} categories The category of the package. (e.g. programming, natural-language)
* @property {DictionaryInfo[]} dictionaries The dictionaries in the package.
* @property {boolean} [isBundle] The dictionary package is a bundle of other packages.
* @property {boolean} [hasEnabledByDefault] The dictionary package has dictionaries enabled by default.
*/

/**
Expand All @@ -50,26 +57,32 @@ export async function fetchDictionaryInfo(dictURL) {
const pkgJson = await readJson(pkgUrl);
const extFile = pkgJson.exports?.['.'] || 'cspell-ext.json';
const cspellExtUrl = new URL(extFile, dictURL);
const extConfigFile = await readConfigFile(cspellExtUrl);
/** @type {CSpellSettings} */
const cspellExt = await readJson(cspellExtUrl);
const dictionaries = extractDictionaryInfo(cspellExt);
const cspellExt = extConfigFile.settings;
const isBundle = extractImports(cspellExt).filter((i) => i.startsWith('@cspell/')).length > 2 || undefined;
const dictionaries = await extractDictionaryInfo(extConfigFile);
const hasEnabledByDefault = dictionaries.some((d) => d.enabled) || undefined;
return {
name: cspellExt.name || pkgJson.name,
dir: path.relative(rootUrl.pathname, dictURL.pathname),
packageName: pkgJson.name,
description: cspellExt.description || pkgJson.description || '',
bundled: defaultCSpellImports.has(pkgJson.name),
categories: extractCategories(pkgJson, cspellExt),
cspell: defaultCSpellImports.has(pkgJson.name),
categories: extractCategories(pkgJson, dictionaries),
dictionaries,
isBundle,
hasEnabledByDefault,
};
}

/**
*
* @param {CSpellSettings} cspellExt
* @returns {DictionaryInfo[]}
* @param {CSpellConfigFile} cspellConfigFile
* @returns {Promise<DictionaryInfo[]>}
*/
export function extractDictionaryInfo(cspellExt) {
export async function extractDictionaryInfo(cspellConfigFile) {
const cspellExt = await resolveConfigFileImports(cspellConfigFile);
const dictionaryDefs = cspellExt.dictionaryDefinitions || [];
/** @type {Map<string, DictionaryInfo>} */
const dictMap = new Map(dictionaryDefs.map((d) => [d.name, { name: d.name, description: d.description }]));
Expand Down Expand Up @@ -160,17 +173,17 @@ function extractImports(cspellExt) {

/**
* @param {Record<string, any>} pkgJson
* @param {CSpellSettings} cspellExt
* @param {DictionaryInfo[]} dictionaries
* @returns {string[]}
*/
function extractCategories(pkgJson, cspellExt) {
function extractCategories(pkgJson, dictionaries) {
const pkgCategories = Array.isArray(pkgJson.categories) ? pkgJson.categories : undefined;
return pkgCategories || extractCategoriesFromDictionaries(extractDictionaryInfo(cspellExt));
return pkgCategories || extractCategoriesFromDictionaries(dictionaries);
}

/**
*
* @param {DictionaryInfo} dictionaries
* @param {DictionaryInfo[]} dictionaries
* @returns {string[]}
*/
function extractCategoriesFromDictionaries(dictionaries) {
Expand Down
33 changes: 25 additions & 8 deletions scripts/lib/packageInfoToMarkdown.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const categoryToTitle = new Map([
['programming', 'Programming Dictionaries'],
['other', 'Specialized Dictionaries'],
['default', 'Default Dictionaries'],
['bundle', 'Dictionary Bundles'],
]);

/**
Expand All @@ -21,7 +22,7 @@ const categoryToTitle = new Map([
export async function packageInfoToMarkdown(packages) {
packages = [...packages].sort((a, b) => a.name.localeCompare(b.name));
const seen = new Set();
const categories = new Set(['natural-language', 'programming', 'other']);
const categories = new Set(['natural-language', 'programming', 'other', 'bundle']);
const byCategory = groupByCategory(packages);

let md = '<!--- Use `pnpm build:readme` to generate this table --->\n\n';
Expand All @@ -41,7 +42,10 @@ export async function packageInfoToMarkdown(packages) {
md += formatCategory(category, filtered);
}

md += '\n\n<sup>*</sup> Bundled with CSpell\n\n';
md +=
'\n\n' +
'<sup>1</sup> Bundled with CSpell.<br>' +
'<sup>2</sup> Dictionaries are enabled when packages is imported.\n\n';
md += extractDictionaryTable(packages);

return formatMarkdown(md);
Expand All @@ -57,16 +61,29 @@ function extractDictionaryTable(packages) {
return `
## All Dictionaries
| package | dictionary ID | name | description |
| --- | --- | --- | --- |
| Package | Name | Dictionary IDs |
| ------- | ---- | -------------- |
${packages.map(formatPackageRow).join('\n')}
<sup>1</sup> Bundled with CSpell.<br><sup>2</sup> Dictionaries are enabled when packages is imported.
`;
}

/**
*
* @param {DictionaryPackageInfo} pkg
* @returns {string}
*/
function formatPackageRow(pkg) {
const { packageName, dictionaries, dir } = pkg;
// | package | dictionary ID | name | description |
return `| [${packageName}](./${dir}#readme) | ${dictionaries.map((d) => d.name).join('<br>')} | ${pkg.name} | ${pkg.description} |`;

const dictNames = pkg.isBundle
? ''
: dictionaries.map((d) => d.name + (d.enabled ? '<sup>2</sup>' : '')).join('<br>');

// | Package | Name | Dictionary IDs |
return `| [${packageName}](./${dir}#readme)${pkg.cspell ? '<sup>1</sup>' : ''} | ${pkg.name} | ${dictNames} |`;
}

/**
Expand All @@ -87,7 +104,7 @@ function formatCategory(category, packages) {
* @returns {string}
*/
function formatPackage(pkg) {
return `- [${pkg.name}](${pkg.dir})${pkg.bundled ? '**<sup>*</sup>**' : ''}`;
return `- [${pkg.name}](${pkg.dir}) - <small>${pkg.description}</small> ${pkg.cspell ? '<sup>1</sup>' : ''} ${pkg.hasEnabledByDefault ? '<sup>2</sup>' : ''}`;
}

/**
Expand All @@ -98,7 +115,7 @@ function formatPackage(pkg) {
function groupByCategory(packages) {
const byCategory = new Map();
for (const pkg of packages) {
const categories = pkg.categories || [];
const categories = pkg.isBundle ? ['bundle'] : pkg.categories || [];
if (categories.length === 0) {
categories.push('other');
}
Expand Down
1 change: 1 addition & 0 deletions scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"dependencies": {
"@cspell/cspell-bundled-dicts": "^8.17.1",
"commander": "^12.1.0",
"cspell-lib": "^8.17.1",
"globby": "^14.0.2",
"json5": "2.2.3",
"prettier": "^3.4.2",
Expand Down
Loading

0 comments on commit b50cabe

Please sign in to comment.