-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy-loader.js
43 lines (35 loc) · 1.42 KB
/
my-loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const simpleGit = require('simple-git/promise')()
const path = require('path')
const { DateTime } = require('luxon')
module.exports = async function (source, map, meta) {
var callback = this.async()
let info = await simpleGit.log([this.resourcePath])
let createdAt = info.all.length
? new Date(info.all[info.all.length - 1].date).getTime()
: null
let updatedAt = info.all.length ? new Date(info.all[0].date).getTime() : null
// we are assuming "content" will be here. if we made this more
// generic we'd allow that to be customized
// path.sep = /home/will/projects/williamhoyle.ca/content/blog/2020/filename.md
let parts = this.resourcePath.split('content')
let href = parts.pop()
// remove extension
href = href
.split('.')
.slice(0, -1)
.join('.')
// enrich the yaml frontmatter before it goes through the loader
// then, we'll be able to access this info inside our templates
source = source.replace(
'---',
`---
updatedAt: ${updatedAt && DateTime.fromMillis(updatedAt).toFormat('yyyy LLLL d')}
createdAt: ${createdAt && DateTime.fromMillis(createdAt).toFormat('yyyy LLLL d')}
jsCreatedAt: ${createdAt}
jsUpdatedAt: ${updatedAt}
href: ${href}
`
)
source = source.replace('[[date]]', `<p class="text-gray-600">${createdAt && DateTime.fromMillis(createdAt).toFormat('yyyy LLLL d')}</p>`)
callback(null, source, map, meta)
}