Skip to content

Commit 1ce36dd

Browse files
authored
add support for opengraph tags (#598)
1 parent c0c4f82 commit 1ce36dd

File tree

11 files changed

+48
-6
lines changed

11 files changed

+48
-6
lines changed

ecma-logo.png

5.84 KB
Loading

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"scripts": {
88
"build": "tsc -sourceMap -declarationMap",
99
"build-release": "tsc",
10-
"build-spec": "mkdir -p docs && node bin/ecmarkup.js spec/index.html docs/index.html --assets-dir=docs",
10+
"build-spec": "mkdir -p docs && node bin/ecmarkup.js spec/index.html docs/index.html --assets-dir=docs && cp ecma-logo.png docs/",
1111
"prepack": "safe-publish-latest && npm run build-release",
1212
"format-spec": "node bin/emu-format.js --write spec/index.html",
1313
"test": "mocha",

spec/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<title>Ecmarkup</title>
44
<pre class="metadata">
55
repository: https://github.com/tc39/ecmarkup
6+
desription: An HTML superset/Markdown subset source format for ECMAScript and related specifications
67
copyright: false
78
markEffects: true
89
</pre>
@@ -69,6 +70,7 @@ <h1>Options</h1>
6970
<tr><td></td><td>`version`</td><td>Document version, for example "6&lt;sup>th&lt;/sup> Edition" (which renders like "6<sup>th</sup> Edition") or "Draft 1".</td></tr>
7071
<tr><td></td><td>`date`</td><td>Timestamp of document rendering, used for various pieces of boilerplate. Defaults to the value of <a href="https://reproducible-builds.org/docs/source-date-epoch/">the `SOURCE_DATE_EPOCH` environment variable</a> (as a number of second since the POSIX epoch) if it exists, otherwise to the current date and time.</td></tr>
7172
<tr><td></td><td>`shortname`</td><td>Document shortname, for example "ECMA-262". If present and `status` is "draft", `version` defaults to "Draft <var>shortname</var>".</td></tr>
73+
<tr><td></td><td>`description`</td><td>Brief description to be used for link previews in social media and the like.</td></tr>
7274
<tr><td></td><td>`location`</td><td>URL of this document. Used in conjunction with the biblio file to support inbound references from other documents.</td></tr>
7375
<tr><td></td><td>`copyright`</td><td>Emit copyright and software license information. Boolean, default true.</td></tr>
7476
<tr><td></td><td>`contributors`</td><td>Contributors to this specification, i.e. those who own the copyright. If your proposal includes text from any Ecma specification, this should include "Ecma International".</td></tr>

src/Spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,42 @@ ${this.opts.multipage ? `<li><span>Navigate to/from multipage</span><code>m</cod
14381438
this.doc.body.insertBefore(h1, this.doc.body.firstChild);
14391439
}
14401440
}
1441+
1442+
// opengraph tags
1443+
const metas = [...this.doc.querySelectorAll('meta')];
1444+
const insertMetaTag =
1445+
metas.length === 0
1446+
? (node: HTMLMetaElement) => this.doc.head.prepend(node)
1447+
: (node: HTMLMetaElement) => metas[metas.length - 1].after(node);
1448+
if (!metas.some(n => n.getAttribute('property') === 'og:description')) {
1449+
const description = (
1450+
this.opts.description ??
1451+
(this.doc.querySelector('emu-intro') ?? this.doc.querySelector('emu-clause'))?.textContent
1452+
)
1453+
?.slice(0, 300)
1454+
.trim();
1455+
if (description) {
1456+
const meta = this.doc.createElement('meta');
1457+
meta.setAttribute('property', 'og:description');
1458+
meta.setAttribute('content', description);
1459+
insertMetaTag(meta);
1460+
}
1461+
}
1462+
if (!metas.some(n => n.getAttribute('property') === 'og:title')) {
1463+
const title = this.opts.title ?? this.doc.querySelector('title')?.textContent;
1464+
if (title) {
1465+
const meta = this.doc.createElement('meta');
1466+
meta.setAttribute('property', 'og:title');
1467+
meta.setAttribute('content', title);
1468+
insertMetaTag(meta);
1469+
}
1470+
}
1471+
if (!metas.some(n => n.getAttribute('property') === 'og:image')) {
1472+
const meta = this.doc.createElement('meta');
1473+
meta.setAttribute('property', 'og:image');
1474+
meta.setAttribute('content', 'https://tc39.es/ecmarkup/ecma-logo.png');
1475+
insertMetaTag(meta);
1476+
}
14411477
}
14421478

14431479
private buildCopyrightBoilerplate() {

src/ecmarkup.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export interface Options {
2727
version?: string;
2828
title?: string;
2929
shortname?: string;
30+
description?: string;
3031
stage?: string | null;
3132
copyright?: boolean;
3233
date?: Date;

test/baselines/generated-reference/copyright.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!doctype html>
2-
<head><meta charset="utf-8"><title>test title!</title></head><body><div id="shortcuts-help">
2+
<head><meta charset="utf-8"><meta property="og:image" content="https://tc39.es/ecmarkup/ecma-logo.png"><meta property="og:title" content="test title!"><meta property="og:description" content="Test Clause"><title>test title!</title></head><body><div id="shortcuts-help">
33
<ul>
44
<li><span>Toggle shortcuts help</span><code>?</code></li>
55
<li><span>Toggle "can call user code" annotations</span><code>u</code></li>

test/baselines/generated-reference/proposal-copyright.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!doctype html>
2-
<head><meta charset="utf-8"><title>test title!</title></head><body><div id="shortcuts-help">
2+
<head><meta charset="utf-8"><meta property="og:image" content="https://tc39.es/ecmarkup/ecma-logo.png"><meta property="og:title" content="test title!"><title>test title!</title></head><body><div id="shortcuts-help">
33
<ul>
44
<li><span>Toggle shortcuts help</span><code>?</code></li>
55
<li><span>Toggle "can call user code" annotations</span><code>u</code></li>

test/baselines/generated-reference/shortname.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!doctype html>
2-
<head><meta charset="utf-8"><title>test title!</title></head><body><div id="shortcuts-help">
2+
<head><meta charset="utf-8"><meta property="og:image" content="https://tc39.es/ecmarkup/ecma-logo.png"><meta property="og:title" content="test title!"><title>test title!</title></head><body><div id="shortcuts-help">
33
<ul>
44
<li><span>Toggle shortcuts help</span><code>?</code></li>
55
<li><span>Toggle "can call user code" annotations</span><code>u</code></li>

test/baselines/generated-reference/test.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<!doctype html>
2-
<head><meta charset="utf-8">
2+
<head><meta charset="utf-8"><meta property="og:image" content="https://tc39.es/ecmarkup/ecma-logo.png"><meta property="og:title" content="Ecmarkup Test Document"><meta property="og:description" content="Intro
3+
4+
Sub Intro">
35
<link rel="stylesheet" href="css/elements.css">
46
<script src="ecmarkup.js"></script>
57
<title>Ecmarkup Test Document</title></head><body><div id="shortcuts-help">

test/baselines/generated-reference/title.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!doctype html>
2-
<head><meta charset="utf-8"><title>test title!</title></head><body><div id="shortcuts-help">
2+
<head><meta charset="utf-8"><meta property="og:image" content="https://tc39.es/ecmarkup/ecma-logo.png"><meta property="og:title" content="test title!"><meta property="og:description" content="a test document"><title>test title!</title></head><body><div id="shortcuts-help">
33
<ul>
44
<li><span>Toggle shortcuts help</span><code>?</code></li>
55
<li><span>Toggle "can call user code" annotations</span><code>u</code></li>

0 commit comments

Comments
 (0)