-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhance(seo): use meta description templates for web-api-* pages (#11635
) * enhance(build): use meta description templates for web-api-* pages * chore(seo): revise meta description templates * chore(seo): further simplify templates * refactor(build): move meta description template to ssr * refactor(ssr): use Intl.ListFormat
- Loading branch information
Showing
2 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { DEFAULT_LOCALE } from "../libs/constants/index.js"; | ||
import { Doc } from "../libs/types/document.js"; | ||
|
||
export function getMetaDescription(doc: Doc): string { | ||
const { pageType } = doc; | ||
if (doc.locale === DEFAULT_LOCALE) { | ||
const sections = doc.toc?.map(({ text }) => text) ?? []; | ||
|
||
const syntaxItems = Object.entries({ | ||
Value: "type", | ||
"Event type": "type", | ||
Syntax: "syntax", | ||
Parameters: "parameters", | ||
Constructor: "constructor", | ||
"Instance properties": "properties", | ||
"Event properties": "properties", | ||
"Instance methods": "methods", | ||
"Return value": "return value", | ||
}) | ||
.filter(([section]) => sections.includes(section)) | ||
.map(([, text]) => text); | ||
|
||
const otherItems = Object.entries({ | ||
Exceptions: "exceptions", | ||
Examples: "code examples", | ||
Specifications: "specifications", | ||
"Browser compatibility": "browser compatibility", | ||
}) | ||
.filter(([section]) => sections.includes(section)) | ||
.map(([, text]) => text); | ||
|
||
const listFormatter = new Intl.ListFormat("en", { | ||
style: "long", | ||
type: "conjunction", | ||
}); | ||
const syntaxContents = listFormatter.format(syntaxItems); | ||
const otherContents = listFormatter.format(otherItems); | ||
const contents = [ | ||
syntaxContents ? `its ${syntaxContents}` : "", | ||
otherContents, | ||
] | ||
.filter(Boolean) | ||
.join(", "); | ||
|
||
switch (pageType) { | ||
case "web-api-instance-property": | ||
case "web-api-static-property": | ||
// "Learn about the Window.localStorage property, ..." | ||
// "Learn about the static Notification.permission property, ..." | ||
return `Learn about the ${doc.title.replace(/^(.*?): (.*?) (static )?property$/, "$3$1.$2 property")}, including ${contents}.`; | ||
|
||
case "web-api-instance-method": | ||
case "web-api-static-method": | ||
// "Learn about the EventTarget.addEventListener() method, ..." | ||
// "Learn about the static URL.createObjectURL() method, ..." | ||
return `Learn about the ${doc.title.replace(/^(.*?): (.*?) (static )?method$/, "$3$1.$2 method")}, including ${contents}.`; | ||
|
||
case "web-api-interface": | ||
// "Learn about the URLSearchParams interface, ..." | ||
return `Learn about the ${doc.title} interface, including ${contents}.`; | ||
|
||
case "web-api-event": | ||
// "Learn about the DOMContentLoaded event, ..." | ||
return `Learn about the ${doc.title.replace(/^.*?: /, "")}, including ${contents}.`; | ||
|
||
case "web-api-constructor": | ||
// "Learn about the URL() constructor, ..." | ||
return `Learn about the ${doc.title.replace(/^.*?: /, "")}, including ${contents}.`; | ||
|
||
case "web-api-global-function": | ||
// "Learn about the global setTimeout() function, ..." | ||
return `Learn about the ${doc.title.replace(/^(.*) global function$/, "global $1 function")}, including ${contents}.`; | ||
} | ||
} | ||
|
||
return doc.summary; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters