|
| 1 | +import axios from 'axios'; |
| 2 | +import * as emoji from 'node-emoji' |
| 3 | +import { JekyllMarkdownParser } from './jekyll-markdown-parser'; |
| 4 | +import { readdir, readFile } from 'fs/promises'; |
| 5 | + |
| 6 | +import { BlogEntry } from './types'; |
| 7 | + |
| 8 | +export class BlogService { |
| 9 | + |
| 10 | + private blogListFetched = this.getBlogListFromAPI(); |
| 11 | + |
| 12 | + constructor(private settings: { |
| 13 | + headers: { |
| 14 | + 'User-Agent': string, |
| 15 | + 'Authorization': string |
| 16 | + }, |
| 17 | + contentsRepo: string, |
| 18 | + branch: string, |
| 19 | + markdownBaseUrl: string |
| 20 | + }) {} |
| 21 | + |
| 22 | + getBlogList() { |
| 23 | + return this.blogListFetched; |
| 24 | + } |
| 25 | + |
| 26 | + async getBlogEntry(slug: string): Promise<BlogEntry> { |
| 27 | + const blogList = await this.getBlogList(); |
| 28 | + const entry = blogList.find(e => e.slug === slug); |
| 29 | + if (!entry) { |
| 30 | + throw new Error(`No entry found for "${slug}"`); |
| 31 | + } |
| 32 | + return entry; |
| 33 | + } |
| 34 | + |
| 35 | + private sortBy<T extends { [key: string]: string }, K extends keyof T>(a: T, b: T, prop: K) { |
| 36 | + return a[prop].localeCompare(b[prop]); |
| 37 | + } |
| 38 | + |
| 39 | + // dead simple way to sort things: create a sort key that can be easily sorted |
| 40 | + private getSortKey(entry: BlogEntry) { |
| 41 | + return (entry.meta.sticky ? 'Z' : 'A') + '---' + (+entry.meta.published) + '---' + entry.slug; |
| 42 | + } |
| 43 | + |
| 44 | + private async readBlogFolders() { |
| 45 | + const folderContents = await readdir('../blog', { withFileTypes: true }); |
| 46 | + return folderContents |
| 47 | + .filter(dirent => dirent.isDirectory()) |
| 48 | + .map(dirent => dirent.name); |
| 49 | + } |
| 50 | + |
| 51 | + private async readBlogFileFromFolder(folder: string) { |
| 52 | + const path = `../blog/${folder}/README.md`; |
| 53 | + return readFile(path, 'utf8'); |
| 54 | + } |
| 55 | + |
| 56 | + private async getBlogListFromAPI() { |
| 57 | + const blogDirs = await this.readBlogFolders(); |
| 58 | + const blogEntries: BlogEntry[] = []; |
| 59 | + |
| 60 | + for (const blogDir of blogDirs) { |
| 61 | + |
| 62 | + try { |
| 63 | + const readme = await this.readBlogFileFromFolder(blogDir); |
| 64 | + const blogEntry = this.readmeToBlogEntry(readme, blogDir); |
| 65 | + blogEntries.push(blogEntry); |
| 66 | + |
| 67 | + } catch (e: any) { |
| 68 | + const errorBlogEntry = { |
| 69 | + slug: this.slugifyPath(blogDir), |
| 70 | + html_url: '', |
| 71 | + error: e.message || 'Error', |
| 72 | + meta: { |
| 73 | + title: 'A minor error occurred while reading: ' + blogDir, |
| 74 | + hidden: true |
| 75 | + } |
| 76 | + } as BlogEntry; |
| 77 | + blogEntries.push(errorBlogEntry); |
| 78 | + |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return blogEntries.sort((a, b) => this.getSortKey(b).localeCompare(this.getSortKey(a))); |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + private readmeToBlogEntry(readme: string, folder: string) { |
| 87 | + const parser = new JekyllMarkdownParser(this.settings.markdownBaseUrl + folder); |
| 88 | + const parsedJekyllMarkdown = parser.parse(readme); |
| 89 | + |
| 90 | + const meta = parsedJekyllMarkdown.parsedYaml || {}; |
| 91 | + |
| 92 | + if (meta.thumbnail && |
| 93 | + !meta.thumbnail.startsWith('http') && |
| 94 | + !meta.thumbnail.startsWith('//')) { |
| 95 | + meta.thumbnail |
| 96 | + = this.settings.markdownBaseUrl + folder + meta.thumbnail; |
| 97 | + } |
| 98 | + |
| 99 | + return { |
| 100 | + slug: this.slugifyPath(folder), |
| 101 | + html: emoji.emojify(parsedJekyllMarkdown.html), |
| 102 | + meta: meta |
| 103 | + } as BlogEntry; |
| 104 | + } |
| 105 | + |
| 106 | + private slugifyPath(path: string): string { |
| 107 | + if (!path) { |
| 108 | + return 'error - no path??'; |
| 109 | + } |
| 110 | + |
| 111 | + return path |
| 112 | + .replace(/README\.md$/, '') |
| 113 | + .replace('blog/', '') |
| 114 | + .replace(/\/$/, '') |
| 115 | + .replace('/', '|'); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | + |
0 commit comments