@@ -6,6 +6,18 @@ import {
6
6
Rels ,
7
7
} from "../types" ;
8
8
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
+
9
21
function getPlainText ( values : MicroformatProperty [ ] ) : string | null {
10
22
if ( values . length === 0 ) {
11
23
return null ;
@@ -24,11 +36,10 @@ function getPlainText(values: MicroformatProperty[]): string | null {
24
36
return plainText && plainText . trim ( ) ;
25
37
}
26
38
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 = { } ;
30
41
31
- if ( hCard . properties !== undefined ) {
42
+ if ( hCard . properties ) {
32
43
// Use first (or only) name
33
44
const names = hCard . properties . name as string [ ] ;
34
45
if ( names ?. length > 0 ) {
@@ -57,25 +68,29 @@ const parseAuthor = (hCard: MicroformatRoot) => {
57
68
}
58
69
}
59
70
60
- return result as Author ;
71
+ if ( isAuthor ( result ) ) {
72
+ return result ;
73
+ }
74
+
75
+ return undefined ;
61
76
} ;
62
77
63
- const findEntryAuthor = ( hEntry : MicroformatRoot ) => {
64
- const values = hEntry . properties . author || [ ] ;
78
+ const findEntryAuthor = ( hEntry : MicroformatRoot ) : Author | undefined => {
79
+ const [ value ] = hEntry . properties . author || [ ] ;
65
80
66
- if ( Object . keys ( values ) . length === 0 ) {
67
- return ;
81
+ if ( ! isMicroformatRoot ( value ) ) {
82
+ return undefined ;
68
83
}
69
84
70
- return parseAuthor ( values [ 0 ] as MicroformatRoot ) ;
85
+ return parseAuthor ( value ) ;
71
86
} ;
72
87
73
- const findFeedAuthor = ( ) => false ;
88
+ const findFeedAuthor = ( ) => undefined ;
74
89
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 ) => {
76
92
// 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" ) {
79
94
return false ;
80
95
}
81
96
@@ -84,20 +99,20 @@ export const findAuthor = async (item: MicroformatRoot, rels: Rels) => {
84
99
const feedAuthor = findFeedAuthor ( ) ; // TODO
85
100
86
101
// 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 ;
88
103
89
104
// 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
+ // }
94
109
95
110
// 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
+ // }
101
116
102
117
// 7. From the parsed `authorPage`, return the first `h-card` that either:
103
118
// * Has a value for `u-url` (or `u-uid`) that matches the `authorPage` URL
0 commit comments