Skip to content

Commit 2d23acc

Browse files
authored
Merge pull request compiler-explorer#1055 from mattgodbolt/ogtest
og: tags
2 parents 245ae4d + e7c6f81 commit 2d23acc

File tree

5 files changed

+50
-24
lines changed

5 files changed

+50
-24
lines changed

app.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -335,16 +335,29 @@ aws.initConfig(awsProps)
335335
const id = req.params.id;
336336
storageHandler.expandId(id)
337337
.then(result => {
338+
const config = JSON.parse(result.config);
338339
const metadata = {
339-
ogDescription: result.metadata ? result.metadata.description.S : null,
340-
ogAuthor: result.metadata ? result.metadata.author.S : null,
341-
ogTitle: result.metadata ? result.metadata.title.S : null
340+
ogDescription: result.specialMetadata ? result.specialMetadata.description.S : null,
341+
ogAuthor: result.specialMetadata ? result.specialMetadata.author.S : null,
342+
ogTitle: result.specialMetadata ? result.specialMetadata.title.S : "Compiler Explorer"
342343
};
344+
if (!metadata.ogDescription) {
345+
const sources = utils.glGetEditorSources(config.content);
346+
if (sources.length === 1) {
347+
const lang = languages[sources[0].language];
348+
metadata.ogDescription = sources[0].source;
349+
if (lang) {
350+
metadata.ogTitle += ` - ${lang.name}`;
351+
}
352+
}
353+
} else if (metadata.ogAuthor && metadata.ogAuthor !== '.') {
354+
metadata.ogDescription += `\nAuthor(s): ${metadata.ogAuthor}`;
355+
}
343356
staticHeaders(res);
344357
contentPolicyHeader(res);
345358
res.render('index', renderConfig({
346359
embedded: false,
347-
config: JSON.parse(result.config),
360+
config: config,
348361
metadata: metadata
349362
}));
350363
})

lib/storage/storage-s3.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ class StorageS3 extends StorageBase {
8181
creation_date: {
8282
S: now.toISOString()
8383
}
84-
8584
}
8685
}).promise(),
8786
this.s3.put(item.fullHash, item.config, this.prefix, {})
@@ -157,15 +156,14 @@ class StorageS3 extends StorageBase {
157156
}).promise()
158157
.then(item => {
159158
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;
159+
const special_metadata = item.Attributes.named_metadata ? item.Attributes.named_metadata.M : null;
162160
return this.s3.get(full_hash, this.prefix)
163161
.then(result => {
164162
// If we're here, we are pretty confident there is a match. But never hurts to double check
165163
if (result.hit) {
166164
return {
167165
config: result.data.toString(),
168-
metadata: metadata
166+
specialMetadata: special_metadata
169167
};
170168
} else {
171169
throw new Error(`ID ${id} not present in storage`);

lib/utils.js

+20
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,23 @@ exports.getHash = function getHash(object, HashVersion = 'Compiler Explorer Defa
123123
.update(asString)
124124
.digest('hex');
125125
};
126+
127+
/***
128+
* Gets every pair of source, lang available
129+
* @param content {Array}
130+
* @returns {Array}
131+
*/
132+
exports.glGetEditorSources = function glGetEditorSources(content) {
133+
let sources = [];
134+
_.each(content, element => {
135+
if (element.type === 'component') {
136+
if (element.componentName === 'codeEditor') {
137+
sources.push({source: element.componentState.source, language: element.componentState.lang});
138+
}
139+
} else {
140+
sources = glGetEditorSources(element.content).concat(sources);
141+
}
142+
});
143+
return sources;
144+
145+
};

test/storage/storage-s3-tests.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ describe('Stores to s3', () => {
162162
prefix: "ABCDEF",
163163
uniqueSubHash: "ABCDEFG",
164164
fullHash: "ABCDEFGHIJKLMNOP",
165-
config: "yo"
165+
config: 'yo'
166166
};
167167

168168
const ran = {s3: false, dynamo: false};

views/head.pug

+10-15
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
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-
if metadata.ogAuthor && metadata.ogAuthor !== '.'
8-
meta(property="og:description" content=metadata.ogDescription + ". Author(s): " + metadata.ogAuthor)
9-
else
10-
meta(property="og:description" content=metadata.ogDescription)
11-
else
12-
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.")
13-
14-
if metadata && metadata.ogTitle
4+
title Compiler Explorer
5+
if metadata
156
meta(property="og:title" content=metadata.ogTitle)
16-
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.")
1713
else
18-
title Compiler Explorer
19-
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")
2016
meta(name="twitter:card" content="summary")
2117
meta(name="twitter:site" content="@mattgodbolt")
22-
meta(property="og:site_name" content="Compiler Explorer")
2318
meta(property="og:image" content=require("ce-icon.png"))
2419
meta(property="og:image:secure_url" content=require("ce-icon.png"))
2520
meta(property="og:image:type" content="image/png")

0 commit comments

Comments
 (0)