@@ -7,10 +7,20 @@ import { getEntry } from "astro:content";
77import { getOgImage } from " ~/util/og" ;
88import type { CollectionEntry } from " astro:content" ;
99
10- // grab the current top-level folder. Remove . characters for 1.1.1.1 URL
10+ const NOINDEX_PRODUCTS = [" email-security" , " style-guide" , " security" ];
11+
1112const currentSection = Astro .url .pathname .split (" /" )[1 ].replaceAll (" ." , " " );
12- const head = Astro .locals .starlightRoute .entry .data .head ;
13- const noIndexProductsList = [" style-guide" , " security" ];
13+
14+ const entry = Astro .locals .starlightRoute .entry ;
15+ const frontmatter = entry .data ;
16+ const head = frontmatter .head ;
17+
18+ const metaTags = [] as Record <string , string >[];
19+
20+ const shouldNoIndex =
21+ NOINDEX_PRODUCTS .includes (currentSection ) ||
22+ frontmatter .noindex ||
23+ frontmatter .external_link ;
1424
1525if (currentSection ) {
1626 const product = await getEntry (" products" , currentSection );
@@ -31,38 +41,26 @@ if (currentSection) {
3141 content: title ,
3242 };
3343 } else {
34- title = ` ${Astro . locals . starlightRoute . entry . data .title } · ${product .data .meta .title } ` ;
44+ title = ` ${frontmatter .title } · ${product .data .meta .title } ` ;
3545 head .push ({
3646 tag: " title" ,
3747 attrs: {},
3848 content: title ,
3949 });
4050 }
4151
42- head .push ({
43- tag: " meta" ,
44- attrs: { property: " og:title" , content: title },
45- content: " " ,
52+ metaTags .push ({
53+ property: " og:title" ,
54+ content: title ,
4655 });
4756 }
4857
4958 if (product .data .product .title ) {
50- const productName = product .data .product .title ;
51- head .push ({
52- tag: " meta" ,
53- attrs: {
54- name: " pcx_product" ,
55- content: productName ,
56- },
57- content: " " ,
58- });
59- head .push ({
60- tag: " meta" ,
61- attrs: {
62- name: " algolia_product_filter" ,
63- content: productName ,
64- },
65- content: " " ,
59+ [" pcx_product" , " algolia_product_filter" ].map ((property ) => {
60+ metaTags .push ({
61+ property ,
62+ content: product .data .product .title ,
63+ });
6664 });
6765 }
6866
@@ -77,100 +75,56 @@ if (currentSection) {
7775 });
7876 }
7977 }
80-
81- if (noIndexProductsList .includes (currentSection )) {
82- head .push ({
83- tag: " meta" ,
84- attrs: {
85- name: " robots" ,
86- content: " noindex" ,
87- },
88- content: " " ,
89- });
90- }
9178}
9279
93- // Add noindex tag if present in frontmatter
94-
95- if (Astro .locals .starlightRoute .entry .data .noindex ) {
96- head .push ({
97- tag: " meta" ,
98- attrs: {
99- name: " robots" ,
100- content: " noindex" ,
101- },
102- content: " " ,
80+ if (shouldNoIndex ) {
81+ metaTags .push ({
82+ name: " robots" ,
83+ content: " noindex" ,
10384 });
10485}
10586
106- // Adding metadata used for reporting and search indexing
107- // content type
108- if (Astro .locals .starlightRoute .entry .data .pcx_content_type ) {
109- const contentType = Astro .locals .starlightRoute .entry .data .pcx_content_type ;
110- head .push ({
111- tag: " meta" ,
112- attrs: {
113- name: " pcx_content_type" ,
114- content: contentType ,
115- },
116- content: " " ,
117- });
118- head .push ({
119- tag: " meta" ,
120- attrs: {
121- name: " algolia_content_type" ,
122- content: contentType ,
123- },
124- content: " " ,
87+ if (frontmatter .pcx_content_type ) {
88+ [" pcx_content_type" , " algolia_content_type" ].map ((name ) => {
89+ metaTags .push ({
90+ name ,
91+ content: frontmatter .pcx_content_type as string ,
92+ });
12593 });
12694}
12795
128- // other products
129- if (Astro .locals .starlightRoute .entry .data .products ) {
130- const additionalProducts = Astro .locals .starlightRoute .entry .data .products ;
131- head .push ({
132- tag: " meta" ,
133- attrs: {
134- name: " pcx_additional_products" ,
135- content: additionalProducts .toString (),
136- },
137- content: " " ,
96+ if (frontmatter .products ) {
97+ metaTags .push ({
98+ name: " pcx_additional_products" ,
99+ content: frontmatter .products .toString (),
138100 });
139101}
140102
141- // other products
142- if (Astro .locals .starlightRoute .entry .data .tags ) {
143- const pageTags = Astro .locals .starlightRoute .entry .data .tags ;
144- head .push ({
145- tag: " meta" ,
146- attrs: {
147- name: " pcx_tags" ,
148- content: pageTags .toString (),
149- },
150- content: " " ,
103+ if (frontmatter .tags ) {
104+ metaTags .push ({
105+ name: " pcx_tags" ,
106+ content: frontmatter .tags .toString (),
151107 });
152108}
153109
154- if (Astro .locals .starlightRoute .entry .data .updated ) {
155- const daysBetween = differenceInCalendarDays (
156- new Date (),
157- Astro .locals .starlightRoute .entry .data .updated ,
158- );
159- head .push ({
160- tag: " meta" ,
161- attrs: {
162- name: " pcx_last_reviewed" ,
163- content: daysBetween .toString (),
164- },
165- content: " " ,
110+ if (frontmatter .updated ) {
111+ const daysBetween = differenceInCalendarDays (new Date (), frontmatter .updated );
112+
113+ metaTags .push ({
114+ name: " pcx_last_reviewed" ,
115+ content: daysBetween .toString (),
166116 });
167117}
168118
169- // end metadata
119+ if (frontmatter .external_link ) {
120+ metaTags .push ({
121+ content: ` 0; url=${frontmatter .external_link } ` ,
122+ " http-equiv" : " refresh" ,
123+ });
124+ }
170125
171- if (Astro .locals .starlightRoute .entry .data .pcx_content_type === " changelog" ) {
172- const href = new URL (Astro .site ?? " " );
173- href .pathname = Astro .locals .starlightRoute .entry .slug .concat (" /index.xml" );
126+ if (frontmatter .pcx_content_type === " changelog" ) {
127+ const href = new URL (entry .id .concat (" /index.xml" ), Astro .url .origin );
174128
175129 head .push ({
176130 tag: " link" ,
@@ -183,59 +137,26 @@ if (Astro.locals.starlightRoute.entry.data.pcx_content_type === "changelog") {
183137 });
184138}
185139
186- if (Astro .locals .starlightRoute .entry .data .external_link ) {
187- head .push ({
188- tag: " meta" ,
189- attrs: {
190- content: " noindex" ,
191- name: " robots" ,
192- },
193- content: " " ,
194- });
195- head .push ({
196- tag: " meta" ,
197- attrs: {
198- content: ` 0; url=${Astro .locals .starlightRoute .entry .data .external_link } ` ,
199- " http-equiv" : " refresh" ,
200- },
201- content: " " ,
202- });
203- }
204-
205140const ogImagePath = await getOgImage (
206141 (Astro .locals .starlightRoute .originalEntry as CollectionEntry <" changelog" >) ??
207142 Astro .locals .starlightRoute .entry ,
208143);
209144const ogImageUrl = new URL (ogImagePath , Astro .url .origin ).toString ();
210145
211- const ogImageTags = [
212- {
213- tag: " meta" ,
214- attrs: {
215- name: " image" ,
216- content: ogImageUrl ,
217- },
218- content: " " ,
219- },
220- {
221- tag: " meta" ,
222- attrs: {
223- name: " og:image" ,
224- content: ogImageUrl ,
225- },
226- content: " " ,
227- },
228- {
146+ [" image" , " og:image" , " twitter:image" ].map ((property ) => {
147+ metaTags .push ({
148+ property ,
149+ content: ogImageUrl ,
150+ });
151+ });
152+
153+ metaTags .map ((attrs ) => {
154+ head .push ({
229155 tag: " meta" ,
230- attrs: {
231- name: " twitter:image" ,
232- content: ogImageUrl ,
233- },
156+ attrs ,
234157 content: " " ,
235- },
236- ] as const ;
237-
238- head .push (... ogImageTags );
158+ });
159+ });
239160---
240161
241162<script src =" src/scripts/footnotes.ts" ></script >
0 commit comments