Skip to content

Commit 746cfc6

Browse files
authored
Merge pull request #5 from Zegnat/feature/switch-parser
Swap microformat-node for newer microformats-parser
2 parents e713ddc + 6a64625 commit 746cfc6

4 files changed

Lines changed: 38 additions & 237 deletions

File tree

index.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
const express = require("express");
2-
const mf2 = require("microformat-node");
2+
const { mf2 } = require("microformats-parser");
33
const undici = require("undici");
44
const querystring = require("querystring");
55
const pkg = require("./package.json");
66
const app = express();
77
const port = process.env.PORT || 9000;
88

9+
function getDependencyVersion(dependencyName) {
10+
const fs = require('fs');
11+
const lockfile = require('@yarnpkg/lockfile');
12+
const parsed = lockfile.parse(fs.readFileSync("./yarn.lock", "utf-8"));
13+
if (parsed.type !== "success") return "unknown";
14+
const dependency = parsed.object[`${dependencyName}@${pkg.dependencies[dependencyName]}`];
15+
if (dependency === undefined) return "unknown";
16+
return dependency.version;
17+
}
18+
const mf2version = getDependencyVersion("microformats-parser");
19+
920
function htmlToMf2(url, html, res) {
10-
mf2.get({ baseUrl: url, html }, (err, data) => {
11-
const body = err || data;
12-
res
13-
.header("content-type", "application/json; charset=UTF-8")
14-
.send(JSON.stringify(body, null, 2));
15-
});
21+
const body = mf2(html, { baseUrl: url });
22+
res
23+
.header("content-type", "application/json; charset=UTF-8")
24+
.send(JSON.stringify(body, null, 2));
1625
}
1726

1827
app.set("view engine", "ejs");
@@ -31,7 +40,7 @@ app.get("/", async (req, res) => {
3140
htmlToMf2(url, text, res);
3241
} else {
3342
res.render("index.html.ejs", {
34-
version: `${pkg.version} (lib: ${mf2.version})`,
43+
version: `${pkg.version} (lib: ${mf2version})`,
3544
});
3645
}
3746
});

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
"start": "node index.js"
1111
},
1212
"dependencies": {
13+
"@yarnpkg/lockfile": "^1.1.0",
1314
"ejs": "^3.1.8",
1415
"express": "^4.18.1",
15-
"microformat-node": "^2.0.1",
16+
"microformats-parser": "^1.4.1",
1617
"querystring": "^0.2.1",
1718
"undici": "^5.6.1"
1819
}

views/index.html.ejs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@
108108
>
109109
</li>
110110
<li>
111-
<a href="https://github.com/microformats/microformat-node"
112-
>Source code for the Microformats Node Parser</a
111+
<a href="https://github.com/microformats/microformats-parser"
112+
>Source code for the Microformats JavaScript Parser</a
113113
>
114114
</li>
115115

0 commit comments

Comments
 (0)