Last active
May 11, 2023 17:58
-
-
Save curtisj44/bebcdaf79b2acc39fd3cfbd5b1d59a3a to your computer and use it in GitHub Desktop.
Bookmarking Bookmarklet for Eleventy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:With some quick line break additions, though, all is well:
Preview view
