gtm-test-site/articlegen/index.mjs

74 lines
1.6 KiB
JavaScript
Raw Normal View History

2024-01-23 17:47:24 +03:00
import fs from 'fs'
import path from 'path'
import { sentence, article } from 'txtgen'
import { extract as buildKeywords } from 'keyword-extractor'
import Mustache from 'mustache'
import dayjs from 'dayjs'
const articleTemplate = `+++
title = "{{title}}"
tags = [
{{#keywords}}
"{{.}}",
{{/keywords}}
]
date = "{{createdAt}}"
categories = [
"placeholder",
"generated",
]
series = ["Insane Outputs"]
author = "insane txtgen"
+++
{{#articleParts}}
# {{title}}
{{paragraphs}}
{{/articleParts}}
`
function generateArticle (created) {
const title = sentence()
const keywords = buildKeywords(article(2), {
language:"english",
remove_digits: true,
return_changed_case:true,
remove_duplicates: false,
}).splice(0, 10)
const createdAt = dayjs(created).format('YYYY-MM-DD')
const articleParts = []
for (let i = 0; i < Math.trunc((Math.random() * 9) + 1); i++) {
articleParts.push({
title: sentence(),
paragraphs: article(Math.trunc((Math.random() * 20) + 4)),
})
}
return Mustache.render(articleTemplate, {
title, keywords, createdAt, articleParts
})
}
let startDate = new Date(2018, 1, 1)
const targetDir = 'output'
if (!fs.existsSync(targetDir)){
fs.mkdirSync(targetDir);
}
for (let i = 0; i < 2000; i++) {
const article = generateArticle(startDate)
try {
fs.writeFileSync(path.join(targetDir, `${i}_generated.md`), article);
} catch (err) {
console.error(err);
}
startDate.setDate(startDate.getDate() + 1)
}
console.log('Done.')