Gestion des liens intéressants simplifiée
This commit is contained in:
@@ -9,7 +9,7 @@ const YAML = require("yaml");
|
||||
const { getArchiveUrl, saveToArchive } = require("./lib/archive");
|
||||
const { scrapePage } = require("./lib/puppeteer");
|
||||
|
||||
const KNOWN_LINKS_FILE = "data/known_links.yaml"; // YAML file with { hash: path }
|
||||
const LINKS_ROOT = path.join("content", "interets", "liens-interessants");
|
||||
|
||||
if (process.argv.length < 3) {
|
||||
console.error("Usage: add_link.js <URL> [optional: YYYY-MM-DD]");
|
||||
@@ -22,22 +22,38 @@ const customDate = process.argv[3] || null;
|
||||
// Generate an MD5 hash of the URL
|
||||
const urlHash = crypto.createHash("md5").update(url).digest("hex").slice(0, 8);
|
||||
|
||||
// Ensure the known_links file is stored at the correct location
|
||||
const hugoRoot = path.resolve(process.cwd());
|
||||
const knownLinksPath = path.join(hugoRoot, KNOWN_LINKS_FILE);
|
||||
const interestingLinksRoot = path.join(hugoRoot, LINKS_ROOT);
|
||||
|
||||
// Load known links from YAML
|
||||
let knownLinks = {};
|
||||
if (fs.existsSync(knownLinksPath)) {
|
||||
try {
|
||||
knownLinks = YAML.parse(fs.readFileSync(knownLinksPath, "utf8")) || {};
|
||||
} catch (err) {
|
||||
console.error(`❌ Unable to parse ${KNOWN_LINKS_FILE}: ${err.message}`);
|
||||
process.exit(1);
|
||||
function findExistingLinkBundle(hash) {
|
||||
if (!fs.existsSync(interestingLinksRoot)) {
|
||||
return null;
|
||||
}
|
||||
const stack = [interestingLinksRoot];
|
||||
while (stack.length > 0) {
|
||||
const current = stack.pop();
|
||||
if (path.basename(current) === hash) {
|
||||
return current;
|
||||
}
|
||||
let entries = [];
|
||||
try {
|
||||
entries = fs.readdirSync(current, { withFileTypes: true });
|
||||
} catch (error) {
|
||||
continue;
|
||||
}
|
||||
for (const entry of entries) {
|
||||
if (entry.isDirectory()) {
|
||||
stack.push(path.join(current, entry.name));
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
if (knownLinks[urlHash]) {
|
||||
console.log(`⚠ Link already exists: ${url}`);
|
||||
|
||||
const duplicateBundlePath = findExistingLinkBundle(urlHash);
|
||||
if (duplicateBundlePath) {
|
||||
const relative = path.relative(hugoRoot, duplicateBundlePath);
|
||||
console.log(`⚠ Link already exists at ${relative}: ${url}`);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
@@ -177,10 +193,6 @@ file: "images/screenshot.png"
|
||||
fs.writeFileSync(metadataPath, metadataContent);
|
||||
console.log(`✔ Metadata saved: ${metadataPath}`);
|
||||
|
||||
// Append the hash to known_links.yaml
|
||||
knownLinks[urlHash] = path.relative(hugoRoot, bundlePath);
|
||||
fs.writeFileSync(knownLinksPath, YAML.stringify(knownLinks));
|
||||
|
||||
console.log(`🎉 Link successfully added! Bundle path: ${bundlePath}`);
|
||||
console.log(bundlePath);
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user