Skip to content

Commit b280391

Browse files
committed
Merge remote-tracking branch 'origin/main' into add-readme
2 parents df482ab + bb689ba commit b280391

4 files changed

Lines changed: 50 additions & 248 deletions

File tree

index.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
const express = require("express");
2-
const mf2 = require("microformat-node");
2+
const { mf2 } = require("microformats-parser");
33
const undici = require("undici");
4-
const querystring = require("querystring");
54
const pkg = require("./package.json");
65
const app = express();
76
const port = process.env.PORT || 9000;
87

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

1826
app.set("view engine", "ejs");
@@ -31,13 +39,12 @@ app.get("/", async (req, res) => {
3139
htmlToMf2(url, text, res);
3240
} else {
3341
res.render("index.html.ejs", {
34-
version: `${pkg.version} (lib: ${mf2.version})`,
42+
version: `${pkg.version} (lib: ${mf2version})`,
3543
});
3644
}
3745
});
38-
app.post("/", (req, res) => {
39-
const qsBody = querystring.parse(req.body);
40-
htmlToMf2(qsBody.url, qsBody.html, res);
46+
app.post("/", express.urlencoded({ extended: false }), (req, res) => {
47+
htmlToMf2(req.body.url, req.body.html, res);
4148
});
4249

4350
app.listen(port, () => {

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "microformats-website-node",
33
"description": "A website written using Node to demonstrate Microformats2 usage",
4-
"version": "1.0.4",
4+
"version": "1.1.0",
55
"engines": {
66
"node": "17.x",
77
"yarn": "1.x"
@@ -10,10 +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-
"querystring": "^0.2.1",
16+
"microformats-parser": "^1.4.1",
1717
"undici": "^5.6.1"
1818
}
1919
}

views/index.html.ejs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@
9898
<button type="submit" class="btn btn-lg btn-success">Parse</button>
9999
</form>
100100

101+
<hr>
102+
103+
<p>
104+
Drag this link to your bookmarks toolbar to parse a page with one click!<br>
105+
</p>
106+
<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>
107+
108+
<hr>
109+
101110
<footer class="my-5">
102111
<ul>
103112
<li><a href="https://microformats.io">About Microformats</a></li>
@@ -108,8 +117,8 @@
108117
>
109118
</li>
110119
<li>
111-
<a href="https://github.com/microformats/microformat-node"
112-
>Source code for the Microformats Node Parser</a
120+
<a href="https://github.com/microformats/microformats-parser"
121+
>Source code for the Microformats JavaScript Parser</a
113122
>
114123
</li>
115124

0 commit comments

Comments
 (0)