Skip to content

Commit

Permalink
add support for opengraph tags (#598)
Browse files Browse the repository at this point in the history
  • Loading branch information
bakkot authored Jun 27, 2024
1 parent c0c4f82 commit 1ce36dd
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 6 deletions.
Binary file added ecma-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"build": "tsc -sourceMap -declarationMap",
"build-release": "tsc",
"build-spec": "mkdir -p docs && node bin/ecmarkup.js spec/index.html docs/index.html --assets-dir=docs",
"build-spec": "mkdir -p docs && node bin/ecmarkup.js spec/index.html docs/index.html --assets-dir=docs && cp ecma-logo.png docs/",
"prepack": "safe-publish-latest && npm run build-release",
"format-spec": "node bin/emu-format.js --write spec/index.html",
"test": "mocha",
Expand Down
2 changes: 2 additions & 0 deletions spec/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<title>Ecmarkup</title>
<pre class="metadata">
repository: https://github.com/tc39/ecmarkup
desription: An HTML superset/Markdown subset source format for ECMAScript and related specifications
copyright: false
markEffects: true
</pre>
Expand Down Expand Up @@ -69,6 +70,7 @@ <h1>Options</h1>
<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>
<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>
<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>
<tr><td></td><td>`description`</td><td>Brief description to be used for link previews in social media and the like.</td></tr>
<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>
<tr><td></td><td>`copyright`</td><td>Emit copyright and software license information. Boolean, default true.</td></tr>
<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>
Expand Down
36 changes: 36 additions & 0 deletions src/Spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,42 @@ ${this.opts.multipage ? `<li><span>Navigate to/from multipage</span><code>m</cod
this.doc.body.insertBefore(h1, this.doc.body.firstChild);
}
}

// opengraph tags
const metas = [...this.doc.querySelectorAll('meta')];
const insertMetaTag =
metas.length === 0
? (node: HTMLMetaElement) => this.doc.head.prepend(node)
: (node: HTMLMetaElement) => metas[metas.length - 1].after(node);
if (!metas.some(n => n.getAttribute('property') === 'og:description')) {
const description = (
this.opts.description ??
(this.doc.querySelector('emu-intro') ?? this.doc.querySelector('emu-clause'))?.textContent
)
?.slice(0, 300)
.trim();
if (description) {
const meta = this.doc.createElement('meta');
meta.setAttribute('property', 'og:description');
meta.setAttribute('content', description);
insertMetaTag(meta);
}
}
if (!metas.some(n => n.getAttribute('property') === 'og:title')) {
const title = this.opts.title ?? this.doc.querySelector('title')?.textContent;
if (title) {
const meta = this.doc.createElement('meta');
meta.setAttribute('property', 'og:title');
meta.setAttribute('content', title);
insertMetaTag(meta);
}
}
if (!metas.some(n => n.getAttribute('property') === 'og:image')) {
const meta = this.doc.createElement('meta');
meta.setAttribute('property', 'og:image');
meta.setAttribute('content', 'https://tc39.es/ecmarkup/ecma-logo.png');
insertMetaTag(meta);
}
}

private buildCopyrightBoilerplate() {
Expand Down
1 change: 1 addition & 0 deletions src/ecmarkup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface Options {
version?: string;
title?: string;
shortname?: string;
description?: string;
stage?: string | null;
copyright?: boolean;
date?: Date;
Expand Down
2 changes: 1 addition & 1 deletion test/baselines/generated-reference/copyright.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!doctype html>
<head><meta charset="utf-8"><title>test title!</title></head><body><div id="shortcuts-help">
<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">
<ul>
<li><span>Toggle shortcuts help</span><code>?</code></li>
<li><span>Toggle "can call user code" annotations</span><code>u</code></li>
Expand Down
2 changes: 1 addition & 1 deletion test/baselines/generated-reference/proposal-copyright.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!doctype html>
<head><meta charset="utf-8"><title>test title!</title></head><body><div id="shortcuts-help">
<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">
<ul>
<li><span>Toggle shortcuts help</span><code>?</code></li>
<li><span>Toggle "can call user code" annotations</span><code>u</code></li>
Expand Down
2 changes: 1 addition & 1 deletion test/baselines/generated-reference/shortname.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!doctype html>
<head><meta charset="utf-8"><title>test title!</title></head><body><div id="shortcuts-help">
<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">
<ul>
<li><span>Toggle shortcuts help</span><code>?</code></li>
<li><span>Toggle "can call user code" annotations</span><code>u</code></li>
Expand Down
4 changes: 3 additions & 1 deletion test/baselines/generated-reference/test.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<!doctype html>
<head><meta charset="utf-8">
<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
Sub Intro">
<link rel="stylesheet" href="css/elements.css">
<script src="ecmarkup.js"></script>
<title>Ecmarkup Test Document</title></head><body><div id="shortcuts-help">
Expand Down
2 changes: 1 addition & 1 deletion test/baselines/generated-reference/title.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!doctype html>
<head><meta charset="utf-8"><title>test title!</title></head><body><div id="shortcuts-help">
<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">
<ul>
<li><span>Toggle shortcuts help</span><code>?</code></li>
<li><span>Toggle "can call user code" annotations</span><code>u</code></li>
Expand Down
1 change: 1 addition & 0 deletions test/baselines/sources/title.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<pre class=metadata>
title: test title!
description: a test document
status: draft
version: Draft 1
toc: false
Expand Down

0 comments on commit 1ce36dd

Please sign in to comment.