Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require Base URL & handle parser errors #10

Open
wants to merge 6 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
18
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down Expand Up @@ -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
http://creativecommons.org/publicdomain/zero/1.0
31 changes: 19 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,49 @@
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");
aimee-gm marked this conversation as resolved.
Show resolved Hide resolved
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");
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})`,
Expand Down
15 changes: 11 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
20 changes: 12 additions & 8 deletions views/index.html.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@

<body>
<main id="mf2" class="container">
<h1 class="mt-5 mb-3">
Microformats Parser (Node) <%- version -%>
</h1>
<h1 class="mt-5 mb-3">Microformats Parser (Node) <%- version -%></h1>

<form action="/" accept-charset="UTF-8" method="get">
<div class="form-group">
Expand Down Expand Up @@ -92,20 +90,26 @@
name="url"
type="url"
class="form-control form-control-lg"
required
/>
</div>

<button type="submit" class="btn btn-lg btn-success">Parse</button>
</form>

<hr>
<hr />
aimee-gm marked this conversation as resolved.
Show resolved Hide resolved

<p>
Drag this link to your bookmarks toolbar to parse a page with one click!<br>
Drag this link to your bookmarks toolbar to parse a page with one
click!<br />
</p>
<a class="btn btn-primary btn-sm" href="javascript:(function(){document.location.href='https://node.microformats.io/?url='+encodeURIComponent(document.location.href);}())">mf2 parser</a>

<hr>
<a
class="btn btn-primary btn-sm"
href="javascript:(function(){document.location.href='https://node.microformats.io/?url='+encodeURIComponent(document.location.href);}())"
>mf2 parser</a
>

<hr />

<footer class="my-5">
<ul>
Expand Down
Loading