Last active
January 11, 2022 22:34
-
-
Save brettjonesdev/9783137e0d762e0d84efa71b1bd69bb5 to your computer and use it in GitHub Desktop.
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
(function bookmarksExportToCsv() { | |
/** | |
* Inspired by https://gist.github.com/iamandrewluca/9e46589f42545446fa387ac193c43634 | |
* 1. Export bookmarks from Pocket as HTML | |
* 2. Open exported html file again in the browser | |
* 3. Copy paste this entire file in console, and execute it (hit enter) | |
* 4. You will be prompted to save a CSV file. Save it. | |
* 5. Open Notion. Click Import -> CSV | |
* 6. Select saved CSV file. Wait for import | |
* 7. You have a new database with all your bookmarks | |
*/ | |
const bookmarks = getBookmarks(document.body); | |
console.log(bookmarks); | |
const csvString = arrayToCsv(bookmarks); | |
downloadString(csvString); | |
function getBookmarks(def) { | |
// Chromium browsers exports dl first | |
// Safari browser exports dt first | |
// We take both into consideration | |
return Array.from(def.querySelectorAll("a")).flatMap( | |
createBookmark | |
); | |
} | |
function getDate(anchor) { | |
const stringDate = anchor.getAttribute("time_added"); | |
// Safari does not add an add_date attribute, default to new Date | |
const date = stringDate ? new Date(Number(stringDate) * 1000) : new Date(); | |
const dateF = new Intl.DateTimeFormat("en-US", { dateStyle: "medium" }); | |
const hourF = new Intl.DateTimeFormat("en-US", { timeStyle: "short" }); | |
// Notion it seems to support it this way | |
return `${dateF.format(date)} ${hourF.format(date)}`; | |
} | |
function createBookmark(anchor) { | |
return { | |
Name: anchor.innerText, | |
Created: getDate(anchor), | |
Tags: anchor.getAttribute('tags'), | |
Source: 'Pocket', | |
// Make sure URL's are encoded, otherwise, Notion may fail to import | |
Url: encodeURI(anchor.href), | |
}; | |
} | |
function arrayToCsv(array) { | |
const titles = ["Name", "Created", "Tags", "Url", "Source"].map((t) => `"${t}"`); | |
const entries = array.map((e) => | |
[e.Name, e.Created, e.Tags, e.Url, e.Source] | |
// We use double quotes as data wrapper | |
// We escape double quotes inside using another double quote as RFC says | |
.map((f) => typeof f === 'string' ? `"${f.replace(/"/g, `""`)}"` : f) | |
.join(",") | |
); | |
return `${titles.join(",")}\n${entries.join("\n")}`; | |
} | |
function downloadString(text) { | |
const a = document.createElement("a"); | |
a.href = window.URL.createObjectURL(new Blob([text], { type: "text/csv" })); | |
// When importing in Notion it will use file name without extension as page name | |
a.download = "Bookmarks.csv"; | |
a.click(); | |
window.setTimeout(() => window.URL.revokeObjectURL(a.href)); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment