-
Notifications
You must be signed in to change notification settings - Fork 17
/
fix-cannonical.ts
53 lines (45 loc) · 1.44 KB
/
fix-cannonical.ts
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
44
45
46
47
48
49
50
51
52
53
import { readdir, readFile, writeFile } from 'fs/promises'
import { join as pathJoin } from 'path'
import { promisify } from 'util'
import readline from 'readline'
import matter from 'gray-matter'
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
})
async function answer(question: string): Promise<string> {
return new Promise(resolve => {
rl.question(question, resolve)
})
}
async function main() {
const basePath = pathJoin(__dirname, 'content', 'blog')
const blogPostsFiles = await readdir(basePath)
const blogPosts = await Promise.all(
blogPostsFiles.map(async blogPost => {
const filePath = pathJoin(basePath, blogPost)
return {
matter: matter(await readFile(filePath)),
filePath,
}
})
)
const blogPostsCross = blogPosts
.filter(post => post.matter.data.crosspost)
.filter(post => post.matter.data.author == 'piotr')
.filter(post => !post.matter.data.canonical_url)
for (const blogPost of blogPostsCross) {
const canonicalUrl = await answer(`Canonical url for ${blogPost.filePath}:\n`)
if (canonicalUrl) {
console.log('Setting canonical url to', canonicalUrl)
blogPost.matter.data.canonical_url = canonicalUrl
const newContent = matter.stringify(blogPost.matter, blogPost.matter.data)
await writeFile(blogPost.filePath, newContent)
}
}
rl.close()
}
main().catch(err => {
console.error(err)
process.exit(1)
})