-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathconfig-node.js
347 lines (277 loc) · 8.48 KB
/
config-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import TOML from '@iarna/toml'
import fs from 'fs-extra'
import pkg from '~/package.json'
import { getSiteUrl } from '~/helpers/url.js'
import { getRouteType } from '~/helpers/app-derived.js'
export const siteUrl = getSiteUrl()
export const nuxtHead = {
// this htmlAttrs you need
htmlAttrs: {
lang: 'en',
},
title: 'Does It ARM',
description: pkg.description,
meta: [
{ charset: 'utf-8' },
{
name: 'viewport',
content: 'width=device-width, initial-scale=1'
},
{
hid: 'description',
name: 'description',
content: pkg.description
},
{
'property': 'og:image',
'content': `${ siteUrl }/images/og-image.png`
},
{
'property': 'og:image:width',
'content': '1200'
},
{
'property': 'og:image:height',
'content': '627'
},
{
'property': 'og:image:alt',
'content': 'Does It ARM Logo'
},
// Twitter Card
{
'property': 'twitter:card',
'content': 'summary'
},
{
'property': 'twitter:title',
'content': 'Does It ARM'
},
{
'property': 'twitter:description',
'content': pkg.description
},
{
'property': 'twitter:url',
'content': `${ siteUrl }`
},
{
'property': 'twitter:image',
'content': `${ siteUrl }/images/mark.png`
}
],
link: [
// Favicon
{
rel: 'icon',
type: 'image/x-icon',
href: '/favicon.ico'
},
// Gtag Preconnect
{
rel: 'preconnect',
href: 'https://www.googletagmanager.com'
},
// Carbon Preconnects
{
rel: 'preconnect',
href: 'https://cdn.carbonads.com'
},
{
rel: 'preconnect',
href: 'https://srv.carbonads.net'
},
{
rel: 'preconnect',
href: 'https://cdn4.buysellads.net'
},
],
script: [
// // Carbon Ads
// // https://sell.buysellads.com/zones/1294/ad-tags#z=js
// {
// // <script async type="text/javascript" src="//cdn.carbonads.com/carbon.js?serve=CK7DVK3M&placement=doesitarmcom" id="_carbonads_js"></script>
// src: 'https://cdn.carbonads.com/carbon.js?serve=CK7DVK3M&placement=doesitarmcom',
// async: true,
// type: 'text/javascript',
// id: '_carbonads_js',
// class: 'include-on-static carbon-inline-wide',
// body: true
// }
]
}
export async function getNetlifyConfig () {
const configPath = './netlify.toml'
const tomlContent = await fs.readFile(configPath, 'utf-8')
const netlifyConfig = TOML.parse(tomlContent)
// console.log('netlifyConfig', netlifyConfig)
// console.log('tomlContent', tomlContent)
return netlifyConfig
}
export async function getNetlifyRedirect ( path ) {
// Check if the path is valid
// by checking if it starts with a slash
// and does not end with a slash
// if ( !path.startsWith('/') || path.endsWith('/') ) {
// throw new Error(`Invalid Netlify redirect path: ${ path }`)
// }
const netlifyConfig = await getNetlifyConfig()
const redirects = netlifyConfig.redirects
for ( const redirect of redirects ) {
// Check if the from path is valid
// by checking if it starts with a slash
// and does not end with a slash
if ( !redirect.from.startsWith('/') || redirect.from.endsWith('/') ) {
throw new Error(`Invalid Netlify redirect.from path: ${ redirect.from }`)
}
if ( redirect.from === path ) {
return redirect
}
}
return null
}
export function makeTitle ( listing ) {
return `Does ${ listing.name } work on Apple Silicon? - ${ nuxtHead.title }`
}
export function makeDescription ( listing ) {
return `Latest reported support status of ${ listing.name } on Apple Silicon and ${ this.$config.processorsVerbiage } Processors.`
}
function makeTag ( tag, tagName = 'meta' ) {
const attributes = Object.entries(tag).map( ([ name, value ]) => `${name}="${value}"` ).join(' ')
return `<${tagName} ${attributes}>`
}
function mapMetaTag ( tag ) {
if ( tag.hasOwnProperty('property') ) {
return [
`property-${tag.property}`,
makeTag(tag)
]
}
if ( tag.hasOwnProperty('name') ) {
return [
`name-${tag.name}`,
makeTag(tag)
]
}
if ( tag.hasOwnProperty('charset') ) {
return [
'charset',
makeTag(tag)
]
}
}
function mapLinkTag ( tag ) {
return [
`type-${tag.type}`,
makeTag(tag, 'link')
]
}
export class PageHead {
constructor ( options = {} ) {
const {
title,
description,
meta = [],
link = [],
structuredData = null,
domain = getSiteUrl(),
pathname = '',
} = options
this.title = title
this.description = description
this.meta = meta
this.link = link
this.structuredData = structuredData
this.domain = domain
this.pathname = pathname
}
get pageUrl () {
const urlInstance = new URL( this.domain )
urlInstance.pathname = this.pathname
return urlInstance
}
get pageUrlString () {
return this.pageUrl.toString()
}
get routeType () {
if ( this.pathname.length < 2 ) return 'none'
return getRouteType( this.pathname )
}
get defaultMeta () {
return nuxtHead.meta
}
get defaultMetaTags () {
return Object.fromEntries( nuxtHead.meta.map( mapMetaTag ) )
}
get defaultLinkTags () {
return Object.fromEntries( nuxtHead.link.map( mapLinkTag ) )
}
get pageMeta () {
// console.log('this.defaultMeta', this.defaultMeta)
return [
...this.defaultMeta,
{
'property': 'twitter:url',
'content': this.pageUrlString
},
...this.meta
]
}
get metaTags () {
const metaTags = {
// ...this.defaultMeta,
// 'property-twitter:url': `<meta property="twitter:url" content="${ this.pageUrlString }">`,
...Object.fromEntries( this.pageMeta.map(mapMetaTag) )
}
// Get description from data
if ( this.description ) {
// Set meta description
metaTags['name-description'] = `<meta hid="description" name="description" content="${ this.description }">`
// Set twitter description
metaTags['property-twitter:description'] = `<meta hid="twitter:description" property="twitter:description" content="${ this.description }">`
}
// Get title from data
if ( this.title ) {
// Set twitter title
metaTags['property-twitter:title'] = `<meta hid="twitter:title" property="twitter:title" content="${ this.title }">`
}
return metaTags
}
get metaMarkup () {
return Object.values( this.metaTags ).join('')
}
get linkTags () {
const linkTags = {
...this.defaultLinkTags,
...Object.fromEntries( this.link.map( mapLinkTag ) )
}
return linkTags
}
get linkMarkup () {
return Object.values( this.linkTags ).join('')
}
get metaAndLinkMarkup () {
return [
this.metaMarkup,
this.linkMarkup
].join('')
}
get structuredDataMarkup () {
// console.log( 'this.routeType', this.routeType, this.pathname )
const hasStructuredData = this.structuredData !== null
// Route is primarily for video
const videoRouteType = [ 'tv', 'benchmarks' ].includes( this.routeType )
if ( hasStructuredData && videoRouteType ) {
const structuredDataJson = JSON.stringify( this.structuredData )
return `<script type="application/ld+json">${ structuredDataJson }</script>`
}
return ''
}
get allHeadMarkup () {
return [
this.metaMarkup,
this.linkMarkup,
this.structuredDataMarkup
].join('')
}
}