From e6e65df94e7cd1d68995ffd1764b30f5e8018062 Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Tue, 6 Jun 2023 14:23:53 +0300 Subject: [PATCH] replace fastify-markdown with remark --- lib/index.js | 37 +++++++++++++++++++++---------------- package.json | 7 +++++-- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/lib/index.js b/lib/index.js index 4356861..4f0217a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,34 +1,39 @@ +import {createRequire} from 'module'; +import {unified} from 'unified' +import remarkParse from 'remark-parse' +import remarkRehype from 'remark-rehype' +import rehypeStringify from 'rehype-stringify' +import fs from 'fs' +import path from 'path' + +const require = createRequire(import.meta.url); +const __dirname = path.dirname(new URL(import.meta.url).pathname); + const fastify = require('fastify'); const fastifyView = require('@fastify/view'); -const fastifyMarkdown = require('fastify-markdown'); const fastifyStatic = require('@fastify/static'); const ejs = require('ejs'); -const fs = require('fs'); -const path = require('path'); const staticsPath = path.join(__dirname, 'public'); const viewsPath = path.join(__dirname, 'views'); const homepagePath = path.join(__dirname, '..', 'HOMEPAGE.md'); + +const processor = unified() + .use(remarkParse) + .use(remarkRehype) + .use(rehypeStringify) + const homepageMD = fs.readFileSync(homepagePath, 'utf8'); +const homepageHTML = processor.processSync(homepageMD) const app = fastify({logger: true}); -app.register(fastifyView, { - engine: {ejs}, - root: viewsPath, -}); +app.register(fastifyView, { engine: {ejs}, root: viewsPath }); -app.register(fastifyMarkdown, { - src: false, -}); - -app.register(fastifyStatic, { - root: staticsPath, -}); +app.register(fastifyStatic, { root: staticsPath }); app.get('/', (req, reply) => { - const markdown = reply.markdown().parse(homepageMD); - reply.view('index.ejs', {markdown}); + reply.view('index.ejs', {markdown: homepageHTML}); }); app.get('/invite', (req, reply) => { diff --git a/package.json b/package.json index ff3a262..9ec6f06 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "require": "./lib/index.js" } }, - "type": "commonjs", + "type": "module", "engines": { "node": ">=16" }, @@ -28,7 +28,10 @@ "@fastify/view": "7.4.1", "ejs": "3.1.9", "fastify": "4.17.0", - "fastify-markdown": "0.4.0" + "unified": "10.1.2", + "remark-parse": "10.0.2", + "remark-rehype": "10.1.0", + "rehype-stringify": "9.0.3" }, "devDependencies": { "c8": "7",