Skip to content

Commit

Permalink
Add noteworthy CLI generator
Browse files Browse the repository at this point in the history
  • Loading branch information
AramZS committed May 18, 2024
1 parent 6c72146 commit 9a24df9
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 1 deletion.
175 changes: 175 additions & 0 deletions bin/add-noteworthy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
#!/usr/bin/env node

const {Select, Input, Confirm} = require('enquirer');
const {slugify} = require("../lib/filters");
const {DateTime} = require('luxon');
const fetch = require("node-fetch");
const cheerio = require('cheerio');
const yaml = require('js-yaml');
const yargs = require("yargs");
const fs = require('fs');
const lists = require('../src/_data/lists-meta');

/**
* Add Bookmark Tool
*
* A quick and dirty script that allows for adding noteworthys to the
* content folder of this repository.
*
* @todo support adding multiple topics to a noteworthys
* @todo check that a link has not already been added
* @todo remember author details per domain so no need to re-enter if noteworthying another page
*/

let added = [];

const fetchUrl = async (url, date) => {
try {
const response = await fetch(url);
const html = await response.text();

const $ = cheerio.load(html);
const selectYourOwn = 'Other, enter your own';

const titles = [...new Set([
$('title').text(),
$('meta[property="og:title"]').attr('content'),
$('meta[name="twitter:title"]').attr('content'),
].filter(Boolean)), selectYourOwn];

const authors = [...new Set([
$('meta[name="copyright"]').attr('content'),
$('meta[name="author"]').attr('content'),
$('meta[name="twitter:creator"]').attr('content'),
$('meta[name="article:author"]').attr('content')
].filter(Boolean)), selectYourOwn];

const topics = [
'Nifty Show and Tell',
'Notable Articles',
...Object.keys(lists),
];

const titlePrompt = new Select({
name: 'title',
message: 'Pick a title',
choices: titles
});

let title = await titlePrompt.run();

if (title === selectYourOwn) {
const prompt = new Input({
message: 'Title',
initial: ''
});

title = (await prompt.run()).trim();
}

const authorPrompt = new Select({
name: 'author',
message: 'Pick an author',
choices: authors
});

let author = await authorPrompt.run();

if (author === selectYourOwn) {
const prompt = new Input({
message: 'Author',
initial: ''
});

author = (await prompt.run()).trim();
}

const topicPrompt = new Select({
name: 'topic',
message: 'Pick a Main Topic',
choices: topics
});

const topic = await topicPrompt.run();

const filename = `${date}-${slugify(title)}.md`;
const pathname = `${__dirname}/../src/content/noteworthy/${filename}`;

if (fs.existsSync(pathname)) {
console.log(`File exists at [${pathname}]`);
return 1;
}

const frontMatter = {
title,
tags: [topic],
cite: {
name: title,
author: author,
href: url,
}
};

fs.writeFileSync(pathname, `---\n${yaml.dump(frontMatter)}\n---\n`);

added.push(frontMatter);
} catch (e) {
console.error('add noteworthy', e.message);
return 1;
}
}

const main = async (argv) => {
let code = -1;
let {url} = argv;

const datePrompt = new Input({
message: 'Date (YYYY-MM-DD)',
initial: DateTime.now().toFormat('yyyy-LL-dd')
});

const date = await datePrompt.run();

while(code === -1) {
if (!url) {
const prompt = new Input({
message: 'Website URL (or Q to quit)',
initial: ''
});

url = (await prompt.run()).trim();
if (!url) {
console.warn('[!] Please enter a valid url, or Q to quit')
continue;
} else if (url.toUpperCase() === 'Q') {
break;
}
}

code = await fetchUrl(url, date);
const prompt = new Confirm({
name: 'question',
message: 'Want to add more noteworthys?'
});

const answer = await prompt.run();
if (answer === true) {
code = -1;
url = undefined;
}
}

if (added.length > 0) {
console.log('Added Bookmark Wikilinks:')
added.forEach(a => console.log(`![[ ${a.title} ]]`));
}

return code;
}

const options = yargs
.usage("Usage: -u <url>")
.option("u", {alias: "url", describe: "Webpage Url", type: "string", demandOption: false})
.argv;

main(options).then(code => process.exit(code));
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"make:tv": "node -e 'var w = require(\"./bin/enrichers/tv.js\"); w.writeTVShows().then(() => console.log(\"complete tv\"))' > ./buildlog.txt",
"make:songs": "node -e 'var w = require(\"./bin/enrichers/rss-music.js\"); w.writeSongs().then(() => console.log(\"complete write songs\"))' > ./buildlog.txt",
"make:pocket": "node -e 'var w = require(\"./bin/enrichers/pocket.js\"); w.writeAmplify().then(() => console.log(\"complete write amplify\"))' > ./buildlog.txt",
"make:books": "node -e 'var w = require(\"./bin/enrichers/books.js\"); w.writeBooks().then(() => console.log(\"complete books\"))' > ./buildlog.txt"
"make:books": "node -e 'var w = require(\"./bin/enrichers/books.js\"); w.writeBooks().then(() => console.log(\"complete books\"))' > ./buildlog.txt",
"make:noteworthy": "node ./bin/add-noteworthy.js"
},
"bin": {
"add-bookmark": "./bin/add-bookmark.js",
Expand Down

0 comments on commit 9a24df9

Please sign in to comment.