|
| 1 | +import { readFileSync, existsSync } from "fs"; |
| 2 | +import Translatable from "./Translatable"; |
| 3 | +import "./Article.scss"; |
| 4 | +import prismjs from "prismjs"; |
| 5 | +import { Remarkable } from "remarkable"; |
| 6 | +import meta from "remarkable-meta"; |
| 7 | + |
| 8 | +class Post extends Translatable { |
| 9 | + html = ""; |
| 10 | + |
| 11 | + static async getPostByKey({ locale, key }) { |
| 12 | + await import("prismjs/components/prism-jsx.min"); |
| 13 | + let path = `i18n/${locale}/posts/${key}.md`; |
| 14 | + if (!existsSync(path)) { |
| 15 | + path = `i18n/${locale}/posts/404.md`; |
| 16 | + } |
| 17 | + const text = readFileSync(path, "utf-8"); |
| 18 | + const md = new Remarkable({ |
| 19 | + highlight: (code) => |
| 20 | + Prism.highlight(code, prismjs.languages.jsx, "javascript"), |
| 21 | + }); |
| 22 | + md.use(meta); |
| 23 | + md.use((md) => { |
| 24 | + const originalRender = md.renderer.rules.link_open; |
| 25 | + md.renderer.rules.link_open = function () { |
| 26 | + let result = originalRender.apply(null, arguments); |
| 27 | + const regexp = /href="([^"]*)"/; |
| 28 | + const href = regexp.exec(result)[1]; |
| 29 | + if (!href.startsWith("/")) { |
| 30 | + result = result.replace(">", ' target="_blank" rel="noopener">'); |
| 31 | + } |
| 32 | + return result; |
| 33 | + }; |
| 34 | + }); |
| 35 | + md.use((md) => { |
| 36 | + md.renderer.rules.heading_open = function (tokens, i) { |
| 37 | + const { content } = tokens[i + 1]; |
| 38 | + const { hLevel } = tokens[i]; |
| 39 | + const id = content |
| 40 | + .toLowerCase() |
| 41 | + .split(/[^a-z]/) |
| 42 | + .join("-"); |
| 43 | + return `<h${hLevel} id="${id}"><a href="#${id}">`; |
| 44 | + }; |
| 45 | + md.renderer.rules.heading_close = function (tokens, i) { |
| 46 | + const { hLevel } = tokens[i]; |
| 47 | + return `</a></h${hLevel}>`; |
| 48 | + }; |
| 49 | + }); |
| 50 | + return { |
| 51 | + html: md.render(text), |
| 52 | + ...md.meta, |
| 53 | + }; |
| 54 | + } |
| 55 | + |
| 56 | + async initiate({ page, locale, params }) { |
| 57 | + super.initiate({ page, locale }); |
| 58 | + const post = await this.getPostByKey({ key: params.slug, locale }); |
| 59 | + Object.assign(this, post); |
| 60 | + } |
| 61 | + |
| 62 | + launch({ project, page }) { |
| 63 | + page.title = `${this.title} - ${project.name}`; |
| 64 | + page.description = this.description; |
| 65 | + if (this.status) { |
| 66 | + page.status = 404; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + render() { |
| 71 | + if (!this.html) return false; |
| 72 | + return ( |
| 73 | + <section class="max-w-screen-md mx-auto px-4 flex flex-wrap sm:flex-nowrap py-12 sm:py-24"> |
| 74 | + <article class="w-full pb-24"> |
| 75 | + <h1 class="text-pink-600 text-4xl font-light block">{this.title}</h1> |
| 76 | + <div class="opacity-80 mb-8"> |
| 77 | + <span class="mr-2"> |
| 78 | + By{" "} |
| 79 | + <a |
| 80 | + href={`https://github.com/${this.author.handle}`} |
| 81 | + rel="noopener" |
| 82 | + target="_blank" |
| 83 | + > |
| 84 | + {this.author.name} |
| 85 | + </a> |
| 86 | + </span> |
| 87 | + <span class="mr-2">|</span> |
| 88 | + <span>{this.date}</span> |
| 89 | + </div> |
| 90 | + |
| 91 | + <div |
| 92 | + html={this.html} |
| 93 | + class="prose dark:prose-dark max-w-none text-lg" |
| 94 | + /> |
| 95 | + </article> |
| 96 | + </section> |
| 97 | + ); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +export default Post; |
0 commit comments