Skip to content

Commit 606298b

Browse files
chore: add SEO meta description (#3202)
- Add SEO meta description to each page - Add also guide-name and service-name metadatas
1 parent 2bc4dbe commit 606298b

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

build/docs/.phpdoc/template/layout.html.twig

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@
22
<html lang="en">
33
<head>
44
<meta charset="utf-8">
5-
{% block head %}
6-
<title>{% block title %}{{ project.name }}{% endblock %}</title>
7-
{% endblock %}
5+
<meta name="guide-name" content="API Reference">
6+
<meta name="service-name" content="AWS SDK for PHP">
7+
<!--
8+
Description is added from DocsBuilder by the `updateMetadata` method.
9+
-->
10+
<meta name="description" content="">
11+
<!--
12+
Title is added from DocsBuilder by the `updateMetadata` method.
13+
-->
14+
<title></title>
815

916
<meta name="viewport" content="width=device-width, initial-scale=1.0">
1017
{{ renderBaseUrlHeader() }}

build/docs/classes/DocsBuilder.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public function build()
139139
if ($this->issueLoggingEnabled) {
140140
$this->updateIssues();
141141
}
142+
$this->updateMetadataInFiles($this->outputDir);
142143
}
143144

144145
private function updateHomepage(array $services)
@@ -351,6 +352,7 @@ private function writeThemeFile($name, $contents)
351352
fwrite(STDOUT, "Writing file: {$name}.\n");
352353
$updatedTemplate = str_replace('index.html#top', $name . '#top', $this->template);
353354
$html = str_replace('{{ contents }}', $contents, $updatedTemplate);
355+
354356
return (bool) file_put_contents("{$this->outputDir}/{$name}", $html);
355357
}
356358

@@ -364,6 +366,71 @@ private function replaceInner($name, $replace, $search = '{{ contents }}')
364366
file_put_contents($path, $contents);
365367
}
366368

369+
/**
370+
* @param string $name
371+
* @param string $html
372+
*
373+
* @return void
374+
*/
375+
private function updateMetadata(string $name, string &$html): void
376+
{
377+
// Standardize file name to be capitalized and without .html
378+
$normalizedName = implode(
379+
'-',
380+
array_map('ucfirst',
381+
explode('-',
382+
str_replace(
383+
'.',
384+
'-',
385+
str_replace(
386+
'.html',
387+
'',
388+
$name
389+
)
390+
)
391+
)
392+
)
393+
);
394+
fwrite(STDOUT, "Adding metadata description to {$name}.\n");
395+
$html = str_replace(
396+
'<meta name="description" content="">',
397+
"<meta name='description' content='$normalizedName'>",
398+
$html
399+
);
400+
// Add title
401+
fwrite(STDOUT, "Adding title to {$name}.\n");
402+
$html = str_replace(
403+
'<title></title>',
404+
"<title>$normalizedName - AWS SDK for PHP V3</title>",
405+
$html
406+
);
407+
}
408+
409+
/**
410+
* @param string $outputDir
411+
*
412+
* @return void
413+
*/
414+
public function updateMetadataInFiles(string $outputDir): void
415+
{
416+
$dirIterator = new \DirectoryIterator($outputDir);
417+
$filter = function ($iterable, callable $pred) {
418+
foreach ($iterable as $value) {
419+
if ($pred($value)) {
420+
yield $value;
421+
}
422+
}
423+
};
424+
$files = $filter($dirIterator, function ($file) {
425+
return str_ends_with($file, '.html');
426+
});
427+
foreach ($files as $file) {
428+
$contents = file_get_contents($file->getPathname());
429+
$this->updateMetadata($file->getFilename(), $contents);
430+
file_put_contents($file->getPathname(), $contents);
431+
}
432+
}
433+
367434
private function gatherServiceVersions()
368435
{
369436
$manifest = __DIR__ . '/../../../src/data/manifest.json';

build/docs/phpdoc.dist.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
55
xmlns="https://www.phpdoc.org"
66
>
7-
<title>AWS SDK for PHP 3.x</title>
7+
<title>AWS SDK for PHP V3</title>
88
<paths>
99
<output>../../build/artifacts/docs</output>
1010
</paths>

0 commit comments

Comments
 (0)