58 lines
1.4 KiB
TypeScript
58 lines
1.4 KiB
TypeScript
#!/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 template = `<article class="til">
|
|
<time datetime="%DATE%">%DATE%</time>
|
|
<div>
|
|
<a href="/til/%DATE%.html">%TITLE%</a>
|
|
(<a rel="nofollow noopener" class="external" href="%SOURCE%" target="_blank">source</a>)
|
|
</div>
|
|
</article>
|
|
`
|
|
|
|
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(output))
|