Skip to content

Commit

Permalink
feat: add open graph article type (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
fuji44 committed Aug 1, 2021
1 parent 5d367d8 commit 8945de7
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 4 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ type Metadata = {
width?: number
tags?: string[]
}[]
article: {
published_time?: string
modified_time?: string
expiration_time?: string
author?: string
section?: string
tags?: string[]
}
}
}
```
Expand Down
19 changes: 15 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ function parse (ctx) {
return function (metadata) {
const parsed: any = {}

let tags = []
let ogVideoTags = []
let articleTags = []
let lastParent

for (let [metaKey, metaValue] of metadata) {
Expand All @@ -338,7 +339,11 @@ function parse (ctx) {

// special case for video tags which we want to map to each video object
if (metaKey === 'og:video:tag') {
tags.push(metaValue)
ogVideoTags.push(metaValue)
continue
}
if (metaKey === 'article:tag') {
articleTags.push(metaValue)
continue
}

Expand Down Expand Up @@ -389,10 +394,16 @@ function parse (ctx) {
target[item.name] || (target[item.name] = metaValue)
}

if (tags.length && parsed.open_graph.videos) {
if (ogVideoTags.length && parsed.open_graph.videos) {
parsed.open_graph.videos = parsed.open_graph.videos.map(obj => ({
...obj,
tags
tags: ogVideoTags
}))
}
if (articleTags.length && parsed.open_graph.articles) {
parsed.open_graph.articles = parsed.open_graph.articles.map(obj => ({
...obj,
tags: articleTags
}))
}

Expand Down
6 changes: 6 additions & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ export const schema = new Map([
['og:video:height', { entry: 'open_graph', name: 'height', parent: 'videos', type: 'number' }],
['og:video:type', { entry: 'open_graph', name: 'type', parent: 'videos', type: 'string' }],
['og:video:tag', { entry: 'open_graph', name: 'tag', parent: 'videos', type: 'string' }],
['article:published_time', { entry: 'open_graph', name: 'published_time', parent: 'articles', type: 'string' }],
['article:modified_time', { entry: 'open_graph', name: 'modified_time', parent: 'articles', type: 'string' }],
['article:expiration_time', { entry: 'open_graph', name: 'expiration_time', parent: 'articles', type: 'string' }],
['article:author', { entry: 'open_graph', name: 'author', parent: 'articles', type: 'string' }],
['article:section', { entry: 'open_graph', name: 'section', parent: 'articles', type: 'string' }],
['article:tag', { entry: 'open_graph', name: 'tag', parent: 'articles', type: 'string' }],
// oEmbed: is prepended to these fields so that it does not interfere with root title or description
['oEmbed:type', { entry: 'oEmbed', name: 'type', type: 'string' }],
['oEmbed:version', { entry: 'oEmbed', name: 'version', type: 'string' }],
Expand Down
8 changes: 8 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,13 @@ export type Metadata = {
width?: number
tags?: string[]
}[]
article: {
published_time?: string
modified_time?: string
expiration_time?: string
author?: string
section?: string
tags?: string[]
}
}
}
16 changes: 16 additions & 0 deletions test/open_graph/article.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta property="og:type" content="article">
<meta property="article:published_time" content="2021-07-20T05:30:22+00:00">
<meta property="article:modified_time" content="2021-07-20T06:30:22+00:00">
<meta property="article:expiration_time" content="2021-08-20T05:30:22+00:00">
<meta property="article:author" content="abc">
<meta property="article:section" content="def">
<meta property="article:tag" content="a">
<meta property="article:tag" content="b">
<meta property="article:tag" content="c">
</head>
<body></body>
</html>
29 changes: 29 additions & 0 deletions test/open_graph/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,32 @@ test('should quality relative urls', async () => {

expect(result.open_graph).toEqual(expected)
})

test('should build article[]', async () => {
nock('http://localhost')
.get('/open_graph/article')
.replyWithFile(200, __dirname + '/article.html', {
'Content-Type': 'text/html'
})

const result = await unfurl('http://localhost/open_graph/article')
const expected = {
type: "article",
articles: [
{
published_time: "2021-07-20T05:30:22+00:00",
modified_time: "2021-07-20T06:30:22+00:00",
expiration_time: "2021-08-20T05:30:22+00:00",
author: "abc",
section: "def",
tags: [
"a",
"b",
"c"
]
}
]
}

expect(result.open_graph).toEqual(expected)
})

0 comments on commit 8945de7

Please sign in to comment.