Skip to content

Commit

Permalink
refactor(ssr): use Intl.ListFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
caugner committed Aug 20, 2024
1 parent 6af1c00 commit 8fa63e7
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions ssr/meta-description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ export function getMetaDescription(doc: Doc): string {
.filter(([section]) => sections.includes(section))
.map(([, text]) => text);

const syntaxContents = enumerate(...syntaxItems);
const otherContents = enumerate(...otherItems);
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,
Expand Down Expand Up @@ -71,11 +75,3 @@ export function getMetaDescription(doc: Doc): string {

return doc.summary;
}

function enumerate(...items: string[]) {
items = items.filter(Boolean);

const lastItem = items.pop();

return [items.join(", "), lastItem].filter(Boolean).join(", and ");
}

0 comments on commit 8fa63e7

Please sign in to comment.