Skip to content

Commit

Permalink
feat: add metaformats fallback to fetch-references (#23)
Browse files Browse the repository at this point in the history
* add metaformats fallback to fetch-references
* update microformats-parser

---------

Co-authored-by: Paul Robert Lloyd <paulrobertlloyd@users.noreply.github.com>
Co-authored-by: Aimee Gamble-Milner <12508200+aimee-gm@users.noreply.github.com>
  • Loading branch information
3 people committed Sep 29, 2023
1 parent eb1c25e commit d4f8315
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 10 deletions.
7 changes: 3 additions & 4 deletions lib/fetch-mf2.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import parser from "microformats-parser";
import { mf2 } from "microformats-parser";
import { fetch } from "./fetch.js";

/**
Expand All @@ -15,9 +15,8 @@ export const fetchMf2 = async (url) => {
throw new Error(response.statusText);
}

const mf2 = parser.mf2(body, {
return mf2(body, {
baseUrl: url,
experimental: { metaformats: true },
});

return mf2;
};
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"prepare": "husky install"
},
"dependencies": {
"microformats-parser": "^1.4.0",
"microformats-parser": "^2.0.0",
"undici": "^5.9.0"
},
"devDependencies": {
Expand Down
39 changes: 39 additions & 0 deletions tests/fetch-references.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,42 @@ test("Fetches JF2 properties for each referenced URL", async () => {
expected,
);
});

test("Uses metaformats fallback for each referenced URL", async () => {
const expected = await fetchReferences({
type: "entry",
name: "A cool git repo",
published: "2019-02-12T10:00:00.000+00:00",
category: ["foo", "bar"],
url: "https://website.example/bookmarks/repo",
"bookmark-of": "https://github.com/getindiekit/mf2tojf2",
});
console.log(expected);
assert.deepEqual(
{
type: "entry",
name: "A cool git repo",
published: "2019-02-12T10:00:00.000+00:00",
category: ["foo", "bar"],
url: "https://website.example/bookmarks/repo",
"bookmark-of": "https://github.com/getindiekit/mf2tojf2",
references: {
"https://github.com/getindiekit/mf2tojf2": {
url: "https://github.com/getindiekit/mf2tojf2",
type: "entry",
name: "getindiekit/mf2tojf2: Convert MF2 to JF2.",
summary:
"Convert MF2 to JF2. Contribute to getindiekit/mf2tojf2 development by creating an account on GitHub.",
featured: [
{
alt: "Card identifying indieweb/mf2tojf2 repo. Notes 2 contributors, 6 stars, and 2 forks.",
url: "https://opengraph.githubassets.com/6d3c627723ff987446e2917808142dfb0e2ccdd9d94db970f9a5aacbb1eb4825/getindiekit/mf2tojf2",
},
],
publication: "GitHub",
},
},
},
expected,
);
});
29 changes: 29 additions & 0 deletions tests/fixtures/repo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>getindiekit/mf2tojf2: Convert MF2 to JF2.</title>
<meta name="description" content="Convert MF2 to JF2. Contribute to getindiekit/mf2tojf2 development by creating an account on GitHub.">
<meta name="twitter:image:src" content="https://opengraph.githubassets.com/6d3c627723ff987446e2917808142dfb0e2ccdd9d94db970f9a5aacbb1eb4825/getindiekit/mf2tojf2"/>
<meta name="twitter:site" content="@github"/>
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:title" content="getindiekit/mf2tojf2: Convert MF2 to JF2."/>
<meta name="twitter:description" content="Convert MF2 to JF2. Contribute to getindiekit/mf2tojf2 development by creating an account on GitHub."/>
<meta property="og:image" content="https://opengraph.githubassets.com/6d3c627723ff987446e2917808142dfb0e2ccdd9d94db970f9a5aacbb1eb4825/getindiekit/mf2tojf2"/>
<meta property="og:image:alt" content="Card identifying indieweb/mf2tojf2 repo. Notes 2 contributors, 6 stars, and 2 forks."/>
<meta property="og:image:width" content="1200"/>
<meta property="og:image:height" content="600"/>
<meta property="og:site_name" content="GitHub"/>
<meta property="og:type" content="object"/>
<meta property="og:title" content="getindiekit/mf2tojf2: Convert MF2 to JF2."/>
<meta property="og:url" content="https://github.com/getindiekit/mf2tojf2"/>
<meta property="og:description" content="Convert MF2 to JF2. Contribute to getindiekit/mf2tojf2 development by creating an account on GitHub."/>
<link rel="canonical" href="https://github.com/getindiekit/mf2tojf2">
</head>
<body>
<article>
<h1>MF2 to JF2</h1>
<p>JF2 is a simpler JSON serialization of microformats2 intended to be easier to consume than the standard <a href="https://microformats.org/wiki/microformats2" rel="nofollow">microformats JSON representation</a>.</p>
</article>
</body>
</html>
6 changes: 6 additions & 0 deletions tests/helpers/mock-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export const mockAgent = () => {
.intercept({ path: "/notes/lunch" })
.reply(200, getFixture("bookmark.html"));

// Get page without mf2
agent
.get("https://github.com")
.intercept({ path: "/getindiekit/mf2tojf2" })
.reply(200, getFixture("repo.html"));

// Get bookmark (Not Found)
agent
.get("https://website.example")
Expand Down

0 comments on commit d4f8315

Please sign in to comment.