First few blogs.
This commit is contained in:
parent
cbe09fc194
commit
702469464e
19 changed files with 719 additions and 28 deletions
64
scripts/articles.ts
Normal file
64
scripts/articles.ts
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
import path from 'path';
|
||||
|
||||
const ARTICLE_PATH = 'src/routes/articles';
|
||||
|
||||
const getFileDates = async (file: string) => {
|
||||
const datePromises = [
|
||||
Bun.$`git log -1 --format="%cd" --date=format:"%Y-%m-%d %H:%M:%S" ${ARTICLE_PATH}/${file}`.text(),
|
||||
Bun.$`git log -1 --reverse --format="%cd" --date=format:"%Y-%m-%d" ${ARTICLE_PATH}/${file}`.text()
|
||||
];
|
||||
|
||||
const dates = await Promise.all(datePromises);
|
||||
|
||||
|
||||
return dates.map((d: string) => {
|
||||
if (!d) { return new Date(); }
|
||||
return new Date(d);
|
||||
});
|
||||
};
|
||||
|
||||
export const generateBlogData = async () => {
|
||||
try {
|
||||
const glob = new Bun.Glob('**/+page.svx');
|
||||
const files = Array.from(glob.scanSync(ARTICLE_PATH))
|
||||
const data = await Promise.all(
|
||||
files.map(async (file) => {
|
||||
const [updated, created] = await getFileDates(file);
|
||||
const fullname = file.replace('/+page.svx', '');
|
||||
const title = fullname.replace(/d+-/, "").replace(/^\d+-/, '');
|
||||
return { title, updated, created, path: `/articles/${fullname} ` };
|
||||
})
|
||||
);
|
||||
|
||||
|
||||
await Bun.write(
|
||||
path.join('src/lib/articles.json'),
|
||||
JSON.stringify({
|
||||
articles: data
|
||||
})
|
||||
);
|
||||
} catch (e) {
|
||||
console.error('Error generating blog data');
|
||||
console.log(e);
|
||||
}
|
||||
};
|
||||
|
||||
export default function () {
|
||||
const glob = new Bun.Glob('**/src/routes/articles/*/+page.svx');
|
||||
|
||||
return {
|
||||
name: 'articles',
|
||||
buildStart: {
|
||||
order: 'first',
|
||||
handler: async () => {
|
||||
await generateBlogData();
|
||||
}
|
||||
},
|
||||
watchChange: async (id: string, change: { event: string }) => {
|
||||
if (!glob.match(id) && change.event) {
|
||||
return;
|
||||
}
|
||||
await generateBlogData();
|
||||
}
|
||||
};
|
||||
}
|
||||
21
scripts/new-article.ts
Normal file
21
scripts/new-article.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#!/usr/bin/env bun
|
||||
|
||||
const ARTICLE_PATH = 'src/routes/articles';
|
||||
|
||||
const glob = new Bun.Glob("**/+page.svx");
|
||||
|
||||
const files = Array.from(glob.scanSync(ARTICLE_PATH));
|
||||
|
||||
const index = files.length;
|
||||
|
||||
const title = prompt('Title : ');
|
||||
|
||||
const slug = title!.replace(/\s+/g, '-').toLowerCase();
|
||||
|
||||
const filename = `${index.toString().padStart(4, '0')}-${slug}`;
|
||||
|
||||
await Bun.write(ARTICLE_PATH + '/' + `${filename}` + '/+page.svx', "");
|
||||
|
||||
console.log(`Created ${filename} +page.svx`);
|
||||
|
||||
export { };
|
||||
Loading…
Add table
Add a link
Reference in a new issue