#!/bin/env deno run import { globber } from "https://deno.land/x/globber@0.1.0/mod.ts" interface FileIndex { title: string source: string date: string } const cwd = './til/' const outputPath = './til/index.md' const fileIndex: FileIndex[] = [] const fileIter = globber({ cwd, include: ["????-??-??.md"] }) const decoder = new TextDecoder('utf-8') const encoder = new TextEncoder() const header = `*This page contains short notes and sometimes code snippets, of interesting things I just found out.* Last updated: ${(new Date()).toISOString().slice(0,10)} ` const template = `
%TITLE% (source)
` function render(fi: FileIndex) { return template .replaceAll('%DATE%', fi.date) .replaceAll('%SOURCE%', fi.source) .replaceAll('%TITLE%', fi.title) } for await (const file of fileIter) { if (file.isDirectory) { console.log('ignoring directory', file.relative) break } const path = file.absolute const date = file.relative.slice(0, -3) const raw = await Deno.readFile(path) const lines = decoder.decode(raw).split('\n') const title = lines[0].slice(2) const source = lines[2].startsWith('[source](') ? lines[2].slice(9, -1) : '#' fileIndex.push({ title, source, date }) } const output = fileIndex .sort((a, b) => a.date.localeCompare(b.date) * -1) .map(render) .join('\n') Deno.writeFile(outputPath, encoder.encode(header+output))