Skip to content

Commit

Permalink
write bookmarks to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
lchski committed Aug 2, 2022
1 parent 1b027e7 commit 523c6e3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
20 changes: 20 additions & 0 deletions _scripts/generate-link-posts.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import 'dotenv/config'
import slugify from 'slugify'
import * as fs from 'fs'
import fetch from 'node-fetch'

const linksDirectory = '_links/';

async function getLatestLinkBookmarks() {
const response = await fetch(
`https://api.pinboard.in/v1/posts/recent?auth_token=${process.env.PINBOARD_API_TOKEN}&format=json&tag=to-link`
Expand Down Expand Up @@ -39,4 +43,20 @@ ${bookmarkAnnotation}
return (linkPostTemplate);
}

function fileNameFromBookmark(bookmark) {
// convert to EST (bookmarks are saved in UTC), then output in `en-CA` locale (which uses ISO dates), then slice just the date
const fileDate = new Date(bookmark.time).toLocaleString('en-CA', {timeZone: 'America/Toronto'}).slice(0, 10);

// slugify, then pull first three (at max) words in slug
const fileSlug = slugify(bookmark.description, {lower: true}).split('-', 3).join('-');

return `${fileDate}-${fileSlug}.md`;
}

function saveLinkPostToDisk(bookmark) {
fs.writeFileSync(`${linksDirectory}${fileNameFromBookmark(bookmark)}`, convertBookmarkToLinkPost(bookmark));
}

const bookmarks = await getLatestLinkBookmarks();

saveLinkPostToDisk(bookmarks.posts[3]);
16 changes: 15 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
},
"dependencies": {
"dotenv": "^16.0.1",
"node-fetch": "^3.2.10"
"node-fetch": "^3.2.10",
"slugify": "^1.6.5"
},
"devDependencies": {
"auto-reload-brunch": "^2",
Expand Down

0 comments on commit 523c6e3

Please sign in to comment.