Skip to content

Instantly share code, notes, and snippets.

@curtisj44
Last active May 11, 2023 17:58
Show Gist options
  • Save curtisj44/bebcdaf79b2acc39fd3cfbd5b1d59a3a to your computer and use it in GitHub Desktop.
Save curtisj44/bebcdaf79b2acc39fd3cfbd5b1d59a3a to your computer and use it in GitHub Desktop.
Bookmarking Bookmarklet for Eleventy
// TODO: update this path to your repo on GitHub
const repo = "https://github.com/your-path/goes-here/new/master/posts";
const getToday = () => {
const today = new Date();
let dd = today.getDate();
let mm = today.getMonth() + 1;
const yyyy = today.getFullYear();
if (dd < 10) dd = `0${dd}`;
if (mm < 10) mm = `0${mm}`;
return `${yyyy}-${mm}-${dd}`;
};
const getSlug = () => {
let slug = document.URL.toLowerCase()
.replace("https://", "")
.replace("http://", "")
.replace("www.", "")
.replace(new RegExp("/", "g"), "-")
.replace(/\?/g, "-")
.replace(/\#/g, "-")
.replace(/\%20/g, "-")
.replace(/\%27/g, "-")
.replace(/\_/g, "-");
// Avoid ending file with `-`
if (slug.length === slug.lastIndexOf("-") + 1) {
slug = slug.slice(0, -1);
}
return slug;
};
const getPageTitle = () => {
return (
document.title
// `:` character
.replace(/\%3A/g, " -")
// `&` character
.replace(/\&/g, "and")
);
};
const getQueryString = () => {
const slug = getSlug();
const pageTitle = getPageTitle();
const filename = `${slug}.md`;
const nextLine = `%0A`;
const space = `%20`;
const fileContent = encodeURIComponent(`
---
${nextLine}
date:${space}${getToday()}
${nextLine}
${nextLine}
tags:${nextLine}
-${space}todo
${nextLine}
${nextLine}
title:${space}${pageTitle}
${nextLine}
${nextLine}
url:${space}${document.URL}
${nextLine}
---
${nextLine}
`);
return `?filename=${filename}&value=${fileContent}`;
};
window.open(`${repo}${getQueryString()}`);
@curtisj44
Copy link
Author

This bookmarklet opens a new tab with a details of the original URL pre-filled in the Front Matter of a new .md file.

I can't figure out how to get the GitHub value field to respect the encoded %0A line breaks, so it's not quite ready to be committed:

initial result

With some quick line break additions, though, all is well:

Edit view

Preview view
Preview view

@curtisj44
Copy link
Author

Update: Line breaks are working now 🎉

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment