-
-
Notifications
You must be signed in to change notification settings - Fork 427
Expand file tree
/
Copy pathuseJsonLd.ts
More file actions
43 lines (40 loc) · 1.05 KB
/
useJsonLd.ts
File metadata and controls
43 lines (40 loc) · 1.05 KB
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
import type { Thing, WebSite, WithContext } from 'schema-dts'
/**
* Inject JSON-LD script into head
*/
export function setJsonLd(schema: WithContext<Thing> | WithContext<Thing>[]): void {
const schemas = Array.isArray(schema) ? schema : [schema]
useHead({
script: schemas.map((s, i) => ({
type: 'application/ld+json',
innerHTML: JSON.stringify(s),
key: `json-ld-${i}`,
})),
})
}
/**
* Create WebSite schema with search action
*/
export function createWebSiteSchema(
siteUrl: string,
options?: {
name?: string
description?: string
},
): WithContext<WebSite> {
return {
'@context': 'https://schema.org',
'@type': 'WebSite',
'name': options?.name ?? 'npmx',
'url': siteUrl,
'description': options?.description ?? 'A fast, modern browser for the npm registry',
'potentialAction': {
'@type': 'SearchAction',
'target': {
'@type': 'EntryPoint',
'urlTemplate': `${siteUrl}/search?q={search_term_string}`,
},
'query': 'required name=search_term_string',
},
}
}