From d486642c4e357101235fd1cd0f8ae1fcfedddd1e Mon Sep 17 00:00:00 2001 From: aimee-gm <12508200+aimee-gm@users.noreply.github.com> Date: Thu, 14 Sep 2023 20:29:36 +0100 Subject: [PATCH 1/5] Require Base URL & handle parser errors --- index.js | 22 ++++++--- package.json | 4 +- views/index.html.ejs | 20 +++++---- yarn.lock | 104 ++++++++++++++++++++++++++----------------- 4 files changed, 91 insertions(+), 59 deletions(-) diff --git a/index.js b/index.js index 26d9c38..83fed45 100644 --- a/index.js +++ b/index.js @@ -6,21 +6,29 @@ const app = express(); const port = process.env.PORT || 9000; function getDependencyVersion(dependencyName) { - const fs = require('fs'); - const lockfile = require('@yarnpkg/lockfile'); + const fs = require("fs"); + const lockfile = require("@yarnpkg/lockfile"); const parsed = lockfile.parse(fs.readFileSync("./yarn.lock", "utf-8")); if (parsed.type !== "success") return "unknown"; - const dependency = parsed.object[`${dependencyName}@${pkg.dependencies[dependencyName]}`]; + const dependency = + parsed.object[`${dependencyName}@${pkg.dependencies[dependencyName]}`]; if (dependency === undefined) return "unknown"; return dependency.version; } const mf2version = getDependencyVersion("microformats-parser"); function htmlToMf2(url, html, res) { - const body = mf2(html, { baseUrl: url }); - res - .header("content-type", "application/json; charset=UTF-8") - .send(JSON.stringify(body, null, 2)); + try { + const body = mf2(html, { baseUrl: url }); + res + .header("content-type", "application/json; charset=UTF-8") + .send(JSON.stringify(body, null, 2)); + } catch (err) { + res + .header("content-type", "application/json; charset=UTF-8") + .status(500) + .send(JSON.stringify({ error: err.message }, null, 2)); + } } app.set("view engine", "ejs"); diff --git a/package.json b/package.json index 24b4ff9..305064f 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "description": "A website written using Node to demonstrate Microformats2 usage", "version": "1.1.0", "engines": { - "node": "17.x", + "node": "18.x", "yarn": "1.x" }, "scripts": { @@ -13,7 +13,7 @@ "@yarnpkg/lockfile": "^1.1.0", "ejs": "^3.1.8", "express": "^4.18.1", - "microformats-parser": "^1.4.1", + "microformats-parser": "^1.5.2", "undici": "^5.6.1" } } diff --git a/views/index.html.ejs b/views/index.html.ejs index 2fd7709..511a013 100644 --- a/views/index.html.ejs +++ b/views/index.html.ejs @@ -53,9 +53,7 @@
-

- Microformats Parser (Node) <%- version -%> -

+

Microformats Parser (Node) <%- version -%>

@@ -92,20 +90,26 @@ name="url" type="url" class="form-control form-control-lg" + required />
-
+

- Drag this link to your bookmarks toolbar to parse a page with one click!
+ Drag this link to your bookmarks toolbar to parse a page with one + click!

- mf2 parser - -
+ mf2 parser + +