sponsor-block-renderer 0.1.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.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +118 -0
  3. package/package.json +35 -0
  4. package/src/index.js +105 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Brandon Himpfen
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,118 @@
1
+ # sponsor-block-renderer
2
+
3
+ Render lightweight sponsor sections for websites, newsletters, and content workflows.
4
+
5
+ `sponsor-block-renderer` helps you define sponsor content once and render it into simple HTML blocks. It is designed for creators, publishers, and developers who want a minimal and repeatable way to output sponsor sections without hardcoding HTML in multiple places.
6
+
7
+ ## Why this project exists
8
+
9
+ Sponsor content is often handled inconsistently.
10
+
11
+ One page may use a hand-written HTML block. Another may embed a slightly different variation in a template. A newsletter workflow may use an entirely separate rendering path. Over time, this creates duplication, formatting drift, and more effort when sponsor content needs to be updated.
12
+
13
+ This project provides a small rendering layer that standardizes sponsor blocks while staying easy to understand and integrate.
14
+
15
+ ## Mental model
16
+
17
+ Think of the package as a small publishing utility:
18
+
19
+ `Structured sponsor data -> HTML block -> site, newsletter, or app`
20
+
21
+ It does not replace a CMS or ad platform.
22
+
23
+ It standardizes one small but useful part of a publishing workflow.
24
+
25
+ ## What is included
26
+
27
+ - Sponsor block builder.
28
+ - HTML renderer for single sponsor blocks.
29
+ - HTML renderer for multiple sponsor blocks.
30
+ - Minimal validation for common fields.
31
+ - Example usage for content workflows.
32
+ - Basic tests.
33
+
34
+ ## Install
35
+
36
+ ```bash
37
+ npm install sponsor-block-renderer
38
+ ```
39
+
40
+ ## Example
41
+
42
+ ```js
43
+ import { renderSponsorBlock } from "sponsor-block-renderer";
44
+
45
+ const html = renderSponsorBlock({
46
+ label: "Sponsor",
47
+ name: "Example Sponsor",
48
+ description: "A simple product for thoughtful publishers.",
49
+ url: "https://example.com",
50
+ ctaText: "Visit sponsor",
51
+ disclosure: "Sponsored placement"
52
+ });
53
+
54
+ console.log(html);
55
+ ```
56
+
57
+ ## Output shape
58
+
59
+ A rendered sponsor block produces simple HTML with a predictable structure.
60
+
61
+ ```html
62
+ <section class="sponsor-block">
63
+ <p class="sponsor-block__label">Sponsor</p>
64
+ <h2 class="sponsor-block__title">Example Sponsor</h2>
65
+ <p class="sponsor-block__description">A simple product for thoughtful publishers.</p>
66
+ <p class="sponsor-block__disclosure">Sponsored placement</p>
67
+ <p class="sponsor-block__cta"><a href="https://example.com" target="_blank" rel="sponsored noopener noreferrer">Visit sponsor</a></p>
68
+ </section>
69
+ ```
70
+
71
+ ## API
72
+
73
+ ### `createSponsorBlock(input)`
74
+
75
+ Normalizes sponsor input and validates required fields.
76
+
77
+ ### `renderSponsorBlock(input, options)`
78
+
79
+ Renders a single sponsor block as HTML.
80
+
81
+ ### `renderSponsorBlocks(items, options)`
82
+
83
+ Renders multiple sponsor blocks separated by blank lines.
84
+
85
+ ## Design Principles
86
+
87
+ This project is intentionally minimal.
88
+
89
+ It focuses on a clear rendering path rather than a full sponsorship management platform. The goal is to keep sponsor content structured, reusable, and easy to publish.
90
+
91
+ The design emphasizes:
92
+
93
+ - Simplicity over abstraction.
94
+ - Reuse over duplication.
95
+ - Predictable output over visual opinion.
96
+ - Easy integration over framework lock-in.
97
+
98
+ ## Example Use Cases
99
+
100
+ This utility is useful for:
101
+
102
+ - websites with recurring sponsor sections.
103
+ - newsletter generation workflows.
104
+ - static site generators.
105
+ - content systems that separate data from presentation.
106
+
107
+ ## Roadmap
108
+
109
+ Future extensions may include:
110
+
111
+ - plain text sponsor rendering.
112
+ - JSON export helpers.
113
+ - optional template presets.
114
+ - disclosure helpers for different publishing workflows.
115
+
116
+ ## License
117
+
118
+ MIT
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "sponsor-block-renderer",
3
+ "version": "0.1.0",
4
+ "description": "Render lightweight sponsor sections for websites, newsletters, and content workflows.",
5
+ "type": "module",
6
+ "main": "src/index.js",
7
+ "exports": {
8
+ ".": "./src/index.js"
9
+ },
10
+ "files": [
11
+ "src"
12
+ ],
13
+ "scripts": {
14
+ "test": "node --test"
15
+ },
16
+ "keywords": [
17
+ "sponsor",
18
+ "sponsorship",
19
+ "renderer",
20
+ "html",
21
+ "newsletter",
22
+ "publishing",
23
+ "utility"
24
+ ],
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "https://github.com/brandonhimpfen/sponsor-block-renderer.git"
28
+ },
29
+ "bugs": {
30
+ "url": "https://github.com/brandonhimpfen/sponsor-block-renderer/issues"
31
+ },
32
+ "homepage": "https://github.com/brandonhimpfen/sponsor-block-renderer#readme",
33
+ "author": "Brandon Himpfen",
34
+ "license": "MIT"
35
+ }
package/src/index.js ADDED
@@ -0,0 +1,105 @@
1
+ function escapeHtml(value) {
2
+ return String(value)
3
+ .replace(/&/g, '&amp;')
4
+ .replace(/</g, '&lt;')
5
+ .replace(/>/g, '&gt;')
6
+ .replace(/"/g, '&quot;')
7
+ .replace(/'/g, '&#39;');
8
+ }
9
+
10
+ function attrsToString(attrs = {}) {
11
+ return Object.entries(attrs)
12
+ .filter(([, value]) => value !== undefined && value !== null && value !== false)
13
+ .map(([key, value]) => {
14
+ if (value === true) return ` ${key}`;
15
+ return ` ${key}="${escapeHtml(value)}"`;
16
+ })
17
+ .join('');
18
+ }
19
+
20
+ function normalizeSponsor(input = {}) {
21
+ const sponsor = {
22
+ label: input.label ?? 'Sponsor',
23
+ name: input.name ?? null,
24
+ description: input.description ?? null,
25
+ url: input.url ?? null,
26
+ ctaText: input.ctaText ?? 'Learn more',
27
+ imageUrl: input.imageUrl ?? null,
28
+ imageAlt: input.imageAlt ?? input.name ?? 'Sponsor image',
29
+ disclosure: input.disclosure ?? null,
30
+ meta: input.meta ?? null
31
+ };
32
+
33
+ if (!sponsor.name) {
34
+ throw new Error('Sponsor name is required.');
35
+ }
36
+
37
+ if (sponsor.url && !/^https?:\/\//i.test(sponsor.url)) {
38
+ throw new Error('Sponsor url must start with http:// or https://');
39
+ }
40
+
41
+ return sponsor;
42
+ }
43
+
44
+ export function createSponsorBlock(input = {}) {
45
+ return normalizeSponsor(input);
46
+ }
47
+
48
+ export function renderSponsorBlock(input = {}, options = {}) {
49
+ const sponsor = normalizeSponsor(input);
50
+ const {
51
+ className = 'sponsor-block',
52
+ headingLevel = 2,
53
+ openInNewTab = true,
54
+ rel = 'sponsored noopener noreferrer',
55
+ wrapperTag = 'section'
56
+ } = options;
57
+
58
+ if (!Number.isInteger(headingLevel) || headingLevel < 1 || headingLevel > 6) {
59
+ throw new Error('headingLevel must be an integer between 1 and 6.');
60
+ }
61
+
62
+ const headingTag = `h${headingLevel}`;
63
+ const linkAttrs = sponsor.url
64
+ ? attrsToString({
65
+ href: sponsor.url,
66
+ target: openInNewTab ? '_blank' : undefined,
67
+ rel: sponsor.url ? rel : undefined
68
+ })
69
+ : '';
70
+
71
+ const imageHtml = sponsor.imageUrl
72
+ ? `<div class="${escapeHtml(className)}__media"><img src="${escapeHtml(sponsor.imageUrl)}" alt="${escapeHtml(sponsor.imageAlt)}"></div>`
73
+ : '';
74
+
75
+ const descriptionHtml = sponsor.description
76
+ ? `<p class="${escapeHtml(className)}__description">${escapeHtml(sponsor.description)}</p>`
77
+ : '';
78
+
79
+ const disclosureHtml = sponsor.disclosure
80
+ ? `<p class="${escapeHtml(className)}__disclosure">${escapeHtml(sponsor.disclosure)}</p>`
81
+ : '';
82
+
83
+ const ctaHtml = sponsor.url
84
+ ? `<p class="${escapeHtml(className)}__cta"><a${linkAttrs}>${escapeHtml(sponsor.ctaText)}</a></p>`
85
+ : '';
86
+
87
+ return [
88
+ `<${wrapperTag} class="${escapeHtml(className)}">`,
89
+ ` <p class="${escapeHtml(className)}__label">${escapeHtml(sponsor.label)}</p>`,
90
+ ` <${headingTag} class="${escapeHtml(className)}__title">${escapeHtml(sponsor.name)}</${headingTag}>`,
91
+ imageHtml ? ` ${imageHtml}` : '',
92
+ descriptionHtml ? ` ${descriptionHtml}` : '',
93
+ disclosureHtml ? ` ${disclosureHtml}` : '',
94
+ ctaHtml ? ` ${ctaHtml}` : '',
95
+ `</${wrapperTag}>`
96
+ ].filter(Boolean).join('\n');
97
+ }
98
+
99
+ export function renderSponsorBlocks(items = [], options = {}) {
100
+ if (!Array.isArray(items)) {
101
+ throw new Error('items must be an array.');
102
+ }
103
+
104
+ return items.map((item) => renderSponsorBlock(item, options)).join('\n\n');
105
+ }