remark-lint/script/plugin/list-of-plugins.js

82 lines
1.9 KiB
JavaScript
Raw Normal View History

2023-11-11 19:47:26 +03:00
/**
2024-07-15 16:22:59 +03:00
* @import {ListItem, List, Root} from 'mdast'
* @import {PackageJson} from 'type-fest'
2023-11-11 19:47:26 +03:00
*/
import assert from 'node:assert/strict'
import fs from 'node:fs/promises'
2023-11-14 10:29:32 +03:00
import structuredClone from '@ungap/structured-clone'
2023-11-11 19:47:26 +03:00
import {zone} from 'mdast-zone'
import {packagesUrl, plugins} from '../info.js'
/**
* List rules.
*
* @returns
* Transform.
*/
export default function remarkListOfRules() {
/**
* Transform.
*
* @param {Root} tree
* Tree.
2023-12-13 18:54:37 +03:00
* @returns {Promise<undefined>}
2023-11-11 19:47:26 +03:00
* Nothing.
*/
2023-12-13 18:54:37 +03:00
return async function (tree) {
/**
* @type {Array<ListItem | undefined>}
* List items.
*/
const items = await Promise.all(
plugins.map(async function (info) {
const packageUrl = new URL(info.name + '/', packagesUrl)
/** @type {PackageJson} */
const pack = JSON.parse(
String(await fs.readFile(new URL('package.json', packageUrl)))
)
const description = String(pack.description || '').replace(
/^remark-lint rule to ?/i,
''
)
2023-12-15 17:03:05 +03:00
if (!info.ruleId) return
2023-12-13 18:54:37 +03:00
assert(typeof pack.repository === 'string')
return {
type: 'listItem',
spread: false,
children: [
{
type: 'paragraph',
children: [
{
type: 'link',
url: pack.repository,
children: [{type: 'inlineCode', value: info.name}]
},
{type: 'text', value: ' — ' + description}
]
}
]
}
})
)
/** @type {List} */
const list = {
type: 'list',
ordered: false,
spread: false,
// @ts-expect-error: filter is correct.
children: items.filter(Boolean)
}
2023-11-11 19:47:26 +03:00
zone(tree, 'rules', function (start, _, end) {
return [start, structuredClone(list), end]
})
}
}