Skip to content

Instantly share code, notes, and snippets.

@jivane-perich
Last active March 30, 2021 08:58
Show Gist options
  • Select an option

  • Save jivane-perich/1eb2574d0e32afc0aadc0bcb507e12ce to your computer and use it in GitHub Desktop.

Select an option

Save jivane-perich/1eb2574d0e32afc0aadc0bcb507e12ce to your computer and use it in GitHub Desktop.
/**
* loadData.js
*
* An utility file adapted from https://github.com/vuejs/blog/blob/master/.vitepress/getPosts.js
*
* Main change : reload posts if a change has been detected on a watched directory.
*
* The update is triggered by using 'touch' the config.js file
*
* Note : this file is located in a `utils` subfolder of the `.vitepress` folder, change the path to conform to your setup.
*/
const fs = require('fs')
const path = require('path')
const matter = require('gray-matter')
exports.getPosts = function getPosts(asFeed = false) {
return loadDataFromDirectory('posts', asFeed)
}
exports.getTalks = function getTalks(asFeed = false) {
return loadDataFromDirectory('talks', asFeed)
}
function loadDataFromDirectory(directory, asFeed) {
const currentDir = path.resolve(__dirname, `../../${directory}`)
fs.watch(currentDir, (eventType, filename) => {
console.log(`Directory changed : ${directory} - ${filename}`)
const configFilePath = path.resolve(__dirname, '../config.js')
const time = new Date()
try {
fs.utimesSync(configFilePath, time, time)
} catch (err) {
fs.closeSync(fs.openSync(filename, 'w'))
}
})
return loadArticlesFromDirectory(currentDir, asFeed)
}
function loadArticlesFromDirectory(currentDir, asFeed = false) {
return fs
.readdirSync(currentDir)
.map((file) => {
const src = fs.readFileSync(path.join(currentDir, file), 'utf-8')
const { data, excerpt, tags } = matter(src, { excerpt: true })
const post = {
title: data.title,
href: `/posts/${file.replace(/\.md$/, '.html')}`,
date: formatDate(data.date),
excerpt,
tags
}
if (asFeed) {
// only attach these when building the RSS feed to avoid bloating the
// client bundle size
post.data = data
}
return post
})
.sort((a, b) => b.date.time - a.date.time)
}
function formatDate(date) {
if (!(date instanceof Date)) {
date = new Date(date)
}
date.setUTCHours(12)
return {
time: +date,
string: date.toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric'
})
}
}
@davay42
Copy link

davay42 commented Mar 30, 2021

That's a good intent! May be it could then be available as a vitepress addon for any theme to incorporate?

https://gist.github.com/jivane-perich/1eb2574d0e32afc0aadc0bcb507e12ce#file-loaddata-js-L49 — you need to change the url according to the folder too.

@jivane-perich
Copy link
Author

No promise there :) I'm hoping to create a new theme, but I don't have the time at the moment.

If I have something usable, I will share it.

@davay42
Copy link

davay42 commented Mar 30, 2021

No pressure ) I think the concept is rather straightforward and it'll eventually be there for sure)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment