@datocms/astro 0.1.11 → 0.1.13

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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@datocms/astro",
3
3
  "description": "A set of components and utilities to work faster with DatoCMS in Astro projects.",
4
4
  "type": "module",
5
- "version": "0.1.11",
5
+ "version": "0.1.13",
6
6
  "sideEffects": false,
7
7
  "repository": {
8
8
  "type": "git",
@@ -24,7 +24,8 @@
24
24
  "./useVideoPlayer": "./src/useVideoPlayer/index.ts",
25
25
  "./VideoPlayer": "./src/VideoPlayer/index.ts",
26
26
  "./Image": "./src/Image/index.ts",
27
- "./StructuredText": "./src/StructuredText/index.ts"
27
+ "./StructuredText": "./src/StructuredText/index.ts",
28
+ "./Seo": "./src/Seo/index.ts"
28
29
  },
29
30
  "engines": {
30
31
  "node": ">=18.0.0"
@@ -0,0 +1,12 @@
1
+ ---
2
+ import { renderMetaTagsToString } from "./renderMetaTagsToString";
3
+ import type { SeoOrFaviconTag, TitleMetaLinkTag } from "./types";
4
+
5
+ interface Props {
6
+ data: TitleMetaLinkTag[] | SeoOrFaviconTag[];
7
+ }
8
+
9
+ const { data } = Astro.props;
10
+ ---
11
+
12
+ <Fragment set:html={renderMetaTagsToString(data)} />
@@ -0,0 +1,5 @@
1
+ import Seo from "./Seo.astro";
2
+ export * from "./renderMetaTagsToString.js";
3
+ export * from "./types.js";
4
+
5
+ export { Seo };
@@ -0,0 +1,23 @@
1
+ import type { SeoOrFaviconTag, TitleMetaLinkTag } from './types.js';
2
+
3
+ export function renderMetaTagsToString(
4
+ data: TitleMetaLinkTag[] | SeoOrFaviconTag[],
5
+ ): string {
6
+ return data
7
+ .map((tag) => {
8
+ if (tag.tag === 'title') {
9
+ return `<title>${tag.content}</title>`;
10
+ }
11
+
12
+ const serializedAttrs = [];
13
+
14
+ for (const key in tag.attributes) {
15
+ if (Object.prototype.hasOwnProperty.call(tag.attributes, key)) {
16
+ serializedAttrs.push(`${key}="${(tag.attributes as any)[key]}"`);
17
+ }
18
+ }
19
+
20
+ return `<${tag.tag} ${serializedAttrs.join(' ')} />`;
21
+ })
22
+ .join('\n');
23
+ }
@@ -0,0 +1,113 @@
1
+ export interface TitleMetaLinkTag {
2
+ /** the tag for the meta information */
3
+ tag: string;
4
+ /** the inner content of the meta tag */
5
+ content?: string | null | undefined;
6
+ /** the HTML attributes to attach to the meta tag */
7
+ attributes?: Record<string, string> | null | undefined;
8
+ }
9
+
10
+ export interface SeoTitleTag {
11
+ tag: 'title';
12
+ content: string | null;
13
+ attributes?: null;
14
+ }
15
+
16
+ export interface RegularMetaAttributes {
17
+ name: string;
18
+ content: string;
19
+ }
20
+
21
+ export interface OgMetaAttributes {
22
+ property: string;
23
+ content: string;
24
+ }
25
+
26
+ export interface SeoMetaTag {
27
+ tag: 'meta';
28
+ content?: null;
29
+ attributes: RegularMetaAttributes | OgMetaAttributes;
30
+ }
31
+
32
+ export interface FaviconAttributes {
33
+ sizes: string;
34
+ type: string;
35
+ rel: string;
36
+ href: string;
37
+ }
38
+
39
+ export interface AppleTouchIconAttributes {
40
+ sizes: string;
41
+ rel: 'apple-touch-icon';
42
+ href: string;
43
+ }
44
+
45
+ export interface SeoLinkTag {
46
+ tag: 'link';
47
+ content?: null;
48
+ attributes: FaviconAttributes | AppleTouchIconAttributes;
49
+ }
50
+
51
+ export type SeoTag = SeoTitleTag | SeoMetaTag;
52
+ export type FaviconTag = SeoMetaTag | SeoLinkTag;
53
+ export type SeoOrFaviconTag = SeoTag | FaviconTag;
54
+
55
+ export const isSeoTitleTag = (tag: any): tag is SeoTitleTag =>
56
+ 'tag' in tag && tag.tag === 'title' && !tag.attributes;
57
+
58
+ export const isSeoTag = (tag: any): tag is SeoTag =>
59
+ isSeoTitleTag(tag) || isSeoMetaTag(tag);
60
+
61
+ export const isFaviconAttributes = (tag: any): tag is FaviconAttributes =>
62
+ 'sizes' in tag &&
63
+ typeof tag.sizes === 'string' &&
64
+ 'type' in tag &&
65
+ typeof tag.type === 'string' &&
66
+ 'rel' in tag &&
67
+ typeof tag.rel === 'string' &&
68
+ 'href' in tag &&
69
+ typeof tag.href === 'string';
70
+
71
+ export const isAppleTouchIconAttributes = (
72
+ tag: any,
73
+ ): tag is AppleTouchIconAttributes =>
74
+ 'sizes' in tag &&
75
+ typeof tag.sizes === 'string' &&
76
+ 'rel' in tag &&
77
+ tag.rel === 'apple-touch-icon' &&
78
+ 'href' in tag &&
79
+ typeof tag.href === 'string';
80
+
81
+ export const isSeoLinkTag = (tag: any): tag is SeoLinkTag =>
82
+ 'tag' in tag &&
83
+ tag.tag === 'link' &&
84
+ !tag.content &&
85
+ (isFaviconAttributes(tag.attributes) ||
86
+ isAppleTouchIconAttributes(tag.attributes));
87
+
88
+ export const isFaviconTag = (tag: any): tag is FaviconTag =>
89
+ isSeoMetaTag(tag) || isSeoLinkTag(tag);
90
+
91
+ export const isSeoOrFaviconTag = (
92
+ seoOrFaviconTag: TitleMetaLinkTag | SeoOrFaviconTag,
93
+ ): seoOrFaviconTag is SeoOrFaviconTag =>
94
+ isSeoTag(seoOrFaviconTag) || isFaviconTag(seoOrFaviconTag);
95
+
96
+ export const isRegularMetaAttributes = (
97
+ attributes: RegularMetaAttributes | OgMetaAttributes,
98
+ ): attributes is RegularMetaAttributes =>
99
+ 'name' in attributes && 'content' in attributes;
100
+
101
+ export const isOgMetaAttributes = (
102
+ attributes: RegularMetaAttributes | OgMetaAttributes,
103
+ ): attributes is OgMetaAttributes =>
104
+ 'property' in attributes && 'content' in attributes;
105
+
106
+ export const isSeoMetaTag = (
107
+ seoMetaTag: SeoOrFaviconTag,
108
+ ): seoMetaTag is SeoMetaTag =>
109
+ 'tag' in seoMetaTag &&
110
+ seoMetaTag.tag === 'meta' &&
111
+ !seoMetaTag.content &&
112
+ (isRegularMetaAttributes(seoMetaTag.attributes) ||
113
+ isOgMetaAttributes(seoMetaTag.attributes));
@@ -1,13 +1,13 @@
1
1
  ---
2
- import type { Code } from "datocms-structured-text-utils";
2
+ import { Code as AstroCode } from "astro:components";
3
+ import type { Code as CodeNode } from "datocms-structured-text-utils";
3
4
 
4
5
  interface Props {
5
- node: Code;
6
+ // https://www.datocms.com/docs/structured-text/dast#code
7
+ node: CodeNode;
6
8
  }
7
9
 
8
10
  const { node } = Astro.props;
9
-
10
- const { code, language } = node;
11
11
  ---
12
12
 
13
- <pre class={language}>{code}</pre>
13
+ <AstroCode code={node.code} lang={node.language as "js"} />