Script to get random line from a txt file

I have been using Obsidian for some days, I’ve made this simple js script to print a random line from a local txt file, I thought I’d share since I couldn’t find anything similar. You can use it with templater.

async function getLine() {
  const util = require("util");
  const fs = require("fs");
  const readFile = util.promisify(fs.readFile);
  const fileContent = await readFile("/your file path", "utf-8");
  var lines = fileContent.split("\n");
  var randLineNum = Math.floor(Math.random() * lines.length);
  return(lines[randLineNum]);
}
module.exports = getLine;
1 Like