Skip to content

Commit 9d02f2e

Browse files
committed
Restructure open graph metadata
1 parent 8b1935b commit 9d02f2e

File tree

4 files changed

+67
-46
lines changed

4 files changed

+67
-46
lines changed

app.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -331,33 +331,27 @@ aws.initConfig(awsProps)
331331
return options;
332332
}
333333

334-
function getEditorCode(config) {
335-
const editors = _.chain(config.content)
336-
.pluck("content")
337-
.reduce()
338-
.pluck("content")
339-
.map(stack => _.reduce(stack))
340-
.filter(component => component.componentName === 'codeEditor')
341-
.value();
342-
if (editors && editors.length === 1) {
343-
const state = editors[0].componentState;
344-
return {source: state.source, lang: languages[state.lang].name};
345-
} else {
346-
return null;
347-
}
348-
}
349-
350334
function storedStateHandler(req, res, next) {
351335
const id = req.params.id;
352336
storageHandler.expandId(id)
353337
.then(result => {
354338
const config = JSON.parse(result.config);
355339
const metadata = {
356-
ogDescription: result.metadata ? result.metadata.description.S : null,
357-
ogAuthor: result.metadata ? result.metadata.author.S : null,
358-
ogTitle: result.metadata ? result.metadata.title.S : null,
359-
code: getEditorCode(config)
340+
ogDescription: result.special_metadata ? result.special_metadata.description.S : null,
341+
ogAuthor: result.special_metadata ? result.special_metadata.author.S : null,
342+
ogTitle: result.special_metadata ? result.special_metadata.title.S : "Compiler Explorer"
360343
};
344+
if (!metadata.ogDescription) {
345+
if (result.metadata && result.metadata.single_code && result.metadata.single_code.M) {
346+
const lang = languages[result.metadata.single_code.M.language.S];
347+
metadata.ogDescription = result.metadata.single_code.M.source.S;
348+
if (lang) {
349+
metadata.ogTitle += ` - ${lang.name}`;
350+
}
351+
}
352+
} else if (metadata.ogAuthor && metadata.ogAuthor !== '.') {
353+
metadata.ogDescription += `\nAuthor(s): ${metadata.ogAuthor}`;
354+
}
361355
staticHeaders(res);
362356
contentPolicyHeader(res);
363357
res.render('index', renderConfig({

lib/storage/storage-s3.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ class StorageS3 extends StorageBase {
4747
this.dynamoDb = new AWS.DynamoDB();
4848
}
4949

50+
getSources(content) {
51+
let sources = [];
52+
_.each(content, element => {
53+
if (element.type === 'component') {
54+
if (element.componentName === 'codeEditor') {
55+
sources.push({source: element.componentState.source, language: element.componentState.lang});
56+
}
57+
} else {
58+
sources = this.getSources(element.content).concat(sources);
59+
}
60+
});
61+
return sources;
62+
}
63+
5064
storeItem(item, req) {
5165
logger.info(`Storing item ${item.prefix}`);
5266
const now = new Date();
@@ -56,6 +70,26 @@ class StorageS3 extends StorageBase {
5670
// Anonymize only client IP
5771
ip = `${anonymizeIp(ip.substring(0, commaIndex))}${ip.substring(commaIndex, ip.length)}`;
5872
}
73+
const sources = this.getSources(JSON.parse(item.config).content);
74+
const metadata = {
75+
M: {
76+
editor_count: {
77+
N: sources.length.toString()
78+
}
79+
}
80+
};
81+
if (sources.length === 1) {
82+
metadata.M.single_code = {
83+
M: {
84+
source: {
85+
S: sources[0].source
86+
},
87+
language: {
88+
S: sources[0].language
89+
}
90+
}
91+
};
92+
}
5993
now.setSeconds(0, 0);
6094
return Promise.all([
6195
this.dynamoDb.putItem({
@@ -75,13 +109,13 @@ class StorageS3 extends StorageBase {
75109
clicks: {N: '0'}
76110
}
77111
},
112+
metadata: metadata,
78113
creation_ip: {
79114
S: ip
80115
},
81116
creation_date: {
82117
S: now.toISOString()
83118
}
84-
85119
}
86120
}).promise(),
87121
this.s3.put(item.fullHash, item.config, this.prefix, {})
@@ -157,14 +191,15 @@ class StorageS3 extends StorageBase {
157191
}).promise()
158192
.then(item => {
159193
const full_hash = item.Attributes.full_hash.S;
160-
const attribute_metadata = item.Attributes.named_metadata;
161-
const metadata = attribute_metadata ? attribute_metadata.M : null;
194+
const special_metadata = item.Attributes.named_metadata ? item.Attributes.named_metadata.M : null;
195+
const metadata = item.Attributes.metadata ? item.Attributes.metadata.M : null;
162196
return this.s3.get(full_hash, this.prefix)
163197
.then(result => {
164198
// If we're here, we are pretty confident there is a match. But never hurts to double check
165199
if (result.hit) {
166200
return {
167201
config: result.data.toString(),
202+
special_metadata: special_metadata,
168203
metadata: metadata
169204
};
170205
} else {

test/storage/storage-s3-tests.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,14 @@ describe('Stores to s3', () => {
162162
prefix: "ABCDEF",
163163
uniqueSubHash: "ABCDEFG",
164164
fullHash: "ABCDEFGHIJKLMNOP",
165-
config: "yo"
165+
config: '{"contents": []}'
166166
};
167167

168168
const ran = {s3: false, dynamo: false};
169169
s3PutObjectHandlers.push((q) => {
170170
q.Bucket.should.equal('bucket');
171171
q.Key.should.equal('prefix/ABCDEFGHIJKLMNOP');
172-
q.Body.should.equal('yo');
172+
q.Body.should.equal('{"contents": []}');
173173
ran.s3 = true;
174174
return {};
175175
});
@@ -182,7 +182,8 @@ describe('Stores to s3', () => {
182182
stats: {M: {clicks: {N: '0'}}},
183183
creation_ip: {S: 'localhost'},
184184
// Cheat the date
185-
creation_date: {S: q.Item.creation_date.S}
185+
creation_date: {S: q.Item.creation_date.S},
186+
metadata: {M: {editor_count: {N: "0"}}}
186187
});
187188
ran.dynamo = true;
188189
return {};

views/head.pug

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,23 @@
11
link(rel="icon" href="favicon.ico?v=1")
22
meta(charset="utf-8")
33
meta(http-equiv="X-UA-Compatible" content="IE=edge")
4-
5-
if metadata && metadata.ogDescription
6-
meta(name="description" content=metadata.ogDescription)
7-
meta(property="og:site_name" content="Compiler Explorer")
8-
if metadata.ogAuthor && metadata.ogAuthor !== '.'
9-
meta(property="og:description" content=metadata.ogDescription + ". Author(s): " + metadata.ogAuthor)
10-
else
11-
meta(property="og:description" content=metadata.ogDescription)
12-
else if metadata && metadata.code
13-
meta(property="og:site_name" content="Compiler Explorer " + metadata.code.lang)
14-
meta(property="og:description" content=metadata.code.source)
15-
else
16-
meta(property="og:site_name" content="Compiler Explorer")
17-
meta(name="description" content="Compiler Explorer is an interactive online compiler which shows the assembly output of compiled C, C++, Rust, Go, D, Haskell, Swift & Pascal code.")
18-
19-
if metadata && metadata.ogTitle
4+
title Compiler Explorer
5+
if metadata
206
meta(property="og:title" content=metadata.ogTitle)
21-
title Compiler Explorer - #{metadata.ogTitle}
7+
if metadata.ogDescription
8+
meta(name="description" content=metadata.ogDescription)
9+
meta(property="og:description" content=metadata.ogDescription)
10+
else
11+
meta(name="description" content="Compiler Explorer is an interactive online compiler which shows the assembly output of compiled C, C++, Rust, Go, D, Haskell, Swift, Pascal & CUDA code.")
12+
meta(property="og:description" content="Compiler Explorer is an interactive online compiler which shows the assembly output of compiled C, C++, Rust, Go, D, Haskell, Swift, Pascal & CUDA code.")
2213
else
23-
title Compiler Explorer
24-
14+
meta(name="description" content="Compiler Explorer is an interactive online compiler which shows the assembly output of compiled C, C++, Rust, Go, D, Haskell, Swift, Pascal & CUDA code.")
15+
meta(property="og:title" content="Compiler Explorer")
2516
meta(name="twitter:card" content="summary")
2617
meta(name="twitter:site" content="@mattgodbolt")
2718
meta(property="og:image" content=require("ce-icon.png"))
2819
meta(property="og:image:secure_url" content=require("ce-icon.png"))
29-
meta(property="og:image:type" content="image/png")
20+
meta(property="og:image:type" content="image/jpg")
3021
meta(property="og:image:width" content="145")
3122
meta(property="og:image:height" content="145")
3223
meta(name="author" content="Matt Godbolt")

0 commit comments

Comments
 (0)