unhead 0.1.4 → 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.
package/dist/index.cjs CHANGED
@@ -26,10 +26,11 @@ const TagConfigKeys = ["tagPosition", "tagPriority", "tagDuplicateStrategy"];
26
26
 
27
27
  function normaliseTag(tagName, input) {
28
28
  const tag = { tag: tagName, props: {} };
29
- if (tagName === "title")
30
- tag.children = String(input);
31
- else
32
- tag.props = normaliseProps({ ...input });
29
+ if (tagName === "title" || tagName === "titleTemplate") {
30
+ tag.children = input;
31
+ return tag;
32
+ }
33
+ tag.props = normaliseProps({ ...input });
33
34
  ["children", "innerHtml", "innerHTML"].forEach((key) => {
34
35
  if (typeof tag.props[key] !== "undefined") {
35
36
  tag.children = tag.props[key];
@@ -125,7 +126,7 @@ function resolveTitleTemplateFromTags(tags) {
125
126
  if (newTitle !== null) {
126
127
  tags[titleIdx].children = newTitle || tags[titleIdx].children;
127
128
  } else {
128
- tags = tags.filter((_, i) => i !== titleIdx);
129
+ delete tags[titleIdx];
129
130
  }
130
131
  } else if (titleTemplateIdx !== -1) {
131
132
  const newTitle = renderTitleTemplate(
@@ -136,9 +137,10 @@ function resolveTitleTemplateFromTags(tags) {
136
137
  tags[titleTemplateIdx].tag = "title";
137
138
  }
138
139
  }
139
- if (titleTemplateIdx !== -1)
140
- tags = tags.filter((_, i) => i !== titleTemplateIdx);
141
- return tags;
140
+ if (titleTemplateIdx !== -1) {
141
+ delete tags[titleTemplateIdx];
142
+ }
143
+ return tags.filter(Boolean);
142
144
  }
143
145
 
144
146
  const DedupesTagsPlugin = (options) => {
@@ -230,6 +232,23 @@ const TitleTemplatePlugin = () => {
230
232
  });
231
233
  };
232
234
 
235
+ function defineHeadPlugin(plugin) {
236
+ return plugin;
237
+ }
238
+
239
+ const DeprecatedTagAttrPlugin = () => {
240
+ return defineHeadPlugin({
241
+ hooks: {
242
+ "tag:normalise": function({ tag }) {
243
+ if (tag.props.body) {
244
+ tag.tagPosition = "bodyClose";
245
+ delete tag.props.body;
246
+ }
247
+ }
248
+ }
249
+ });
250
+ };
251
+
233
252
  function hashCode(s) {
234
253
  let h = 9;
235
254
  for (let i = 0; i < s.length; )
@@ -314,7 +333,7 @@ function normaliseEntryTags(e) {
314
333
  });
315
334
  }
316
335
 
317
- async function createHead(options = {}) {
336
+ function createHead(options = {}) {
318
337
  let entries = [];
319
338
  let _sde = {};
320
339
  let entryId = 0;
@@ -322,12 +341,14 @@ async function createHead(options = {}) {
322
341
  if (options.hooks)
323
342
  hooks.addHooks(options.hooks);
324
343
  const plugins = [
344
+ DeprecatedTagAttrPlugin(),
325
345
  DedupesTagsPlugin(),
326
346
  SortTagsPlugin(),
327
347
  TitleTemplatePlugin()
328
348
  ];
329
349
  plugins.push(...options.plugins || []);
330
350
  plugins.forEach((plugin) => hooks.addHooks(plugin.hooks || {}));
351
+ const triggerUpdate = () => hooks.callHook("entries:updated", head);
331
352
  const head = {
332
353
  _removeQueuedSideEffect(key) {
333
354
  delete _sde[key];
@@ -350,28 +371,29 @@ async function createHead(options = {}) {
350
371
  _sde: {},
351
372
  ...options2
352
373
  });
353
- hooks.callHook("entries:updated", head);
374
+ triggerUpdate();
375
+ const queueSideEffects = (e) => {
376
+ _sde = { ..._sde, ...e._sde || {} };
377
+ e._sde = {};
378
+ triggerUpdate();
379
+ };
354
380
  return {
355
381
  dispose() {
356
382
  entries = entries.filter((e) => {
357
383
  if (e._i !== _i)
358
384
  return true;
359
- _sde = { ..._sde, ...e._sde || {} };
360
- e._sde = {};
385
+ queueSideEffects(e);
361
386
  return false;
362
387
  });
363
- hooks.callHook("entries:updated", head);
364
388
  },
365
389
  patch(input2) {
366
390
  entries = entries.map((e) => {
367
391
  if (e._i === _i) {
368
- _sde = { ..._sde, ...e._sde || {} };
369
- e._sde = {};
392
+ queueSideEffects(e);
370
393
  e.input = e._i === _i ? input2 : e.input;
371
394
  }
372
395
  return e;
373
396
  });
374
- hooks.callHook("entries:updated", head);
375
397
  }
376
398
  };
377
399
  },
@@ -389,16 +411,13 @@ async function createHead(options = {}) {
389
411
  return resolveCtx.tags;
390
412
  }
391
413
  };
392
- await head.hooks.callHook("init", head);
414
+ head.hooks.callHook("init", head);
393
415
  setActiveHead(head);
394
416
  return head;
395
417
  }
396
418
 
397
- function defineHeadPlugin(plugin) {
398
- return plugin;
399
- }
400
-
401
419
  exports.DedupesTagsPlugin = DedupesTagsPlugin;
420
+ exports.DeprecatedTagAttrPlugin = DeprecatedTagAttrPlugin;
402
421
  exports.HydratesStatePlugin = HydratesStatePlugin;
403
422
  exports.SortTagsPlugin = SortTagsPlugin;
404
423
  exports.TitleTemplatePlugin = TitleTemplatePlugin;
package/dist/index.d.ts CHANGED
@@ -10,6 +10,8 @@ declare const SortTagsPlugin: () => _unhead_schema.HeadPlugin;
10
10
 
11
11
  declare const TitleTemplatePlugin: () => _unhead_schema.HeadPlugin;
12
12
 
13
+ declare const DeprecatedTagAttrPlugin: () => _unhead_schema.HeadPlugin;
14
+
13
15
  declare const HydratesStatePlugin: () => _unhead_schema.HeadPlugin;
14
16
 
15
17
  declare function useHead<T extends Head>(input: T, options?: HeadEntryOptions): void;
@@ -29,7 +31,7 @@ declare let activeHead: HeadClient<any> | undefined;
29
31
  declare const setActiveHead: <T extends HeadClient<_unhead_schema.Head<_unhead_schema.SchemaAugmentations>>>(head: T | undefined) => T | undefined;
30
32
  declare const getActiveHead: <T extends HeadClient<_unhead_schema.Head<_unhead_schema.SchemaAugmentations>>>() => T;
31
33
 
32
- declare function createHead<T extends {} = Head>(options?: CreateHeadOptions): Promise<HeadClient<T>>;
34
+ declare function createHead<T extends {} = Head>(options?: CreateHeadOptions): HeadClient<T>;
33
35
 
34
36
  declare function defineHeadPlugin(plugin: HeadPlugin): HeadPlugin;
35
37
 
@@ -38,4 +40,4 @@ declare function normaliseEntryTags<T extends {} = Head>(e: HeadEntry<T>): HeadT
38
40
  declare type Arrayable<T> = T | Array<T>;
39
41
  declare function asArray<T>(value: Arrayable<T>): T[];
40
42
 
41
- export { Arrayable, DedupesTagsPlugin, DedupesTagsPluginOptions, HydratesStatePlugin, SortTagsPlugin, TitleTemplatePlugin, activeHead, asArray, createHead, defineHeadPlugin, getActiveHead, normaliseEntryTags, setActiveHead, useBase, useBodyAttrs, useHead, useHtmlAttrs, useLink, useMeta, useNoscript, useScript, useServerHead, useStyle, useTitle, useTitleTemplate };
43
+ export { Arrayable, DedupesTagsPlugin, DedupesTagsPluginOptions, DeprecatedTagAttrPlugin, HydratesStatePlugin, SortTagsPlugin, TitleTemplatePlugin, activeHead, asArray, createHead, defineHeadPlugin, getActiveHead, normaliseEntryTags, setActiveHead, useBase, useBodyAttrs, useHead, useHtmlAttrs, useLink, useMeta, useNoscript, useScript, useServerHead, useStyle, useTitle, useTitleTemplate };
package/dist/index.mjs CHANGED
@@ -24,10 +24,11 @@ const TagConfigKeys = ["tagPosition", "tagPriority", "tagDuplicateStrategy"];
24
24
 
25
25
  function normaliseTag(tagName, input) {
26
26
  const tag = { tag: tagName, props: {} };
27
- if (tagName === "title")
28
- tag.children = String(input);
29
- else
30
- tag.props = normaliseProps({ ...input });
27
+ if (tagName === "title" || tagName === "titleTemplate") {
28
+ tag.children = input;
29
+ return tag;
30
+ }
31
+ tag.props = normaliseProps({ ...input });
31
32
  ["children", "innerHtml", "innerHTML"].forEach((key) => {
32
33
  if (typeof tag.props[key] !== "undefined") {
33
34
  tag.children = tag.props[key];
@@ -123,7 +124,7 @@ function resolveTitleTemplateFromTags(tags) {
123
124
  if (newTitle !== null) {
124
125
  tags[titleIdx].children = newTitle || tags[titleIdx].children;
125
126
  } else {
126
- tags = tags.filter((_, i) => i !== titleIdx);
127
+ delete tags[titleIdx];
127
128
  }
128
129
  } else if (titleTemplateIdx !== -1) {
129
130
  const newTitle = renderTitleTemplate(
@@ -134,9 +135,10 @@ function resolveTitleTemplateFromTags(tags) {
134
135
  tags[titleTemplateIdx].tag = "title";
135
136
  }
136
137
  }
137
- if (titleTemplateIdx !== -1)
138
- tags = tags.filter((_, i) => i !== titleTemplateIdx);
139
- return tags;
138
+ if (titleTemplateIdx !== -1) {
139
+ delete tags[titleTemplateIdx];
140
+ }
141
+ return tags.filter(Boolean);
140
142
  }
141
143
 
142
144
  const DedupesTagsPlugin = (options) => {
@@ -228,6 +230,23 @@ const TitleTemplatePlugin = () => {
228
230
  });
229
231
  };
230
232
 
233
+ function defineHeadPlugin(plugin) {
234
+ return plugin;
235
+ }
236
+
237
+ const DeprecatedTagAttrPlugin = () => {
238
+ return defineHeadPlugin({
239
+ hooks: {
240
+ "tag:normalise": function({ tag }) {
241
+ if (tag.props.body) {
242
+ tag.tagPosition = "bodyClose";
243
+ delete tag.props.body;
244
+ }
245
+ }
246
+ }
247
+ });
248
+ };
249
+
231
250
  function hashCode(s) {
232
251
  let h = 9;
233
252
  for (let i = 0; i < s.length; )
@@ -312,7 +331,7 @@ function normaliseEntryTags(e) {
312
331
  });
313
332
  }
314
333
 
315
- async function createHead(options = {}) {
334
+ function createHead(options = {}) {
316
335
  let entries = [];
317
336
  let _sde = {};
318
337
  let entryId = 0;
@@ -320,12 +339,14 @@ async function createHead(options = {}) {
320
339
  if (options.hooks)
321
340
  hooks.addHooks(options.hooks);
322
341
  const plugins = [
342
+ DeprecatedTagAttrPlugin(),
323
343
  DedupesTagsPlugin(),
324
344
  SortTagsPlugin(),
325
345
  TitleTemplatePlugin()
326
346
  ];
327
347
  plugins.push(...options.plugins || []);
328
348
  plugins.forEach((plugin) => hooks.addHooks(plugin.hooks || {}));
349
+ const triggerUpdate = () => hooks.callHook("entries:updated", head);
329
350
  const head = {
330
351
  _removeQueuedSideEffect(key) {
331
352
  delete _sde[key];
@@ -348,28 +369,29 @@ async function createHead(options = {}) {
348
369
  _sde: {},
349
370
  ...options2
350
371
  });
351
- hooks.callHook("entries:updated", head);
372
+ triggerUpdate();
373
+ const queueSideEffects = (e) => {
374
+ _sde = { ..._sde, ...e._sde || {} };
375
+ e._sde = {};
376
+ triggerUpdate();
377
+ };
352
378
  return {
353
379
  dispose() {
354
380
  entries = entries.filter((e) => {
355
381
  if (e._i !== _i)
356
382
  return true;
357
- _sde = { ..._sde, ...e._sde || {} };
358
- e._sde = {};
383
+ queueSideEffects(e);
359
384
  return false;
360
385
  });
361
- hooks.callHook("entries:updated", head);
362
386
  },
363
387
  patch(input2) {
364
388
  entries = entries.map((e) => {
365
389
  if (e._i === _i) {
366
- _sde = { ..._sde, ...e._sde || {} };
367
- e._sde = {};
390
+ queueSideEffects(e);
368
391
  e.input = e._i === _i ? input2 : e.input;
369
392
  }
370
393
  return e;
371
394
  });
372
- hooks.callHook("entries:updated", head);
373
395
  }
374
396
  };
375
397
  },
@@ -387,13 +409,9 @@ async function createHead(options = {}) {
387
409
  return resolveCtx.tags;
388
410
  }
389
411
  };
390
- await head.hooks.callHook("init", head);
412
+ head.hooks.callHook("init", head);
391
413
  setActiveHead(head);
392
414
  return head;
393
415
  }
394
416
 
395
- function defineHeadPlugin(plugin) {
396
- return plugin;
397
- }
398
-
399
- export { DedupesTagsPlugin, HydratesStatePlugin, SortTagsPlugin, TitleTemplatePlugin, activeHead, asArray, createHead, defineHeadPlugin, getActiveHead, normaliseEntryTags, setActiveHead, useBase, useBodyAttrs, useHead, useHtmlAttrs, useLink, useMeta, useNoscript, useScript, useServerHead, useStyle, useTitle, useTitleTemplate };
417
+ export { DedupesTagsPlugin, DeprecatedTagAttrPlugin, HydratesStatePlugin, SortTagsPlugin, TitleTemplatePlugin, activeHead, asArray, createHead, defineHeadPlugin, getActiveHead, normaliseEntryTags, setActiveHead, useBase, useBodyAttrs, useHead, useHtmlAttrs, useLink, useMeta, useNoscript, useScript, useServerHead, useStyle, useTitle, useTitleTemplate };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "unhead",
3
3
  "type": "module",
4
- "version": "0.1.4",
4
+ "version": "0.2.0",
5
5
  "packageManager": "pnpm@7.14.0",
6
6
  "author": "Harlan Wilton <harlan@harlanzw.com>",
7
7
  "license": "MIT",
@@ -30,11 +30,11 @@
30
30
  "dist"
31
31
  ],
32
32
  "dependencies": {
33
- "@unhead/schema": "0.1.4",
33
+ "@unhead/schema": "0.2.0",
34
34
  "hookable": "^5.4.1"
35
35
  },
36
36
  "devDependencies": {
37
- "zhead": "1.0.0-beta.5"
37
+ "zhead": "1.0.0-beta.8"
38
38
  },
39
39
  "scripts": {
40
40
  "build": "unbuild .",