Skip to content

Commit 4e7e4a6

Browse files
committed
improve the docs titles and add structured data
1 parent 8be6b51 commit 4e7e4a6

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

‎scripts/docs.js‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// builds the documentation site into docs/ with typedoc, then adds what typedoc can't: a description and link previews
2-
// (Open Graph) on every page, the icons browsers look for at the root, the README's wordmark following the docs' theme,
2+
// (Open Graph) on every page, structured data (JSON-LD) on the start page, the icons browsers look for at the root, the README's wordmark following the docs' theme,
33
// and redirects from the pages of the old jsdoc site (imagescript.dev/Image.html#resize -> classes/Image.html#resize)
44
// so existing links keep working
55
const fs = require('fs');
@@ -14,7 +14,7 @@ fs.rmSync(out, { recursive: true, force: true });
1414
execFileSync(process.execPath, [typedoc], { cwd: root, stdio: 'inherit' });
1515

1616
const site = 'https://imagescript.dev/';
17-
const description = 'Pure TypeScript image processing that runs everywhere: Node.js, Deno, Bun, browsers and edge runtimes';
17+
const description = 'Decode, resize, edit and encode PNG, JPEG, WebP, AVIF and GIF in pure TypeScript. No dependencies; runs on Node.js, Deno, Bun, browsers and edge runtimes.';
1818
for (const file of ['favicon.ico', 'apple-touch-icon.png', 'social-preview.png'])
1919
fs.copyFileSync(path.join(root, 'media', file), path.join(out, file));
2020

@@ -23,10 +23,29 @@ const pages = fs.readdirSync(out, { recursive: true }).filter(file => file.endsW
2323
for (const page of pages) {
2424
const file = path.join(out, page);
2525
let html = fs.readFileSync(file, 'utf8');
26-
const title = html.match(/<title>(.*?)<\/title>/)?.[1] ?? 'ImageScript';
26+
// typedoc titles carry the version ('Image | ImageScript - v3.20.5'); the version changes every release, so it stays out
27+
const title = page === 'index.html' ? 'ImageScript: Pure TypeScript image processing that runs everywhere'
28+
: (html.match(/<title>(.*?)<\/title>/)?.[1] ?? 'ImageScript').replace(/ - v[^<]*$/, '');
29+
html = html.replace(/<title>.*?<\/title>/, `<title>${escape(title)}</title>`);
2730
const url = site + page.split(path.sep).join('/').replace(/^index\.html$/, '');
2831
html = html.replace(/<meta name="description" content="[^"]*"\/>/, `<meta name="description" content="${escape(description)}"/>`);
32+
// what the start page is, for search engines (https://schema.org/SoftwareApplication)
33+
const structured = page !== 'index.html' ? '' : `<script type="application/ld+json">${JSON.stringify({
34+
'@context': 'https://schema.org',
35+
'@type': 'SoftwareApplication',
36+
name: 'ImageScript',
37+
description,
38+
url: site,
39+
image: `${site}social-preview.png`,
40+
applicationCategory: 'DeveloperApplication',
41+
operatingSystem: 'Any',
42+
softwareVersion: require('../package.json').version,
43+
license: 'https://opensource.org/license/mit',
44+
offers: { '@type': 'Offer', price: 0, priceCurrency: 'USD' },
45+
sameAs: ['https://github.com/imagescript/ImageScript', 'https://www.npmjs.com/package/imagescript', 'https://jsr.io/@matmen/imagescript']
46+
}).replace(/</g, '\\u003c')}</script>`;
2947
html = html.replace('</head>', [
48+
structured,
3049
`<meta property="og:type" content="website"/>`,
3150
`<meta property="og:site_name" content="ImageScript"/>`,
3251
`<meta property="og:title" content="${title}"/>`,

0 commit comments

Comments
 (0)