Open a random link from a markdown file

I have a list of all the links I want to read in a markdown file. and I build this script to open a random link to start reading!

/** @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/`)
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) {
// open a random url from the generated list
let index = Math.floor(Math.random() * (links.length - 1))
exec(`open '${links[index].url}'`)

there are some things I want to modify to make it better and more "agnostic":

  • [x] let de user set the file he/she wants to parse
  • [ ] store the remark AST result in the db, and maybe update it everytime I update the file (which I do using another script)
  • [ ] update the file and create another list of the links I "visited"

any feedback and suggestion is welcome!