Skip to content

Commit 50aa892

Browse files
committed
Get tests working
1 parent d190378 commit 50aa892

10 files changed

+59
-34
lines changed

src/helpers/authorship.ts

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ import {
66
Rels,
77
} from "../types";
88

9+
type DraftAuthor = Partial<Author>;
10+
11+
function isAuthor(value: DraftAuthor): value is Author {
12+
return value.value !== undefined;
13+
}
14+
15+
function isMicroformatRoot(
16+
value: MicroformatProperty,
17+
): value is MicroformatRoot {
18+
return typeof value !== "object" || !("id" in value);
19+
}
20+
921
function getPlainText(values: MicroformatProperty[]): string | null {
1022
if (values.length === 0) {
1123
return null;
@@ -24,11 +36,10 @@ function getPlainText(values: MicroformatProperty[]): string | null {
2436
return plainText && plainText.trim();
2537
}
2638

27-
const parseAuthor = (hCard: MicroformatRoot) => {
28-
// TODO: Figure out how to stop TypeScript complaining about missing `value`
29-
const result: Author = {};
39+
const parseAuthor = (hCard: MicroformatRoot): Author | undefined => {
40+
const result: DraftAuthor = {};
3041

31-
if (hCard.properties !== undefined) {
42+
if (hCard.properties) {
3243
// Use first (or only) name
3344
const names = hCard.properties.name as string[];
3445
if (names?.length > 0) {
@@ -57,25 +68,29 @@ const parseAuthor = (hCard: MicroformatRoot) => {
5768
}
5869
}
5970

60-
return result as Author;
71+
if (isAuthor(result)) {
72+
return result;
73+
}
74+
75+
return undefined;
6176
};
6277

63-
const findEntryAuthor = (hEntry: MicroformatRoot) => {
64-
const values = hEntry.properties.author || [];
78+
const findEntryAuthor = (hEntry: MicroformatRoot): Author | undefined => {
79+
const [value] = hEntry.properties.author || [];
6580

66-
if (Object.keys(values).length === 0) {
67-
return;
81+
if (!isMicroformatRoot(value)) {
82+
return undefined;
6883
}
6984

70-
return parseAuthor(values[0] as MicroformatRoot);
85+
return parseAuthor(value);
7186
};
7287

73-
const findFeedAuthor = () => false;
88+
const findFeedAuthor = () => undefined;
7489

75-
export const findAuthor = async (item: MicroformatRoot, rels: Rels) => {
90+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
91+
export const findAuthor = (item: MicroformatRoot, _rels: Rels) => {
7692
// 1. If no `h-entry` then there’s no post to find authorship for.
77-
const itemIsEntry = item.type && item.type[0] === "h-entry";
78-
if (!itemIsEntry) {
93+
if (item.type?.[0] !== "h-entry") {
7994
return false;
8095
}
8196

@@ -84,20 +99,20 @@ export const findAuthor = async (item: MicroformatRoot, rels: Rels) => {
8499
const feedAuthor = findFeedAuthor(); // TODO
85100

86101
// 3 & 4. Return author in `h-entry`, else find author in parent `h-feed`
87-
const author = entryAuthor ? entryAuthor : feedAuthor;
102+
const author = entryAuthor || feedAuthor;
88103

89104
// 5. Return `author` if `h-card`
90-
const authorIsCard = author && author.type[0] === "h-card";
91-
if (authorIsCard) {
92-
return author;
93-
}
105+
// const authorIsCard = author && author.type[0] === "h-card";
106+
// if (authorIsCard) {
107+
// return author;
108+
// }
94109

95110
// 6. Use `h-card` fetched from rel=author
96-
const authorPage = author.properties?.url || rels.author;
97-
if (authorPage) {
98-
// Fetch `authorPage` and parse result using `parseMicroformat`
99-
// This is an async function, which would bubble up to the parent function
100-
}
111+
// const authorPage = author.properties?.url || rels.author;
112+
// if (authorPage) {
113+
// Fetch `authorPage` and parse result using `parseMicroformat`
114+
// This is an async function, which would bubble up to the parent function
115+
// }
101116

102117
// 7. From the parsed `authorPage`, return the first `h-card` that either:
103118
// * Has a value for `u-url` (or `u-uid`) that matches the `authorPage` URL

src/microformats/parse.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ export const parseMicroformat = (
7171
const author = findAuthor(item, options.rels);
7272

7373
if (author) {
74-
console.log("author", author);
75-
// item.properties.author = author;
74+
item.properties.author = [author];
7675
}
7776
}
7877

test/suites/experimental/authorship-h-card-with-rel-author-to-rel-me.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
</head>
88
<body class="h-entry">
99
<div class="e-content">
10-
<p>A woman must have money and a room of her own if she is to write fiction.</p>
10+
<p>
11+
A woman must have money and a room of her own if she is to write
12+
fiction.
13+
</p>
1114
</div>
1215
<footer>
1316
<a href="https://virginia.woolf" rel="author">About Virginia Woolf</a>

test/suites/experimental/authorship-h-card-with-rel-author-to-rel-me.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
{
2121
"value": "A woman must have money and a room of her own if she is to write fiction.",
2222
"lang": "en",
23-
"html": "<p>A woman must have money and a room of her own if she is to write fiction.</p>"
23+
"html": "<p>\n A woman must have money and a room of her own if she is to write\n fiction.\n </p>"
2424
}
2525
]
2626
}

test/suites/experimental/authorship-h-card-with-rel-author.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
</head>
88
<body class="h-entry">
99
<div class="e-content">
10-
<p>For one who sees the distinction, there is no further confusing of the mind with the self.</p>
10+
<p>
11+
For one who sees the distinction, there is no further confusing of the
12+
mind with the self.
13+
</p>
1114
</div>
1215
<p class="p-author h-card">
1316
<a href="https://patanjali.example" class="u-url">

test/suites/experimental/authorship-h-card-with-rel-author.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
{
2020
"value": "For one who sees the distinction, there is no further confusing of the mind with the self.",
2121
"lang": "en",
22-
"html": "<p>For one who sees the distinction, there is no further confusing of the mind with the self.</p>"
22+
"html": "<p>\n For one who sees the distinction, there is no further confusing of the\n mind with the self.\n </p>"
2323
}
2424
]
2525
}

test/suites/experimental/authorship-h-card.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
</head>
88
<body class="h-entry">
99
<div class="e-content">
10-
<p>Even in the house of Hades there is left something, a soul and an image, but there is no real heart of life in it.</p>
10+
<p>
11+
Even in the house of Hades there is left something, a soul and an image,
12+
but there is no real heart of life in it.
13+
</p>
1114
</div>
1215
<footer class="p-author h-card">
1316
<a href="https://homer.example" class="u-url">

test/suites/experimental/authorship-h-card.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
{
2121
"value": "Even in the house of Hades there is left something, a soul and an image, but there is no real heart of life in it.",
2222
"lang": "en",
23-
"html": "<p>Even in the house of Hades there is left something, a soul and an image, but there is no real heart of life in it.</p>"
23+
"html": "<p>\n Even in the house of Hades there is left something, a soul and an image,\n but there is no real heart of life in it.\n </p>"
2424
}
2525
]
2626
}

test/suites/experimental/authorship-h-feed.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
</article>
1414
<article class="h-entry">
1515
<div class="e-content">
16-
<p>Whosoever is delighted in solitude is either a wild beast or a god.</p>
16+
<p>
17+
Whosoever is delighted in solitude is either a wild beast or a god.
18+
</p>
1719
</div>
1820
</article>
1921
<footer class="p-author h-card">

test/suites/experimental/authorship-h-feed.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
],
6262
"content": [
6363
{
64-
"html": "<p>Whosoever is delighted in solitude is either a wild beast or a god.</p>",
64+
"html": "<p>\n Whosoever is delighted in solitude is either a wild beast or a god.\n </p>",
6565
"lang": "en",
6666
"value": "Whosoever is delighted in solitude is either a wild beast or a god."
6767
}

0 commit comments

Comments
 (0)