@devsantara/head 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,293 @@
1
+ import { _ as TwitterOptions, a as HeadAttributeTypeMap, c as HeadMetaAttributes, d as HeadTitleAttributes, f as IconOptions, g as StylesheetOptions, h as RobotsOptions, i as HeadAdapter, l as HeadScriptAttributes, m as OpenGraphOptions, n as CharSet, o as HeadElement, p as IconPreset, r as ColorScheme, s as HeadLinkAttributes, t as AlternateLocaleOptions, u as HeadStyleAttributes, v as ViewportOptions } from "./types-Cvpk_Zha.js";
2
+
3
+ //#region src/builder.d.ts
4
+ /**
5
+ * Helper object provided to callback functions with utilities for dynamic metadata generation.
6
+ */
7
+ interface BuilderHelper {
8
+ /**
9
+ * Resolves a relative or absolute URL into an absolute URL using the configured metadataBase.
10
+ *
11
+ * @param url - The URL to resolve
12
+ * @returns The resolved absolute URL as a string
13
+ */
14
+ resolveUrl: (url: string | URL) => string;
15
+ }
16
+ /**
17
+ * Generic type for builder method options that can accept either a value or a function
18
+ */
19
+ type BuilderOption<T> = T | ((helper: BuilderHelper) => T);
20
+ declare class HeadBuilder<TOutput = HeadElement[]> {
21
+ private metadataBase?;
22
+ private elements;
23
+ private adapter?;
24
+ /**
25
+ * Resolves a value that can be either static or a callback function receiving helper utilities.
26
+ *
27
+ * @param valueOrFn - Static value or function that returns the value
28
+ * @returns The resolved value
29
+ */
30
+ private parseValueOrFn;
31
+ /**
32
+ * Creates a new HeadBuilder instance for constructing HTML head elements with optional base URL resolution and output transformation.
33
+ *
34
+ * @param options - Configuration options
35
+ * @param options.metadataBase - Base URL for resolving relative URLs in metadata (Open Graph, canonical, etc.)
36
+ * @param options.adapter - Adapter to transform output into framework-specific format
37
+ *
38
+ * @example
39
+ * const head = new HeadBuilder({
40
+ * metadataBase: new URL('https://devsantara.com'),
41
+ * adapter: new ReactAdapter()
42
+ * })
43
+ * .addTitle('My Site')
44
+ * .build();
45
+ */
46
+ constructor(options?: {
47
+ metadataBase?: URL;
48
+ adapter?: HeadAdapter<TOutput>;
49
+ });
50
+ /**
51
+ * Resolves a relative or absolute URL into an absolute URL using the configured metadataBase.
52
+ *
53
+ * @param url - The URL to resolve
54
+ * @returns The resolved absolute URL as a string
55
+ */
56
+ private resolveUrl;
57
+ /**
58
+ * Adds a head element to the internal collection for later transformation.
59
+ *
60
+ * @param type - The HTML element type
61
+ * @param attributes - The element's attributes
62
+ * @returns The builder instance for method chaining
63
+ */
64
+ private addElement;
65
+ /**
66
+ * Adds a custom meta element with any valid attributes. Use this for meta tags without dedicated helper methods.
67
+ *
68
+ * @param attributes - The meta element attributes
69
+ * @returns The builder instance for method chaining
70
+ *
71
+ * @example
72
+ * new HeadBuilder()
73
+ * .addMeta({ name: 'theme-color', content: '#ffffff' })
74
+ * .build();
75
+ */
76
+ addMeta(attributes: HeadAttributeTypeMap['meta']): this;
77
+ /**
78
+ * Adds a custom link element with any valid attributes. Use this for link tags without dedicated helper methods.
79
+ *
80
+ * @param attributes - The link element attributes
81
+ * @returns The builder instance for method chaining
82
+ *
83
+ * @example
84
+ * new HeadBuilder()
85
+ * .addLink({ rel: 'preconnect', href: 'https://fonts.googleapis.com' })
86
+ * .build();
87
+ */
88
+ addLink(attributes: HeadAttributeTypeMap['link']): this;
89
+ /**
90
+ * Adds a custom script element with any valid attributes for external scripts or inline code.
91
+ *
92
+ * @param attributes - The script element attributes
93
+ * @returns The builder instance for method chaining
94
+ *
95
+ * @example
96
+ * new HeadBuilder()
97
+ * .addScript({ src: '/analytics.js', async: true })
98
+ * .build();
99
+ */
100
+ addScript(attributes: HeadAttributeTypeMap['script']): this;
101
+ /**
102
+ * Adds a custom style element with inline CSS.
103
+ *
104
+ * @param attributes - The style element attributes
105
+ * @returns The builder instance for method chaining
106
+ *
107
+ * @example
108
+ * new HeadBuilder()
109
+ * .addStyle({ children: 'body { margin: 0; padding: 0; }' })
110
+ * .build();
111
+ */
112
+ addStyle(attributes: HeadAttributeTypeMap['style']): this;
113
+ /**
114
+ * Adds a character encoding declaration to specify how the document should be interpreted.
115
+ *
116
+ * @param charSet - The character encoding (e.g., 'utf-8', 'iso-8859-1')
117
+ * @returns The builder instance for method chaining
118
+ *
119
+ * @example
120
+ * new HeadBuilder()
121
+ * .addCharSet('utf-8')
122
+ * .build();
123
+ */
124
+ addCharSet(charSet: CharSet): this;
125
+ /**
126
+ * Adds a color scheme preference indicating which color schemes the page supports for proper rendering.
127
+ *
128
+ * @param colorScheme - The supported color schemes (e.g., 'light', 'dark', 'light dark')
129
+ * @returns The builder instance for method chaining
130
+ *
131
+ * @example
132
+ * new HeadBuilder()
133
+ * .addColorScheme('light dark')
134
+ * .build();
135
+ */
136
+ addColorScheme(colorScheme: ColorScheme): this;
137
+ /**
138
+ * Adds a title element that appears in browser tabs, search results, and bookmarks.
139
+ *
140
+ * @param title - The document title text
141
+ * @returns The builder instance for method chaining
142
+ *
143
+ * @example
144
+ * new HeadBuilder()
145
+ * .addTitle('My Awesome Website')
146
+ * .build();
147
+ */
148
+ addTitle(title: string): this;
149
+ /**
150
+ * Adds viewport configuration for responsive web design and mobile optimization.
151
+ *
152
+ * @param options - Viewport settings (width, initial scale, zoom controls, etc.)
153
+ * @returns The builder instance for method chaining
154
+ *
155
+ * @example
156
+ * new HeadBuilder()
157
+ * .addViewport({ width: 'device-width', initialScale: 1 })
158
+ * .build();
159
+ */
160
+ addViewport(options: ViewportOptions): this;
161
+ /**
162
+ * Adds a description that appears in search engine results and social media previews.
163
+ *
164
+ * @param description - The page description text
165
+ * @returns The builder instance for method chaining
166
+ *
167
+ * @example
168
+ * new HeadBuilder()
169
+ * .addDescription('A comprehensive guide to web development')
170
+ * .build();
171
+ */
172
+ addDescription(description: string): this;
173
+ /**
174
+ * Adds a canonical URL to help search engines identify the preferred version of a page and prevent duplicate content issues.
175
+ *
176
+ * @param valueOrFn - The canonical URL or a callback function receiving helper utilities
177
+ * @returns The builder instance for method chaining
178
+ *
179
+ * @example
180
+ * new HeadBuilder({ metadataBase: new URL('https://devsantara.com') })
181
+ * .addCanonical((helper) => helper.resolveUrl('/page'))
182
+ * .build();
183
+ */
184
+ addCanonical(valueOrFn: BuilderOption<string | URL>): this;
185
+ /**
186
+ * Adds robots directives to control search engine crawling and indexing behavior.
187
+ *
188
+ * @param options - Robots configuration with index/follow booleans and custom directives
189
+ * @returns The builder instance for method chaining
190
+ *
191
+ * @example
192
+ * new HeadBuilder()
193
+ * .addRobots({ index: true, follow: true, 'max-snippet': 160 })
194
+ * .build();
195
+ */
196
+ addRobots(options: RobotsOptions): this;
197
+ /**
198
+ * Adds Open Graph metadata for rich social media previews on platforms like Facebook, LinkedIn, and Slack.
199
+ *
200
+ * @param valueOrFn - Open Graph configuration or a callback function receiving helper utilities
201
+ * @returns The builder instance for method chaining
202
+ *
203
+ * @example
204
+ * new HeadBuilder({ metadataBase: new URL('https://devsantara.com') })
205
+ * .addOpenGraph((helper) => ({
206
+ * title: 'My Page',
207
+ * url: helper.resolveUrl('/page'),
208
+ * image: { url: helper.resolveUrl('/og-image.jpg') }
209
+ * }))
210
+ * .build();
211
+ */
212
+ addOpenGraph(valueOrFn: BuilderOption<OpenGraphOptions>): this;
213
+ /**
214
+ * Adds Twitter Card metadata for rich previews when links are shared on Twitter/X.
215
+ *
216
+ * @param valueOrFn - Twitter Card configuration or a callback function receiving helper utilities
217
+ * @returns The builder instance for method chaining
218
+ *
219
+ * @example
220
+ * new HeadBuilder({ metadataBase: new URL('https://devsantara.com') })
221
+ * .addTwitter((helper) => ({
222
+ * card: { name: 'summary_large_image' },
223
+ * image: { url: helper.resolveUrl('/twitter-card.jpg') }
224
+ * }))
225
+ * .build();
226
+ */
227
+ addTwitter(valueOrFn: BuilderOption<TwitterOptions>): this;
228
+ /**
229
+ * Adds alternate language/locale versions of the page to help search engines serve the correct localized content to users.
230
+ *
231
+ * @param valueOrFn - Locale-to-URL mapping or a callback function receiving helper utilities
232
+ * @returns The builder instance for method chaining
233
+ *
234
+ * @example
235
+ * new HeadBuilder({ metadataBase: new URL('https://example.com') })
236
+ * .addAlternateLocale((helper) => ({
237
+ * 'en-US': helper.resolveUrl('/en'),
238
+ * 'fr-FR': helper.resolveUrl('/fr'),
239
+ * 'x-default': helper.resolveUrl('/')
240
+ * }))
241
+ * .build();
242
+ */
243
+ addAlternateLocale<TLocale extends string = string>(valueOrFn: BuilderOption<AlternateLocaleOptions<TLocale>>): this;
244
+ /**
245
+ * Adds a web app manifest link that defines how your application appears when installed on devices.
246
+ *
247
+ * @param valueOrFn - The manifest URL or a callback function receiving helper utilities
248
+ * @returns The builder instance for method chaining
249
+ *
250
+ * @example
251
+ * new HeadBuilder({ metadataBase: new URL('https://devsantara.com') })
252
+ * .addManifest((helper) => helper.resolveUrl('/manifest.json'))
253
+ * .build();
254
+ */
255
+ addManifest(valueOrFn: BuilderOption<string | URL>): this;
256
+ /**
257
+ * Adds an external CSS stylesheet link to the page.
258
+ *
259
+ * @param href - The stylesheet URL
260
+ * @param options - Additional link attributes (media queries, integrity, etc.)
261
+ * @returns The builder instance for method chaining
262
+ *
263
+ * @example
264
+ * new HeadBuilder()
265
+ * .addStylesheet('/styles.css', { media: 'print' })
266
+ * .build();
267
+ */
268
+ addStylesheet(href: string | URL, options?: StylesheetOptions): this;
269
+ /**
270
+ * Adds a favicon or app icon using preset types or custom rel values.
271
+ *
272
+ * @param preset - Icon type ('icon', 'apple', 'shortcut', or custom string)
273
+ * @param valueOrFn - Icon configuration or a callback function receiving helper utilities
274
+ * @returns The builder instance for method chaining
275
+ *
276
+ * @example
277
+ * new HeadBuilder({ metadataBase: new URL('https://devsantara.com') })
278
+ * .addIcon('apple', (helper) => ({
279
+ * href: helper.resolveUrl('/apple-icon.png'),
280
+ * sizes: '180x180'
281
+ * }))
282
+ * .build();
283
+ */
284
+ addIcon(preset: IconPreset, valueOrFn: BuilderOption<IconOptions>): this;
285
+ /**
286
+ * Builds and returns the final head configuration. Returns adapted output if an adapter was provided, otherwise returns `HeadElement[]`.
287
+ *
288
+ * @returns The head configuration in the target format
289
+ */
290
+ build(): TOutput;
291
+ }
292
+ //#endregion
293
+ export { AlternateLocaleOptions, CharSet, ColorScheme, HeadAdapter, HeadAttributeTypeMap, HeadBuilder, HeadElement, HeadLinkAttributes, HeadMetaAttributes, HeadScriptAttributes, HeadStyleAttributes, HeadTitleAttributes, IconOptions, IconPreset, OpenGraphOptions, RobotsOptions, StylesheetOptions, TwitterOptions, ViewportOptions };