Skip to content

Instantly share code, notes, and snippets.

@robinglen
Last active September 2, 2022 12:31
Show Gist options
  • Save robinglen/88732443616aa690fa4c81f287326922 to your computer and use it in GitHub Desktop.
Save robinglen/88732443616aa690fa4c81f287326922 to your computer and use it in GitHub Desktop.
How to load bookmarks and use them for Slack integration
// importing the required exposed methods from bookworms
import { loadBookmarks, generateBookmarks } from "bookworms";
// don't really like this global but its fine for this hack
let bookmarks = "";
// load the config into memory
const init = async () => {
// for production add some error handling
const { body } = await loadBookmarks.fetchBookmarkConfig("./bookmarks.yaml");
bookmarks = body;
};
// generating the markdown only for the top level folder requested in the slash command
const generateBookmarkMarkdown = (folder) => {
// find the requested folder in the global bookmarks loaded
const requestedFolder = findBookMarkFolder(folder, bookmarks.folders);
if (requestedFolder) {
// creating the structure generateImportBookmarkMarkup expects
const selectedBookmarks = {
folders: [requestedFolder],
};
// taking the readme as this is markdown
const [browser, readme] =
generateBookmarks.generateImportBookmarkMarkup(selectedBookmarks);
// return the body which is the markdown
return readme.body;
} else {
// error message if the folder is not found
return `Sorry ${folder} could not be found, use \`/bookmarks all\` for a list of available bookmarks`;
}
};
// used for showing a dynamic list of available top level folders
const listOfBookmarksMarkDown = () => {
const commands = bookmarks.folders.map((folder) => {
return `\`/bookmarks ${folder.label.toLowerCase()}\``;
});
// adding in new line for formatting in Slack response
const commandsMarkdown = `
${commands.join("\r\n")}
`;
return `Bookmarks are seperated into different domains, to get the specific bookmarks you can enter the following commands
${commandsMarkdown}
`;
};
// returning the markdown of the requested top level folder
const findBookMarkFolder = (name, folders) => {
return folders.find((folder) => {
if (folder.label.toLowerCase() === name) {
return folder;
}
});
};
const getBookmarks = (folder) => generateBookmarkMarkdown(folder);
export { init, getBookmarks, listOfBookmarksMarkDown };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment