diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..a557beb --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,20 @@ +name: Test + +on: + push: + branches: + - main + pull_request: + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v3 + with: + node-version-file: ".nvmrc" + - name: Install dependencies + run: yarn + - name: Run prettier list + run: yarn prettier:list diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 0000000..25bf17f --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +18 \ No newline at end of file diff --git a/README.md b/README.md index 5e6c77b..92b57c1 100644 --- a/README.md +++ b/README.md @@ -13,10 +13,11 @@ All commits to the `main` branch get auto-deployed to the live website [running The base branch is `next`. All PRs should target this branch. To initiate a release: -* Create a new branch, from `next` named `release/x.y.z` -* Bump the version in `package.json` to `x.y.z` -* Merge `release/x.y.z` to `main`. This will trigger the [deployment](#deployment). -* Merge `main` into `next` + +- Create a new branch, from `next` named `release/x.y.z` +- Bump the version in `package.json` to `x.y.z` +- Merge `release/x.y.z` to `main`. This will trigger the [deployment](#deployment). +- Merge `main` into `next` ## Getting Started @@ -35,7 +36,7 @@ Next, install the required dependencies and start the server: yarn install yarn start # Or, if you'd like a different port: -# PORT=5000 yarn start +# PORT=5000 yarn start ``` You can view your running local application at this URL: @@ -65,4 +66,4 @@ If you find bugs, have feature requests or questions, please Microformats Parser Website Node is dedicated to the public domain using Creative Commons -- CC0 1.0 Universal. -http://creativecommons.org/publicdomain/zero/1.0 \ No newline at end of file +http://creativecommons.org/publicdomain/zero/1.0 diff --git a/index.js b/index.js index 26d9c38..f909106 100644 --- a/index.js +++ b/index.js @@ -1,26 +1,33 @@ const express = require("express"); const { mf2 } = require("microformats-parser"); -const undici = require("undici"); const pkg = require("./package.json"); 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"); @@ -28,15 +35,15 @@ app.use(express.static("public")); app.get("/", async (req, res) => { if (req.query.url) { const url = req.query.url; - const { body } = await undici.request(url, { - maxRedirections: 2, + await fetch(url, { headers: { accept: "text/html, text/mf2+html", }, method: "GET", + }).then(async (response) => { + const html = await response.text(); + htmlToMf2(url, html, res); }); - const text = await body.text(); - htmlToMf2(url, text, res); } else { res.render("index.html.ejs", { version: `${pkg.version} (lib: ${mf2version})`, diff --git a/package.json b/package.json index 296d47e..fa52f21 100644 --- a/package.json +++ b/package.json @@ -4,17 +4,24 @@ "version": "1.1.0", "license": "CC0-1.0", "engines": { - "node": "17.x", + "node": "18.x", "yarn": "1.x" }, "scripts": { - "start": "node index.js" + "start": "node index.js", + "prettier:list": "prettier '**/*.{js,ts,json,md,html}' --list-different", + "prettier:fix": "prettier '**/*.{js,ts,json,md,html}' --write" }, "dependencies": { "@yarnpkg/lockfile": "^1.1.0", "ejs": "^3.1.8", "express": "^4.18.1", - "microformats-parser": "^1.4.1", - "undici": "^5.6.1" + "microformats-parser": "^1.5.2" + }, + "devDependencies": { + "prettier": "^3.0.3" + }, + "lint-staged": { + "*.{js,css,md}": "prettier --write" } } 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 + +