-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build(feat): add kurzschnitte as content type to new script
closes #1213
- Loading branch information
1 parent
101a3d6
commit bfc0900
Showing
5 changed files
with
298 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
'$schema': /static/_schemata/blog.schema.yaml | ||
type: blog | ||
title: {{ replace .Name "-" " " | title }} | ||
linkTitle: {{ replace .Name "-" " " | title }} | ||
description: "" | ||
summary: "" | ||
|
||
draft: true | ||
|
||
date: "{{ .Date }}" | ||
publishDate: "{{ .Date }}" | ||
lastmod: "{{ .Date }}" | ||
|
||
resources: | ||
- title: Photo by [Kelsy Gagnebin](https://unsplash.com/@kelsymichael) via [Unsplash](https://unsplash.com/) | ||
src: header.jpg | ||
categories: | ||
- kurzschnitte | ||
tags: | ||
- kurzschnitte | ||
- bookmarks | ||
- 100DaysToOffload | ||
type: blog | ||
unsplash: | ||
imageid: UcEzgZ6k19o | ||
|
||
--- | ||
|
||
INTRODUCTION | ||
|
||
## Webdev | ||
|
||
## Learn | ||
|
||
## Auditing | ||
|
||
## Food for though | ||
|
||
## Fun and stuff | ||
|
||
## Listen | ||
|
||
## Watch | ||
|
||
## What you missed | ||
|
||
{{< tagnavigation "kurzschnitte" >}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"latest": 7 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
// @see https://github.com/natemoo-re/clack/tree/main/packages/prompt | ||
import { | ||
intro, | ||
outro, | ||
select, | ||
spinner, | ||
isCancel, | ||
cancel, | ||
text, | ||
} from '@clack/prompts'; | ||
|
||
// see https://github.com/unjs/consola | ||
import { consola, createConsola } from "consola"; | ||
|
||
import { exec as exec } from 'node:child_process'; | ||
import fs from 'node:fs'; | ||
import toml from 'toml'; | ||
import path from 'node:path'; | ||
|
||
import { fileURLToPath } from 'node:url'; | ||
|
||
// Read TOML config file | ||
const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
const configPath = path.resolve(__dirname, 'content.toml'); | ||
let config; | ||
try { | ||
const configFile = fs.readFileSync(configPath, 'utf8'); | ||
config = toml.parse(configFile); | ||
} catch (err) { | ||
consola.error(`Error reading TOML config file: ${err}`); | ||
process.exit(0); | ||
} | ||
|
||
async function prepareSlug(title) { | ||
const slug_pre = title.replace(/\s+/g, '-').toLowerCase(); | ||
const slug = slug_pre.replace(/[^a-zA-Z0-9\-]/g, ''); | ||
return slug; | ||
} | ||
|
||
/** | ||
* Increments a counter in a JSON file. | ||
* @param {string} filename - The path to the JSON file. | ||
* @returns {Promise<number>} - A promise that resolves with the incremented counter value. | ||
*/ | ||
async function incrementCounter(filename) { | ||
return new Promise((resolve, reject) => { | ||
fs.readFile(filename, 'utf8', (err, data) => { | ||
if (err) { | ||
console.error(err); | ||
reject(err); | ||
return; | ||
} | ||
let jsonData = JSON.parse(data); | ||
let number = jsonData.latest; | ||
let increment = number + 1; | ||
jsonData.latest = increment; | ||
fs.writeFile(filename, JSON.stringify(jsonData, null, 2), (err) => { | ||
if (err) { | ||
consola.error(err); | ||
reject(err); | ||
return; | ||
} | ||
resolve(increment); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
// Function to handle file reading errors | ||
function handleFileReadError(err, filename) { | ||
consola.error(`Error reading file '${filename}': ${err}`); | ||
cancel(`An error occurred while reading file '${filename}': ${err}`); | ||
process.exit(0); | ||
} | ||
|
||
// Function to handle file write errors | ||
function handleFileWriteError(err, filename) { | ||
consola.error(`Error writing to file '${filename}': ${err}`); | ||
cancel(`An error occurred while writing to file '${filename}': ${err}`); | ||
process.exit(0); | ||
} | ||
|
||
// Function to handle command execution errors | ||
function handleCommandExecutionError(err, command) { | ||
consola.error(`Error executing command '${command}': ${err}`); | ||
cancel(`An error occurred while executing command '${command}': ${err}`); | ||
process.exit(0); | ||
} | ||
|
||
// Validate title or slug input | ||
function validateTitleOrSlug(value, errorMessage) { | ||
if (!value.trim()) { | ||
return errorMessage; | ||
} | ||
// You can add more validation rules here if needed | ||
} | ||
|
||
async function main() { | ||
|
||
let command, command2, title, slug, post; | ||
const date = new Date(); | ||
const year = date.getFullYear(); | ||
const spin = spinner(); | ||
|
||
intro('Create content:'); | ||
|
||
const quitOption = [{ value: "quit", label: "Quit" }]; | ||
|
||
// Merge the config.contentOptions array with the quitOption array | ||
const mergedOptions = [...config.contentOptions, ...quitOption]; | ||
|
||
const contentType = await select({ | ||
message: 'Pick a content type.', | ||
options: mergedOptions | ||
}); | ||
|
||
if (isCancel(contentType)) { | ||
cancel('Operation cancelled'); | ||
return process.exit(0); | ||
}; | ||
|
||
switch (contentType) { | ||
|
||
case 'quit': | ||
|
||
outro('Operation cancelled'); | ||
return process.exit(0); | ||
|
||
default: | ||
|
||
const itemContentOptions = config.contentOptions.find( | ||
(/** @type {{ value: any; }} */ option) => option.value === contentType | ||
); | ||
|
||
const { | ||
kind, | ||
path: contentPath, | ||
customSlug = true, | ||
counter = false, | ||
labels = { | ||
title: 'Post Slug (special characters will be removed):', | ||
titleError: 'Slug is required, doh!', | ||
}, | ||
} = itemContentOptions; | ||
|
||
if (customSlug) { | ||
title = await text({ | ||
message: labels.title, | ||
validate(value) { | ||
if (value.length === 0) return labels.titleError; | ||
} | ||
}); | ||
slug = await prepareSlug(title); | ||
} | ||
|
||
let postPath = contentPath.replace('${year}', year).replace('${slug}', slug); | ||
|
||
if (counter) { | ||
const counterInt = await incrementCounter(counter); | ||
postPath = postPath.replace('${counter}', counterInt); | ||
} | ||
|
||
command = `hugo new --kind ${kind} ${postPath}`; | ||
command2 = `code content/${postPath}/index.md`; | ||
|
||
break; | ||
|
||
} | ||
|
||
spin.start('Creating content files...'); | ||
|
||
await /** @type {Promise<void>} */(new Promise((resolve, _reject) => { | ||
exec(command, function (error, _stdout, stderr) { | ||
if (error) { | ||
consola.error(error); | ||
cancel(`An error occured: ${error}`); | ||
process.exit(0); | ||
} | ||
if (stderr) { | ||
consola.error(stderr); | ||
cancel(`An error occured: ${stderr}`); | ||
process.exit(0); | ||
} | ||
resolve(); | ||
}); | ||
})); | ||
|
||
spin.stop('Content created. Opening in VS Code...'); | ||
|
||
await /** @type {Promise<void>} */(new Promise((resolve, _reject) => { | ||
exec(command2, function (error, _stdout, stderr) { | ||
if (error) { | ||
consola.error(error); | ||
cancel(`An error occured: ${error}`); | ||
process.exit(0); | ||
} | ||
if (stderr) { | ||
consola.error(stderr); | ||
cancel(`An error occured: ${stderr}`); | ||
process.exit(0); | ||
} | ||
resolve(); | ||
}); | ||
})); | ||
|
||
outro('All done :]'); | ||
process.exit(1); | ||
|
||
} | ||
|
||
main().catch(consola.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
[[contentOptions]] | ||
value = "post" | ||
label = "Post" | ||
kind = "blog" | ||
path = "blog/${year}/${slug}" | ||
|
||
[[contentOptions]] | ||
value = "video" | ||
label = "Video Post" | ||
kind = "video" | ||
path = "blog/${year}/${slug}" | ||
|
||
[[contentOptions]] | ||
value = "m2p2" | ||
label = "Music to program to" | ||
kind = "music2program2" | ||
counter = "data/dnb/kollitsch/m2p2.json" | ||
path = "blog/${year}/music-to-program-to-${counter}" | ||
customSlug = false | ||
|
||
[[contentOptions]] | ||
value = "kurzschnitte" | ||
label = "Kurzschnitte" | ||
kind = "kurzschnitte" | ||
counter = "data/dnb/kollitsch/kurzschnitt-counter.json" | ||
path = "blog/${year}/kurzschnitte-ii-${counter}" | ||
customSlug = false | ||
|
||
[[contentOptions]] | ||
value = "tag" | ||
label = "Tag" | ||
hint = "Tag description page" | ||
path = "tags/${slug}" | ||
[contentOptions.labels] | ||
title = "Tag Title:" | ||
titleError = "Tag title is required" |