Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: markdown table links check #33

Merged
merged 4 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions .github/scripts/helper/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const cheerio = require('cheerio');
const MarkdownIt = require('markdown-it');
const md = new MarkdownIt();

function parseMarkdownTables(readmeContent) {
const HTML = md.render(readmeContent);
const collectedTables = [];
const $ = cheerio.load(HTML);
const partition = (array, n) => {
return array.length ? [array.splice(0, n)].concat(partition(array, n)) : [];
}
$("table").each((index, element) => {
const tableHeaders = [];
const tableElement = $(element).clone();
$(element).clone().find('th').each((i, element) => {
tableHeaders.push(
$(element)
.text()
.toLowerCase()
);
});
const result = $(tableElement).find('td').map((i, element) => {
return $(element).text()
});
const partitioned = partition(result.toArray(), tableHeaders.length);
const parsedTable = tableHeaders.reduce((acc, cur, index) => {
if (acc[cur]) {
return acc;
}
acc[cur] = partitioned.map(part => part[index]);
return acc;

}, {})

collectedTables.push(parsedTable)
});

return collectedTables;
}
module.exports = {
parseMarkdownTables
}
69 changes: 69 additions & 0 deletions .github/scripts/markdown-table-link-check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const fs = require('fs');
const linkCheck = require('link-check');
const LinkCheckResult = require('link-check').LinkCheckResult;
const readmeContent = fs.readFileSync('./README.md', 'utf8');
const { parseMarkdownTables } = require('./helper/index.js');
const opt = require('../../.mlc_config.json');
const { resolve } = require('dns');

const replacementSymbolDead = '❌';
const replacementSymbolAlive = '✅';
const parsedTable = parseMarkdownTables(readmeContent);
const allLinksFromUrlsClm = parsedTable.map(table => table.url).flat().filter(Boolean);

async function linksCheck(links) {
const linkCheckResults = [];
for (const link of links) {
const res = await new Promise((resolve) => linkCheck(link, opt, (err, result) => {
if(err) {
console.warn(err)
return resolve({
link,
status: 'dead'
})
}
return resolve(result);
}));
linkCheckResults.push(res);
}
return linkCheckResults;
}

async function checkLinksInTables() {
const results = await linksCheck(allLinksFromUrlsClm);
return results.reduce((acc, cur) => {
if(cur.status === 'dead') {
acc.arrDead.push(cur.link);
}
if(cur.status === 'alive'){
acc.arrAlive.push(cur.link)
}
return acc;
}, {arrDead: [], arrAlive: []})
}

async function updateLinks(arrDead, arrAlive, lines) {
const updatedLines = lines.map((line) => {
arrDead.forEach((link) => {
if (line.includes(link)) {
line = line.replace(replacementSymbolAlive, replacementSymbolDead);
}
});
arrAlive.forEach((link) => {
if (line.includes(link)) {
line = line.replace(replacementSymbolDead, replacementSymbolAlive);
}
});
return line;
});
return updatedLines;
}

(async function(){
const { arrAlive, arrDead } = await checkLinksInTables();
const lines = readmeContent.split('\n');

const updatedLines = await updateLinks(arrDead, arrAlive, lines);
const updatedTable = updatedLines.join('\n');
fs.writeFileSync('./README.md', updatedTable, 'utf8');
})()
Loading
Loading