Script Kit Logo
Script Kit
by John Lindquist
Community ScriptsDocsGuideDiscussBlog
Horacio Herrera
Scripts /

Horacio Herrera

hhg2288

open-a-random-link-from-a-markdown-file

by Horacio Herrera

/** @type {import("@johnlindquist/kit")} */
// Menu: Read Random link from a mardown file
// Description: Open a random link from a markdown file
// Author: Horacio Herrera
// Twitter: @hhg2288
/** @type {typeof import("unified")} */
let { unified } = await npm("unified")
/** @type {typeof import("remark-parse").default} */
let remarkParse = await npm("remark-parse")
/** @type {typeof import("unist-util-visit")} */
let { visit } = await npm("unist-util-visit")
// get the markdown file you want to parse
let file = home(`workspace/braindump/inbox.md`)
let contents = await readFile(file, "utf-8")
let tree = unified().use(remarkParse).parse(contents)
// create an array of links
let links = []
visit(tree, "link", function linkVisitor(node) {
links.push(node)
})
// open a random url from the generated list
let index = Math.floor(Math.random() * (links.length - 1))
exec(`open '${links[index].url}'`)
created by
John Lindquist
GitHub