2022-10-16 09:37:13 +00:00
|
|
|
import { join } from 'path';
|
|
|
|
import fs from 'fs/promises';
|
|
|
|
import yaml from 'yaml';
|
|
|
|
import { marked } from 'marked';
|
|
|
|
import hljs from 'highlight.js';
|
|
|
|
import { getDateObject } from '../utils/date-object';
|
2022-12-04 10:12:29 +00:00
|
|
|
import { ArchiveDto, BlogPost, BlogPostTag } from '../types/post';
|
2022-10-16 09:37:13 +00:00
|
|
|
|
|
|
|
const dir = join('content', 'blog');
|
|
|
|
|
|
|
|
marked.use({
|
|
|
|
renderer: {
|
|
|
|
code: (code, info, escaped): string => {
|
2022-12-04 10:12:29 +00:00
|
|
|
const language = hljs.getLanguage(info as string)
|
|
|
|
? (info as string)
|
|
|
|
: 'plaintext';
|
2022-10-16 09:37:13 +00:00
|
|
|
const created = hljs.highlight(code, { language }).value;
|
|
|
|
return `<div class="codeblock"><pre><code class="hljs language-${language}">${created}</code></pre></div>`;
|
|
|
|
},
|
2023-09-22 15:01:40 +00:00
|
|
|
link: (
|
|
|
|
href: string,
|
|
|
|
title: string | undefined | null,
|
|
|
|
text: string,
|
|
|
|
): string => {
|
2022-10-16 11:04:03 +00:00
|
|
|
return `<a href="${href}" rel="nofollow" target="_blank"${
|
|
|
|
title ? ` title="${title}"` : ''
|
|
|
|
}>${text}</a>`;
|
|
|
|
},
|
2022-10-16 09:37:13 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-10-16 16:17:55 +00:00
|
|
|
async function readBlogPosts() {
|
2022-10-16 09:37:13 +00:00
|
|
|
const files = await fs.readdir(dir);
|
|
|
|
const readMD = files.filter((file) => file.endsWith('.md'));
|
2022-12-04 10:12:29 +00:00
|
|
|
const readFiles: BlogPost[] = [];
|
2022-10-16 16:17:55 +00:00
|
|
|
|
2022-10-16 09:37:13 +00:00
|
|
|
for (const file of readMD) {
|
2022-10-16 16:17:55 +00:00
|
|
|
const post = await readBlogPost(file.replace('.md', ''));
|
2022-10-16 09:37:13 +00:00
|
|
|
if (!post) continue;
|
|
|
|
readFiles.push(post);
|
|
|
|
}
|
2022-10-16 16:17:55 +00:00
|
|
|
|
2022-10-16 09:37:13 +00:00
|
|
|
readFiles.sort((a, b) =>
|
|
|
|
new Date(b.date)
|
|
|
|
.toISOString()
|
2023-09-22 15:01:40 +00:00
|
|
|
.localeCompare(new Date(a.date).toISOString(), 'en', { numeric: true }),
|
2022-10-16 09:37:13 +00:00
|
|
|
);
|
2022-10-16 16:17:55 +00:00
|
|
|
|
2022-10-16 09:37:13 +00:00
|
|
|
return readFiles;
|
|
|
|
}
|
|
|
|
|
2022-12-04 10:12:29 +00:00
|
|
|
async function readBlogPost(slug: string): Promise<BlogPost> {
|
2022-10-16 09:37:13 +00:00
|
|
|
const decoded = decodeURIComponent(slug);
|
|
|
|
if (!slug || decoded.includes('/') || decoded.includes('.'))
|
|
|
|
throw new Error('Invalid post slug');
|
|
|
|
|
|
|
|
const file = `${slug}.md`;
|
|
|
|
const mdpath = join(dir, file);
|
|
|
|
const read = await fs.readFile(mdpath, { encoding: 'utf-8' });
|
|
|
|
|
|
|
|
const { header: parsedHeader, length: headerLength } = await readHeader(read);
|
2022-10-16 16:17:55 +00:00
|
|
|
const renderedMd = await marked(read.substring(headerLength), {
|
|
|
|
async: true,
|
|
|
|
});
|
|
|
|
|
2022-12-04 10:12:29 +00:00
|
|
|
const boundary = renderedMd.indexOf('<!-- more -->');
|
2022-10-20 18:52:42 +00:00
|
|
|
|
2022-10-16 09:37:13 +00:00
|
|
|
const { year, month, day } = getDateObject(parsedHeader);
|
|
|
|
const content = {
|
|
|
|
...parsedHeader,
|
|
|
|
file,
|
|
|
|
slug,
|
2022-10-20 18:52:42 +00:00
|
|
|
summary: renderedMd.substring(0, boundary > -1 ? boundary : 240),
|
2022-10-16 09:37:13 +00:00
|
|
|
fullSlug: `${year}/${month}/${day}/${slug}`,
|
2022-10-16 16:17:55 +00:00
|
|
|
markdown: read.substring(headerLength),
|
2022-10-20 18:52:42 +00:00
|
|
|
html: renderedMd.replace('<!-- more -->', ''),
|
2022-10-16 09:37:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
2022-10-16 16:17:55 +00:00
|
|
|
let blogPostCache: BlogPost[] = [];
|
|
|
|
export const blogPostList = async () => {
|
|
|
|
if (!blogPostCache.length) {
|
|
|
|
blogPostCache = await readBlogPosts();
|
|
|
|
}
|
|
|
|
return blogPostCache;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getFilteredBlogPosts = async (
|
|
|
|
condition?: (body: BlogPost) => boolean,
|
|
|
|
htmlContent = true,
|
2023-09-22 15:01:40 +00:00
|
|
|
includeMarkdown = true,
|
2022-10-16 16:17:55 +00:00
|
|
|
): Promise<BlogPost[]> => {
|
|
|
|
const posts = await blogPostList();
|
|
|
|
return posts
|
|
|
|
.filter((item) => (condition ? condition(item) : true))
|
|
|
|
.map((item) => ({
|
|
|
|
...item,
|
|
|
|
html: htmlContent ? item.html : undefined,
|
|
|
|
markdown: includeMarkdown ? item.markdown : undefined,
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getBlogPost = async (
|
|
|
|
slug: string,
|
|
|
|
htmlContent = true,
|
2023-09-22 15:01:40 +00:00
|
|
|
includeMarkdown = true,
|
2022-12-04 10:12:29 +00:00
|
|
|
): Promise<BlogPost | undefined> => {
|
2022-10-16 16:17:55 +00:00
|
|
|
const posts = await blogPostList();
|
|
|
|
const item = posts.find((item) => item.slug === slug);
|
|
|
|
if (!item) return;
|
|
|
|
|
|
|
|
const index = posts.indexOf(item);
|
|
|
|
const next = index > 0 ? posts[index - 1] : undefined;
|
|
|
|
const prev = posts[index + 1];
|
|
|
|
|
|
|
|
return {
|
|
|
|
...item,
|
|
|
|
html: htmlContent ? item.html : undefined,
|
|
|
|
markdown: includeMarkdown ? item.markdown : undefined,
|
|
|
|
next: next
|
|
|
|
? { title: next.title, slug: next.slug, fullSlug: next.fullSlug }
|
|
|
|
: undefined,
|
|
|
|
prev: prev
|
|
|
|
? { title: prev.title, slug: prev.slug, fullSlug: prev.fullSlug }
|
|
|
|
: undefined,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-10-16 11:04:03 +00:00
|
|
|
export async function getTags(): Promise<BlogPostTag[]> {
|
2022-10-16 16:17:55 +00:00
|
|
|
const posts = await getFilteredBlogPosts(undefined, false, false);
|
2022-10-16 11:04:03 +00:00
|
|
|
const obj: BlogPostTag[] = [];
|
2022-10-16 09:37:13 +00:00
|
|
|
|
|
|
|
for (const post of posts) {
|
|
|
|
for (const tag of post.tags || []) {
|
|
|
|
const find = obj.find((item) => item.name === tag);
|
|
|
|
if (find) {
|
|
|
|
find.count++;
|
|
|
|
find.posts.push(post.slug);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.push({
|
|
|
|
name: tag,
|
|
|
|
count: 1,
|
|
|
|
posts: [post.slug],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-16 12:50:21 +00:00
|
|
|
obj.sort((a, b) => a.name.localeCompare(b.name, 'en'));
|
|
|
|
|
2022-10-16 09:37:13 +00:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2022-10-16 11:04:03 +00:00
|
|
|
export async function getArchiveTree(condition?: (body: BlogPost) => boolean) {
|
2022-10-16 16:17:55 +00:00
|
|
|
const posts = await getFilteredBlogPosts(condition, false, false);
|
2022-12-04 10:12:29 +00:00
|
|
|
const obj: ArchiveDto = {};
|
2022-10-16 09:37:13 +00:00
|
|
|
|
|
|
|
for (const post of posts) {
|
|
|
|
const { year, month, day } = getDateObject(post);
|
|
|
|
|
|
|
|
if (!obj[year]) {
|
|
|
|
obj[year] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!obj[year][month]) {
|
|
|
|
obj[year][month] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!obj[year][month][day]) {
|
|
|
|
obj[year][month][day] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
obj[year][month][day].push(post.slug);
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function generatePaths(prefix = '/') {
|
|
|
|
const tree = await getArchiveTree();
|
|
|
|
const paths = [];
|
|
|
|
|
|
|
|
for (const year of Object.keys(tree)) {
|
|
|
|
paths.push(`${prefix}archive/${year}`);
|
|
|
|
for (const month of Object.keys(tree[year])) {
|
|
|
|
paths.push(`${prefix}archive/${year}/${month}`);
|
|
|
|
for (const day of Object.keys(tree[year][month])) {
|
|
|
|
paths.push(`${prefix}archive/${year}/${month}/${day}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
paths.sort((a, b) => b.localeCompare(a, 'en', { numeric: true }));
|
|
|
|
|
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function readHeader(fileContents: string) {
|
|
|
|
const splitter = fileContents.split('\n');
|
|
|
|
let header = '';
|
|
|
|
|
|
|
|
for (const line of splitter) {
|
|
|
|
if (line === '---') {
|
|
|
|
if (!header.length) continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
header += line + '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
header: yaml.parse(header),
|
|
|
|
length: header.length + 9,
|
|
|
|
};
|
|
|
|
}
|