Skip to content

Commit 69fc7ad

Browse files
committed
ci: add learn to generate content command
1 parent 5b5e309 commit 69fc7ad

File tree

4 files changed

+53
-12
lines changed

4 files changed

+53
-12
lines changed

bin/commands/generate/content.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
22
import { join } from 'node:path'
33
import { defineCommand } from 'citty'
44
import { consola } from 'consola'
5-
import { getBlogPath, getBlogTemplatePath } from '../../utils/content'
5+
import { getBlogPath, getBlogTemplatePath, getLearnPath, getLearnTemplatePath } from '../../utils/content'
66
import { slugify } from '../../utils/slugify'
7-
import { getBlogImagesPath } from '../../utils/public'
7+
import { getBlogImagesPath, getLearnImagesPath } from '../../utils/public'
88

99
export const content = defineCommand({
1010
args: {
@@ -15,7 +15,7 @@ export const content = defineCommand({
1515
},
1616
},
1717
async run({ args }) {
18-
const acceptedTypes = ['blog']
18+
const acceptedTypes = ['blog', 'learn']
1919

2020
const type = args.type || (await consola.prompt('Type of content to generate', {
2121
type: 'select',
@@ -46,6 +46,30 @@ export const content = defineCommand({
4646

4747
consola.success(`Successfully generated at:\nContent: ${path}\nImages: ${imagesPath}`)
4848
}
49+
50+
if (type === 'learn') {
51+
const categories = ['getting-started', 'building-blocks']
52+
53+
const category = await consola.prompt('Category of the learn article', {
54+
type: 'select',
55+
options: categories,
56+
})
57+
58+
const learnPath = getLearnPath()
59+
const learnTemplatePath = getLearnTemplatePath()
60+
const template = readFileSync(learnTemplatePath, 'utf-8')
61+
62+
const content = template.replace('learn_title', title).replaceAll('learn_date', date).replace('learn_category', category)
63+
64+
const path = join(learnPath, filename)
65+
writeContent(content, path)
66+
67+
const learnImages = getLearnImagesPath()
68+
const imagesPath = join(learnImages, filename.replace('.md', ''))
69+
generateImageFolder(imagesPath)
70+
71+
consola.success(`Successfully generated at:\nContent: ${path}\nImages: ${imagesPath}`)
72+
}
4973
},
5074
})
5175

bin/utils/content.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs'
22
import { join } from 'node:path'
33
import { cwd } from 'node:process'
4-
import yaml from 'js-yaml'
4+
import { dump, load } from 'js-yaml'
55
import { ofetch } from 'ofetch'
66
import type { ContentPackage, GitHubFile } from '../types'
77

@@ -30,11 +30,11 @@ export function getBlogTemplatePath() {
3030
}
3131

3232
export function loadPackageContent(name: string) {
33-
return yaml.load(readFileSync(join(getPackagesPath(), `${name}.yml`), 'utf-8')) as ContentPackage
33+
return load(readFileSync(join(getPackagesPath(), `${name}.yml`), 'utf-8')) as ContentPackage
3434
}
3535

3636
export function writePackageContent(package_: ContentPackage) {
37-
writeFileSync(join(getPackagesPath(), `${package_.title}.yml`), yaml.dump(package_))
37+
writeFileSync(join(getPackagesPath(), `${package_.title}.yml`), dump(package_))
3838
}
3939

4040
export function getPackages() {
@@ -70,3 +70,15 @@ export async function getExamplesLink(name: string) {
7070

7171
return hasExamples ? `https://github.com/unjs/${name}/blob/main/examples` : null
7272
}
73+
74+
export function getLearnPath() {
75+
const contentPath = getContentPath()
76+
77+
return join(contentPath, 'learn', 'articles')
78+
}
79+
80+
export function getLearnTemplatePath() {
81+
const learnPath = getLearnPath()
82+
83+
return join(learnPath, '.template.md')
84+
}

bin/utils/public.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ export function getBlogImagesPath() {
3232
return join(imagesPath, 'blog')
3333
}
3434

35+
export function getLearnImagesPath() {
36+
const imagesPath = getImagesPath()
37+
38+
return join(imagesPath, 'learn')
39+
}
40+
3541
/**
3642
* Get list of puzzle parts, without directory or the extension
3743
*/
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
---
2-
title:
2+
title: learn_title
33
description:
44
authors:
55
- name:
66
picture:
77
twitter:
8-
category: # getting-started | building-blocks
8+
category: learn_category
99
packages:
1010
-
11-
resources: # link to external resources
11+
resources:
1212
- label:
1313
to:
1414
icon:
15-
publishedAt:
16-
modifiedAt:
15+
publishedAt: learn_date
16+
modifiedAt: learn_date
1717
---
1818

1919
Checklist:
2020

21-
- [ ] Filename is the title stringified prefix with the date in the format YYYY-MM-DD and replacing dot with dash
2221
- [ ] Packages must match the package filename

0 commit comments

Comments
 (0)