diff options
author | takase1121 <20792268+takase1121@users.noreply.github.com> | 2021-12-22 20:28:26 +0800 |
---|---|---|
committer | takase1121 <20792268+takase1121@users.noreply.github.com> | 2021-12-22 22:02:14 +0800 |
commit | 7bc11a2d1141925a37e7e497c762305023c2ae0d (patch) | |
tree | 380a9d34b367ed4b94dc4991775ce0985a5bda11 /scripts/index.mjs | |
parent | 05701641aa4e9f68c58a818c4f5a96a68d2e25c0 (diff) | |
download | lite-xl-plugins-7bc11a2d1141925a37e7e497c762305023c2ae0d.tar.gz lite-xl-plugins-7bc11a2d1141925a37e7e497c762305023c2ae0d.zip |
add CI and script to reorganize README
Diffstat (limited to 'scripts/index.mjs')
-rw-r--r-- | scripts/index.mjs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/scripts/index.mjs b/scripts/index.mjs new file mode 100644 index 0000000..576cfb8 --- /dev/null +++ b/scripts/index.mjs @@ -0,0 +1,74 @@ +import { readFile, writeFile } from 'fs/promises' + +import centra from 'centra' +import pLimit from 'p-limit' +import { remark } from 'remark' +import remarkGfm from 'remark-gfm' +import { select, selectAll } from 'unist-util-select' + +const baseurl = process.argv[2] +const inputFile = process.argv[3] +const outputFile = process.argv[4] + +const selector = 'tableCell:first-child inlineCode' +const sortEntries = async tree => { + const rows = select('table', tree) + const header = rows.children.shift() + + rows.children.sort((first, second) => + select(selector, first).value + .localeCompare(select(selector, second).value)) + + rows.children.unshift(header) + return tree +} + +const makeUrl = link => link.url.startsWith('http') ? link.url : baseurl + link.url + +const get404s = async tree => { + const rows = select('table', tree) + const links = selectAll('tableCell:first-child link', rows) + .map(makeUrl) + + + const limit = pLimit(5) + const reqs = links + .map(url => + limit(() => centra(url, 'HEAD') + .send() + .then(res => [url, res.statusCode]) + .catch(() => [url, undefined]))) + + const status = await Promise.all(reqs) + const good = new Set(status.filter(([_, s]) => typeof s === 'number' && s < 400).map(([url]) => url)) + + if (good.size !== status.length) { + console.log(`Found ${status.length - good.size} broken URLs`) + status + .filter(([url]) => !good.has(url)) + .forEach(([url, status]) => console.log(`${url} : ${typeof status === 'number' ? status : 'failed'}`)) + } + + // filter the table + rows.children = rows.children + .filter(row => { + const link = select('tableCell:first-child link', row) + return link ? good.has(makeUrl(link)) : true + }) + + + return tree +} + +const main = async () => { + const file = await readFile(inputFile, { encoding: 'utf-8' }) + const output = await remark() + .use(remarkGfm) + .use(() => get404s) + .use(() => sortEntries) + .process(file) + + await writeFile(outputFile, String(output)) +} + +main() |