Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drafting stuff to share with Tadhg #3999

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions lib/related-posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
const { DateTime } = require('luxon')

// Builds the file path for a related posts cache, using the current time.
// Optionally pass in timestamp for a deterministic result.
// @param ts [number, string, undefined] Timestamp to assign to the filename
// @return [string] Filepath to a related posts cache
// @example With no given timestamp, uses current time
// filepath() => `.cache/related-posts-1720796065028`
// @example With a given timestamp
// filepath(1737392400000) => `.cache/related-posts-1737392400000`
const filepath = (ts=undefined) => {

Check failure on line 11 in lib/related-posts.js

View workflow job for this annotation

GitHub Actions / Run linter

Unexpected block statement surrounding arrow body; move the returned value immediately after the `=>`
return `.cache/related-posts-${ ts || DateTime.now().ts }.json`
}

// Creates a related posts cache file for the current time (or given timestamp)
// and writes the given object to the file as JSON data
// @param ts [number, string, undefined] Timestamp to assign to the filename
// @param data [object] data to write to the file
// @return [undefined]
const createFile = (data={}, ts=undefined) => {

Check failure on line 20 in lib/related-posts.js

View workflow job for this annotation

GitHub Actions / Run linter

'createFile' is assigned a value but never used

Check failure on line 20 in lib/related-posts.js

View workflow job for this annotation

GitHub Actions / Run linter

Unexpected block statement surrounding arrow body; move the returned value immediately after the `=>`
return fs.writeFileSync(filepath(ts), JSON.stringify(data))

Check failure on line 21 in lib/related-posts.js

View workflow job for this annotation

GitHub Actions / Run linter

'fs' is not defined
}

// @return [Array<string>] List of filepaths to related posts caches
const cacheFiles = () => {

Check failure on line 25 in lib/related-posts.js

View workflow job for this annotation

GitHub Actions / Run linter

Unexpected block statement surrounding arrow body; move the returned value immediately after the `=>`
return fs.readdirSync('.cache').filter(file => file.startsWith('related-posts-')).sort().reverse()

Check failure on line 26 in lib/related-posts.js

View workflow job for this annotation

GitHub Actions / Run linter

'fs' is not defined
}

// @return [object] JSON data from the most recent cache
const currentCache = () => {

Check failure on line 30 in lib/related-posts.js

View workflow job for this annotation

GitHub Actions / Run linter

'currentCache' is assigned a value but never used

Check failure on line 30 in lib/related-posts.js

View workflow job for this annotation

GitHub Actions / Run linter

Unexpected block statement surrounding arrow body; move the returned value immediately after the `=>`
return JSON.parse(fs.readFileSync(`.cache/${cacheFiles()[0]}`))

Check failure on line 31 in lib/related-posts.js

View workflow job for this annotation

GitHub Actions / Run linter

'fs' is not defined
}

// Deletes all caches except the cache with the most future date
// @return [undefined]
const deleteOldCaches = () => {

Check failure on line 36 in lib/related-posts.js

View workflow job for this annotation

GitHub Actions / Run linter

'deleteOldCaches' is assigned a value but never used
cacheFiles().slice(1,).forEach(file => fs.unlinkSync(`.cache/${file}`))
}

// Deletes all caches
// @return [undefined]
const deleteAllCaches = () => {
cacheFiles().forEach(file => fs.unlinkSync(`.cache/${file}`))
}



/*
usage:

Create a bunch of related posts files

createFile()
createFile()
createFile()
createFile({hi: "tadhg"})


Get the data from the file with the latest date (it should have content).

currentCache()


Delete all the caches except the latest one

deleteOldCaches()


The current cache should still be there

currentCache()


Delete all the caches except the latest one

deleteAllCaches()


The current cache shouldn't exit

currentCache()


*/

// Compares two files' frontmatter for similar authors and similar tags and returns
// a similarity score
// Current algorithm:
// - Each author in common is +1
// - Each tag in common is +1
// - If there are both authors and tags in common, multiply the base score x2
// @param file [object] JSON data representing front matter of the current file
// @param other [object] JSON data representing front matter of the file being compared
// @return [number]
const similarity = (file, other) => {
const commonAuthors = intersection(new Set(file.authors), new Set(other.authors)).length
const commonTags = intersection(new Set(file.tags), new Set(other.tags)).length
const multiplier = (commonAuthors > 0 && commonTags > 0) ? 2 : 1
return (commonTags + commonAuthors) * multiplier
}

function intersection(setA, setB) {
const result = new Set();
setA.forEach((value) => {
if (setB.has(value)) {
result.add(value)
}
});
return result
}





Loading