@arraypress/seo 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 David Sherlock / ArrayPress Limited
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # @arraypress/seo
2
+
3
+ HTML meta tag builder for SEO. Open Graph, Twitter Cards, canonical URLs, robots directives, hreflang, and site verification. Zero dependencies.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @arraypress/seo
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```js
14
+ import { buildHead, injectHead, robotsTxt } from '@arraypress/seo';
15
+
16
+ const head = buildHead({
17
+ title: 'My Page',
18
+ description: 'A great page',
19
+ url: 'https://example.com/page',
20
+ image: 'https://example.com/og.png',
21
+ siteName: 'Example',
22
+ twitterSite: '@example',
23
+ });
24
+
25
+ const html = injectHead('<html><head></head><body>...</body></html>', head);
26
+
27
+ const robots = robotsTxt({
28
+ sitemapUrl: 'https://example.com/sitemap.xml',
29
+ disallow: ['/admin'],
30
+ });
31
+ ```
32
+
33
+ ## API
34
+
35
+ ### `buildHead(options?)`
36
+
37
+ Build a complete `<head>` HTML string from options.
38
+
39
+ | Option | Type | Description |
40
+ |--------|------|-------------|
41
+ | `title` | `string` | Page title |
42
+ | `description` | `string` | Meta description |
43
+ | `image` | `string` | OG/Twitter image URL |
44
+ | `url` | `string` | Canonical URL |
45
+ | `type` | `string` | OG type (default: `website`) |
46
+ | `robots` | `string` | Robots directive |
47
+ | `siteName` | `string` | OG site name |
48
+ | `twitterCard` | `string` | Twitter card type (auto-detected from image) |
49
+ | `twitterSite` | `string` | Twitter @username for site |
50
+ | `twitterCreator` | `string` | Twitter @username for author |
51
+ | `locale` | `string` | OG locale |
52
+ | `articlePublished` | `string` | Article published date (ISO 8601) |
53
+ | `articleModified` | `string` | Article modified date (ISO 8601) |
54
+ | `articleAuthor` | `string` | Article author name |
55
+ | `verification` | `object` | Site verification codes (`google`, `bing`, `pinterest`, `yandex`) |
56
+ | `hreflang` | `array` | Alternate language links (`{ lang, url }`) |
57
+ | `jsonLd` | `object | array` | JSON-LD structured data to inject |
58
+
59
+ ### `injectHead(html, headHtml)`
60
+
61
+ Inject head HTML into an HTML document string. Replaces any existing `<title>` and inserts before `</head>`.
62
+
63
+ ### `robotsTxt(options?)`
64
+
65
+ Generate robots.txt content.
66
+
67
+ | Option | Type | Description |
68
+ |--------|------|-------------|
69
+ | `sitemapUrl` | `string` | Sitemap URL to include |
70
+ | `allow` | `string[]` | Allowed paths |
71
+ | `disallow` | `string[]` | Disallowed paths |
72
+ | `crawlDelay` | `number` | Crawl delay in seconds |
73
+ | `customRules` | `string[]` | Additional raw lines |
74
+
75
+ ### `escapeHtml(str)`
76
+
77
+ Escape HTML entities (`&`, `"`, `<`, `>`) in a string. Returns empty string for falsy input.
78
+
79
+ ## License
80
+
81
+ MIT
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@arraypress/seo",
3
+ "version": "1.0.0",
4
+ "description": "HTML meta tag builder for SEO. Open Graph, Twitter Cards, canonical URLs, robots directives, hreflang, and site verification.",
5
+ "type": "module",
6
+ "main": "src/index.js",
7
+ "types": "src/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./src/index.js",
11
+ "types": "./src/index.d.ts"
12
+ }
13
+ },
14
+ "files": ["src"],
15
+ "scripts": {
16
+ "test": "node --test tests/seo.test.js"
17
+ },
18
+ "keywords": ["seo", "meta", "open-graph", "twitter-card", "canonical", "robots", "hreflang", "json-ld", "head"],
19
+ "author": "David Sherlock",
20
+ "license": "MIT",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "https://github.com/arraypress/seo"
24
+ }
25
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,44 @@
1
+ export interface BuildHeadOptions {
2
+ title?: string;
3
+ description?: string;
4
+ image?: string;
5
+ url?: string;
6
+ type?: string;
7
+ robots?: string;
8
+ siteName?: string;
9
+ twitterCard?: string;
10
+ twitterSite?: string;
11
+ twitterCreator?: string;
12
+ locale?: string;
13
+ articlePublished?: string;
14
+ articleModified?: string;
15
+ articleAuthor?: string;
16
+ verification?: {
17
+ google?: string;
18
+ bing?: string;
19
+ pinterest?: string;
20
+ yandex?: string;
21
+ };
22
+ hreflang?: Array<{ lang: string; url: string }>;
23
+ jsonLd?: object | object[];
24
+ }
25
+
26
+ /** Build a complete <head> HTML string from options. */
27
+ export function buildHead(options?: BuildHeadOptions): string;
28
+
29
+ /** Inject head HTML into an HTML document (before </head>). Replaces existing <title>. */
30
+ export function injectHead(html: string, headHtml: string): string;
31
+
32
+ export interface RobotsTxtOptions {
33
+ sitemapUrl?: string;
34
+ allow?: string[];
35
+ disallow?: string[];
36
+ crawlDelay?: number;
37
+ customRules?: string[];
38
+ }
39
+
40
+ /** Generate robots.txt content. */
41
+ export function robotsTxt(options?: RobotsTxtOptions): string;
42
+
43
+ /** Escape HTML entities in a string. */
44
+ export function escapeHtml(str: string): string;
package/src/index.js ADDED
@@ -0,0 +1,196 @@
1
+ /**
2
+ * @arraypress/seo
3
+ *
4
+ * HTML meta tag builder for SEO. Open Graph, Twitter Cards, canonical URLs,
5
+ * robots directives, hreflang, and site verification.
6
+ *
7
+ * Zero dependencies. Works everywhere.
8
+ *
9
+ * @module @arraypress/seo
10
+ */
11
+
12
+ /**
13
+ * Escape HTML entities in a string for safe use in attribute values.
14
+ *
15
+ * Replaces `&`, `"`, `<`, and `>` with their HTML entity equivalents.
16
+ *
17
+ * @param {string} str - The string to escape.
18
+ * @returns {string} The escaped string, or empty string if input is falsy.
19
+ *
20
+ * @example
21
+ * escapeHtml('Tom & Jerry');
22
+ * // => 'Tom &amp; Jerry'
23
+ *
24
+ * @example
25
+ * escapeHtml('<script>alert("xss")</script>');
26
+ * // => '&lt;script&gt;alert(&quot;xss&quot;)&lt;/script&gt;'
27
+ */
28
+ function esc(str) {
29
+ if (!str) return '';
30
+ return String(str).replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
31
+ }
32
+
33
+ /**
34
+ * Build a complete `<head>` HTML string from SEO options.
35
+ *
36
+ * Generates `<title>`, canonical URL, meta description, robots directive,
37
+ * Open Graph tags, Twitter Card tags, site verification meta tags, hreflang
38
+ * alternate links, and inline JSON-LD script blocks.
39
+ *
40
+ * @param {Object} [options={}] - SEO configuration.
41
+ * @param {string} [options.title] - Page title (used in `<title>`, OG, and Twitter).
42
+ * @param {string} [options.description] - Meta description (used in meta, OG, and Twitter).
43
+ * @param {string} [options.image] - Image URL for OG and Twitter Card.
44
+ * @param {string} [options.url] - Canonical page URL.
45
+ * @param {string} [options.type='website'] - Open Graph type (e.g. 'website', 'article').
46
+ * @param {string} [options.robots] - Robots directive (e.g. 'noindex, nofollow').
47
+ * @param {string} [options.siteName] - Site name for `og:site_name`.
48
+ * @param {string} [options.twitterCard] - Twitter card type. Defaults to 'summary_large_image' when image is present.
49
+ * @param {string} [options.twitterSite] - Twitter `@username` for the site.
50
+ * @param {string} [options.twitterCreator] - Twitter `@username` for the content creator.
51
+ * @param {string} [options.locale] - Locale string (e.g. 'en_US') for `og:locale`.
52
+ * @param {string} [options.articlePublished] - ISO 8601 date for `article:published_time`.
53
+ * @param {string} [options.articleModified] - ISO 8601 date for `article:modified_time`.
54
+ * @param {string} [options.articleAuthor] - Author name or URL for `article:author`.
55
+ * @param {Object} [options.verification={}] - Site verification tokens.
56
+ * @param {string} [options.verification.google] - Google Search Console verification token.
57
+ * @param {string} [options.verification.bing] - Bing Webmaster verification token.
58
+ * @param {string} [options.verification.pinterest] - Pinterest domain verification token.
59
+ * @param {string} [options.verification.yandex] - Yandex Webmaster verification token.
60
+ * @param {Array<{lang: string, url: string}>} [options.hreflang=[]] - Alternate language links.
61
+ * @param {Object|Object[]} [options.jsonLd] - JSON-LD structured data object(s) to embed.
62
+ * @returns {string} HTML string suitable for injection into `<head>`.
63
+ *
64
+ * @example
65
+ * const head = buildHead({
66
+ * title: 'My Product',
67
+ * description: 'The best product ever.',
68
+ * url: 'https://example.com/products/my-product',
69
+ * image: 'https://example.com/images/product.jpg',
70
+ * siteName: 'My Store',
71
+ * });
72
+ *
73
+ * @example
74
+ * // With hreflang and JSON-LD
75
+ * const head = buildHead({
76
+ * title: 'About Us',
77
+ * url: 'https://example.com/about',
78
+ * hreflang: [
79
+ * { lang: 'en', url: 'https://example.com/about' },
80
+ * { lang: 'es', url: 'https://example.com/es/about' },
81
+ * ],
82
+ * jsonLd: { '@context': 'https://schema.org', '@type': 'Organization', name: 'Acme' },
83
+ * });
84
+ */
85
+ export function buildHead(options = {}) {
86
+ const { title, description, image, url, type = 'website', robots, siteName,
87
+ twitterCard, twitterSite, twitterCreator,
88
+ locale, articlePublished, articleModified, articleAuthor,
89
+ verification = {}, hreflang = [], jsonLd } = options;
90
+
91
+ const parts = [];
92
+
93
+ if (title) parts.push(`<title>${esc(title)}</title>`);
94
+ if (url) parts.push(`<link rel="canonical" href="${esc(url)}">`);
95
+ if (description) parts.push(`<meta name="description" content="${esc(description)}">`);
96
+ if (robots) parts.push(`<meta name="robots" content="${esc(robots)}">`);
97
+
98
+ // Open Graph
99
+ if (title) parts.push(`<meta property="og:title" content="${esc(title)}">`);
100
+ parts.push(`<meta property="og:type" content="${esc(type)}">`);
101
+ if (url) parts.push(`<meta property="og:url" content="${esc(url)}">`);
102
+ if (description) parts.push(`<meta property="og:description" content="${esc(description)}">`);
103
+ if (image) parts.push(`<meta property="og:image" content="${esc(image)}">`);
104
+ if (siteName) parts.push(`<meta property="og:site_name" content="${esc(siteName)}">`);
105
+ if (locale) parts.push(`<meta property="og:locale" content="${esc(locale)}">`);
106
+
107
+ // Article-specific OG
108
+ if (articlePublished) parts.push(`<meta property="article:published_time" content="${esc(articlePublished)}">`);
109
+ if (articleModified) parts.push(`<meta property="article:modified_time" content="${esc(articleModified)}">`);
110
+ if (articleAuthor) parts.push(`<meta property="article:author" content="${esc(articleAuthor)}">`);
111
+
112
+ // Twitter Card
113
+ const cardType = twitterCard || (image ? 'summary_large_image' : 'summary');
114
+ parts.push(`<meta name="twitter:card" content="${esc(cardType)}">`);
115
+ if (title) parts.push(`<meta name="twitter:title" content="${esc(title)}">`);
116
+ if (description) parts.push(`<meta name="twitter:description" content="${esc(description)}">`);
117
+ if (image) parts.push(`<meta name="twitter:image" content="${esc(image)}">`);
118
+ if (twitterSite) parts.push(`<meta name="twitter:site" content="${esc(twitterSite)}">`);
119
+ if (twitterCreator) parts.push(`<meta name="twitter:creator" content="${esc(twitterCreator)}">`);
120
+
121
+ // Site verification
122
+ if (verification.google) parts.push(`<meta name="google-site-verification" content="${esc(verification.google)}">`);
123
+ if (verification.bing) parts.push(`<meta name="msvalidate.01" content="${esc(verification.bing)}">`);
124
+ if (verification.pinterest) parts.push(`<meta name="p:domain_verify" content="${esc(verification.pinterest)}">`);
125
+ if (verification.yandex) parts.push(`<meta name="yandex-verification" content="${esc(verification.yandex)}">`);
126
+
127
+ // hreflang
128
+ for (const tag of hreflang) {
129
+ if (tag.lang && tag.url) parts.push(`<link rel="alternate" hreflang="${esc(tag.lang)}" href="${esc(tag.url)}">`);
130
+ }
131
+
132
+ // JSON-LD injection
133
+ const ldItems = Array.isArray(jsonLd) ? jsonLd : jsonLd ? [jsonLd] : [];
134
+ for (const ld of ldItems) {
135
+ parts.push(`<script type="application/ld+json">${JSON.stringify(ld)}</script>`);
136
+ }
137
+
138
+ return parts.join('\n');
139
+ }
140
+
141
+ /**
142
+ * Inject head HTML into an existing HTML document string.
143
+ *
144
+ * Removes any existing `<title>` tag and inserts the provided HTML
145
+ * immediately before the closing `</head>` tag.
146
+ *
147
+ * @param {string} html - The full HTML document string.
148
+ * @param {string} headHtml - The HTML to inject (typically from {@link buildHead}).
149
+ * @returns {string} The modified HTML document.
150
+ *
151
+ * @example
152
+ * const doc = '<html><head><title>Old</title></head><body></body></html>';
153
+ * const head = buildHead({ title: 'New Title', description: 'Updated.' });
154
+ * const result = injectHead(doc, head);
155
+ * // Old <title> removed, new head tags injected before </head>
156
+ */
157
+ export function injectHead(html, headHtml) {
158
+ let result = html.replace(/<title>[^<]*<\/title>/, '');
159
+ return result.replace('</head>', headHtml + '\n</head>');
160
+ }
161
+
162
+ /**
163
+ * Generate a `robots.txt` file content string.
164
+ *
165
+ * @param {Object} [options={}] - Robots.txt configuration.
166
+ * @param {string} [options.sitemapUrl] - Absolute URL to the sitemap (appended at the end).
167
+ * @param {string[]} [options.allow=[]] - Paths to allow (e.g. `['/']`).
168
+ * @param {string[]} [options.disallow=[]] - Paths to disallow (e.g. `['/admin', '/api']`).
169
+ * @param {number} [options.crawlDelay] - Crawl delay in seconds.
170
+ * @param {string[]} [options.customRules=[]] - Additional raw lines to include.
171
+ * @returns {string} The complete robots.txt content, newline-terminated.
172
+ *
173
+ * @example
174
+ * const txt = robotsTxt({
175
+ * sitemapUrl: 'https://example.com/sitemap.xml',
176
+ * disallow: ['/admin', '/api'],
177
+ * });
178
+ * // => "User-agent: *\nDisallow: /admin\nDisallow: /api\n\nSitemap: https://example.com/sitemap.xml\n"
179
+ *
180
+ * @example
181
+ * const txt = robotsTxt({ allow: ['/'], crawlDelay: 10 });
182
+ * // => "User-agent: *\nAllow: /\nCrawl-delay: 10\n"
183
+ */
184
+ export function robotsTxt(options = {}) {
185
+ const { sitemapUrl, allow = [], disallow = [], crawlDelay, customRules = [] } = options;
186
+ const lines = ['User-agent: *'];
187
+ for (const path of allow) lines.push(`Allow: ${path}`);
188
+ for (const path of disallow) lines.push(`Disallow: ${path}`);
189
+ if (crawlDelay) lines.push(`Crawl-delay: ${crawlDelay}`);
190
+ for (const rule of customRules) lines.push(rule);
191
+ if (sitemapUrl) lines.push('', `Sitemap: ${sitemapUrl}`);
192
+ return lines.join('\n') + '\n';
193
+ }
194
+
195
+ /** @see esc */
196
+ export { esc as escapeHtml };