-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(experimental): authorship #292
Draft
paulrobertlloyd
wants to merge
5
commits into
microformats:main
Choose a base branch
from
getindiekit:authorship
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e635f4f
docs: fix typo in parse.ts comment
paulrobertlloyd bc9d8d3
feat(experimental): add support for authorship (option flag)
paulrobertlloyd d87355c
feat(experimental): add support for authorship (demo option)
paulrobertlloyd c01370d
feat(experimental): add support for authorship (test suite)
paulrobertlloyd d190378
feat(experimental): add support for authorship (wip)
paulrobertlloyd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { | ||
Author, | ||
Image, | ||
MicroformatProperty, | ||
MicroformatRoot, | ||
Rels, | ||
} from "../types"; | ||
|
||
function getPlainText(values: MicroformatProperty[]): string | null { | ||
if (values.length === 0) { | ||
return null; | ||
} | ||
|
||
const value = values[0] as Author; | ||
let plainText: string | null; | ||
if (value.value !== undefined && typeof value.value === "string") { | ||
plainText = value.value; | ||
} else if (typeof value === "string") { | ||
plainText = value; | ||
} else { | ||
plainText = null; | ||
} | ||
|
||
return plainText && plainText.trim(); | ||
} | ||
|
||
const parseAuthor = (hCard: MicroformatRoot) => { | ||
// TODO: Figure out how to stop TypeScript complaining about missing `value` | ||
const result: Author = {}; | ||
|
||
if (hCard.properties !== undefined) { | ||
// Use first (or only) name | ||
const names = hCard.properties.name as string[]; | ||
if (names?.length > 0) { | ||
result.name = names[0]; | ||
} | ||
|
||
// Use first (or only) photo | ||
const photos = hCard.properties.photo as Image[]; | ||
if (photos?.length > 0) { | ||
const photo = getPlainText(photos); | ||
if (photo) { | ||
result.photo = photo; | ||
} | ||
} | ||
|
||
// Use first (or only) URL | ||
const urls = hCard.properties.url as string[]; | ||
if (urls?.length > 0) { | ||
result.url = urls[0]; | ||
} | ||
} else if (hCard) { | ||
if (URL.canParse(String(hCard))) { | ||
result.url = String(hCard); | ||
} else { | ||
result.name = String(hCard); | ||
} | ||
} | ||
|
||
return result as Author; | ||
}; | ||
|
||
const findEntryAuthor = (hEntry: MicroformatRoot) => { | ||
const values = hEntry.properties.author || []; | ||
|
||
if (Object.keys(values).length === 0) { | ||
return; | ||
} | ||
|
||
return parseAuthor(values[0] as MicroformatRoot); | ||
}; | ||
|
||
const findFeedAuthor = () => false; | ||
|
||
export const findAuthor = async (item: MicroformatRoot, rels: Rels) => { | ||
// 1. If no `h-entry` then there’s no post to find authorship for. | ||
const itemIsEntry = item.type && item.type[0] === "h-entry"; | ||
if (!itemIsEntry) { | ||
return false; | ||
} | ||
|
||
// 2. Parse the `h-entry` | ||
const entryAuthor = findEntryAuthor(item); | ||
const feedAuthor = findFeedAuthor(); // TODO | ||
|
||
// 3 & 4. Return author in `h-entry`, else find author in parent `h-feed` | ||
const author = entryAuthor ? entryAuthor : feedAuthor; | ||
|
||
// 5. Return `author` if `h-card` | ||
const authorIsCard = author && author.type[0] === "h-card"; | ||
if (authorIsCard) { | ||
return author; | ||
} | ||
|
||
// 6. Use `h-card` fetched from rel=author | ||
const authorPage = author.properties?.url || rels.author; | ||
if (authorPage) { | ||
// Fetch `authorPage` and parse result using `parseMicroformat` | ||
// This is an async function, which would bubble up to the parent function | ||
} | ||
|
||
// 7. From the parsed `authorPage`, return the first `h-card` that either: | ||
// * Has a value for `u-url` (or `u-uid`) that matches the `authorPage` URL | ||
// * Has a value for `u-url` that matches a `rel=me` on the `authorPage` | ||
|
||
// 8. Else, no deterministic author can be found | ||
return author; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
test/suites/experimental/authorship-h-card-with-rel-author-to-rel-me.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!-- This next entry is by Virginia Woolf. Her URL is https://virginia.example, and her photo is at https://virginia.example/photo.jpg. --> | ||
|
||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>Entry with rel=author (links to h-card with rel=me)</title> | ||
</head> | ||
<body class="h-entry"> | ||
<div class="e-content"> | ||
<p>A woman must have money and a room of her own if she is to write fiction.</p> | ||
</div> | ||
<footer> | ||
<a href="https://virginia.woolf" rel="author">About Virginia Woolf</a> | ||
</footer> | ||
</body> | ||
</html> |
38 changes: 38 additions & 0 deletions
38
test/suites/experimental/authorship-h-card-with-rel-author-to-rel-me.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"items": [ | ||
{ | ||
"type": ["h-entry"], | ||
"lang": "en", | ||
"properties": { | ||
"author": [ | ||
{ | ||
"type": ["h-card"], | ||
"lang": "en", | ||
"properties": { | ||
"name": ["Virginia Woolf"], | ||
"photo": ["https://virginia.example/photo.jpg"], | ||
"url": ["https://virginia.example"] | ||
}, | ||
"value": "Virginia Woolf" | ||
} | ||
], | ||
"content": [ | ||
{ | ||
"value": "A woman must have money and a room of her own if she is to write fiction.", | ||
"lang": "en", | ||
"html": "<p>A woman must have money and a room of her own if she is to write fiction.</p>" | ||
} | ||
] | ||
} | ||
} | ||
], | ||
"rel-urls": { | ||
"https://virginia.woolf": { | ||
"rels": ["author"], | ||
"text": "About Virginia Woolf" | ||
} | ||
}, | ||
"rels": { | ||
"author": ["https://virginia.woolf"] | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
test/suites/experimental/authorship-h-card-with-rel-author-to-u-url.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!-- This is a haiku by Basho. His URL is https://basho.example, and his photo is at https://basho.example/photo.jpg. --> | ||
|
||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>Entry with rel=author (links to h-card with u-url and u-uid)</title> | ||
</head> | ||
<body class="h-entry"> | ||
<div class="e-content" lang="jp"> | ||
<p>古池や<br />蛙飛び込む<br />水の音</p> | ||
</div> | ||
<footer> | ||
<a href="https://basho.example" rel="author">About Basho</a> | ||
</footer> | ||
</body> | ||
</html> |
38 changes: 38 additions & 0 deletions
38
test/suites/experimental/authorship-h-card-with-rel-author-to-u-url.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"items": [ | ||
{ | ||
"type": ["h-entry"], | ||
"lang": "en", | ||
"properties": { | ||
"author": [ | ||
{ | ||
"type": ["h-card"], | ||
"lang": "en", | ||
"properties": { | ||
"name": ["Basho"], | ||
"photo": ["https://basho.example/photo.jpg"], | ||
"url": ["https://basho.example"] | ||
}, | ||
"value": "Basho" | ||
} | ||
], | ||
"content": [ | ||
{ | ||
"value": "古池や\n蛙飛び込む\n水の音", | ||
"lang": "jp", | ||
"html": "<p>古池や<br>蛙飛び込む<br>水の音</p>" | ||
} | ||
] | ||
} | ||
} | ||
], | ||
"rel-urls": { | ||
"https://basho.example": { | ||
"rels": ["author"], | ||
"text": "About Basho" | ||
} | ||
}, | ||
"rels": { | ||
"author": ["https://basho.example"] | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
test/suites/experimental/authorship-h-card-with-rel-author.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<!-- This is an entry by Patañjali. His URL is https://patanjali.example, and he uses no photo. --> | ||
|
||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>Entry with separate h-card and rel=author</title> | ||
</head> | ||
<body class="h-entry"> | ||
<div class="e-content"> | ||
<p>For one who sees the distinction, there is no further confusing of the mind with the self.</p> | ||
</div> | ||
<p class="p-author h-card"> | ||
<a href="https://patanjali.example" class="u-url"> | ||
<span class="p-name">Patañjali</span> | ||
</a> | ||
</p> | ||
<footer> | ||
<a href="https://patanjali.example" rel="author">About Patañjali</a> | ||
</footer> | ||
</body> | ||
</html> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure we can use
URL.canParse()
- see https://developer.mozilla.org/en-US/docs/Web/API/URL/canParse_static#browser_compatibilityThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, think I confused myself with another Node-only project supporting Node v20. I’ll create a utility function that achieves similar.