@contentgrowth/content-widget 1.1.0 → 1.1.2

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.
@@ -1,230 +0,0 @@
1
- /**
2
- * Content Viewer Component
3
- * Displays full content with markdown rendering
4
- */
5
- import { formatDate, calculateReadingTime, escapeHtml } from '../utils/helpers.js';
6
- import { marked } from 'marked';
7
-
8
- export class ContentViewer {
9
- constructor(container, api, options = {}) {
10
- this.container = container;
11
- this.api = api;
12
- this.options = {
13
- displayMode: options.displayMode || 'inline', // 'inline' or 'modal'
14
- showBackButton: options.showBackButton !== false, // Default true, can be disabled
15
- showSummary: options.showSummary !== false, // Default true, can be disabled
16
- onBack: options.onBack || null
17
- };
18
-
19
- this.article = null;
20
- this.loading = false;
21
- this.summaryExpanded = true; // Summary visible by default
22
- }
23
-
24
- /**
25
- * Load and display an article
26
- */
27
- async loadArticle(uuid) {
28
- if (this.loading) return;
29
-
30
- this.loading = true;
31
- this.showLoading();
32
-
33
- try {
34
- this.article = await this.api.fetchArticle(uuid);
35
- this.render();
36
- } catch (error) {
37
- this.showError('Failed to load article. Please try again.');
38
- console.error(error);
39
- } finally {
40
- this.loading = false;
41
- }
42
- }
43
-
44
- /**
45
- * Render the article
46
- */
47
- render() {
48
- if (!this.article) return;
49
-
50
- const readingTime = calculateReadingTime(this.article.wordCount || this.article.content);
51
- const content = this.renderMarkdown(this.article.content || '');
52
-
53
- this.container.innerHTML = `
54
- <div class="cg-content-viewer">
55
- ${this.options.showBackButton ? `
56
- <div class="cg-viewer-header">
57
- <button class="cg-back-btn">
58
- <svg width="20" height="20" viewBox="0 0 20 20" fill="none">
59
- <path d="M12 16L6 10L12 4" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
60
- </svg>
61
- Back to list
62
- </button>
63
- </div>
64
- ` : ''}
65
-
66
- <article class="cg-viewer-content">
67
- <header class="cg-post-title-section">
68
- <h1 class="cg-post-title">${escapeHtml(this.article.title)}</h1>
69
-
70
- ${this.options.showSummary && this.article.summary && this.article.category !== 'announce' ? `
71
- <div class="cg-ai-summary ${this.summaryExpanded ? 'expanded' : 'collapsed'}">
72
- <div class="cg-ai-summary-header">
73
- <div class="cg-ai-summary-label">
74
- <svg class="cg-ai-icon" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor">
75
- <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z" />
76
- </svg>
77
- <span>AI Generated Summary</span>
78
- </div>
79
- <button class="cg-summary-toggle" aria-label="Toggle summary">
80
- <svg class="cg-chevron" width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="currentColor">
81
- <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6l4 4 4-4" />
82
- </svg>
83
- </button>
84
- </div>
85
- <div class="cg-ai-summary-content">
86
- <p>${escapeHtml(this.article.summary)}</p>
87
- </div>
88
- </div>
89
- ` : ''}
90
-
91
- <div class="cg-post-meta">
92
- <span class="cg-meta-item">
93
- <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
94
- <path d="M8 8C9.65685 8 11 6.65685 11 5C11 3.34315 9.65685 2 8 2C6.34315 2 5 3.34315 5 5C5 6.65685 6.34315 8 8 8Z" stroke="currentColor" stroke-width="1.5"/>
95
- <path d="M14 14C14 11.7909 11.3137 10 8 10C4.68629 10 2 11.7909 2 14" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
96
- </svg>
97
- ${escapeHtml(this.article.authorName)}
98
- </span>
99
- <span class="cg-meta-item">
100
- <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
101
- <rect x="2" y="3" width="12" height="11" rx="2" stroke="currentColor" stroke-width="1.5"/>
102
- <path d="M5 2V4M11 2V4M2 6H14" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
103
- </svg>
104
- ${formatDate(this.article.publishedAt)}
105
- </span>
106
- <span class="cg-meta-item">
107
- <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
108
- <circle cx="8" cy="8" r="6" stroke="currentColor" stroke-width="1.5"/>
109
- <path d="M8 4V8L10.5 10.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
110
- </svg>
111
- ${readingTime}
112
- </span>
113
- </div>
114
- </header>
115
-
116
- <div class="cg-post-body">
117
- ${content}
118
- </div>
119
- </article>
120
- </div>
121
- `;
122
-
123
- // Add back button handler if button exists
124
- if (this.options.showBackButton) {
125
- const backBtn = this.container.querySelector('.cg-back-btn');
126
- if (backBtn) {
127
- backBtn.addEventListener('click', () => this.handleBack());
128
- }
129
- }
130
-
131
- // Add summary toggle handler
132
- const summaryToggle = this.container.querySelector('.cg-summary-toggle');
133
- if (summaryToggle) {
134
- summaryToggle.addEventListener('click', () => this.toggleSummary());
135
- }
136
- }
137
-
138
- /**
139
- * Toggle AI summary visibility
140
- */
141
- toggleSummary() {
142
- this.summaryExpanded = !this.summaryExpanded;
143
- const summaryEl = this.container.querySelector('.cg-ai-summary');
144
- if (summaryEl) {
145
- if (this.summaryExpanded) {
146
- summaryEl.classList.add('expanded');
147
- summaryEl.classList.remove('collapsed');
148
- } else {
149
- summaryEl.classList.add('collapsed');
150
- summaryEl.classList.remove('expanded');
151
- }
152
- }
153
- }
154
-
155
- /**
156
- * Render markdown content to HTML
157
- */
158
- renderMarkdown(markdown) {
159
- // Configure marked
160
- marked.setOptions({
161
- breaks: true,
162
- gfm: true,
163
- headerIds: true,
164
- mangle: false
165
- });
166
-
167
- try {
168
- return marked.parse(markdown);
169
- } catch (error) {
170
- console.error('Markdown parsing error:', error);
171
- return `<p>${escapeHtml(markdown)}</p>`;
172
- }
173
- }
174
-
175
- /**
176
- * Handle back button click
177
- */
178
- handleBack() {
179
- if (this.options.onBack) {
180
- this.options.onBack();
181
- }
182
- }
183
-
184
- /**
185
- * Show loading state
186
- */
187
- showLoading() {
188
- this.container.innerHTML = `
189
- <div class="cg-content-viewer">
190
- <div class="cg-viewer-loading">
191
- <div class="cg-spinner"></div>
192
- <p>Loading article...</p>
193
- </div>
194
- </div>
195
- `;
196
- }
197
-
198
- /**
199
- * Show error message
200
- */
201
- showError(message) {
202
- this.container.innerHTML = `
203
- <div class="cg-content-viewer">
204
- <div class="cg-viewer-error">
205
- <svg width="48" height="48" viewBox="0 0 48 48" fill="none">
206
- <circle cx="24" cy="24" r="20" stroke="currentColor" stroke-width="2"/>
207
- <path d="M24 16V26M24 32V32.5" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
208
- </svg>
209
- <p>${message}</p>
210
- ${this.options.showBackButton ? '<button class="cg-back-btn">Back to articles</button>' : ''}
211
- </div>
212
- </div>
213
- `;
214
-
215
- if (this.options.showBackButton) {
216
- const backBtn = this.container.querySelector('.cg-back-btn');
217
- if (backBtn) {
218
- backBtn.addEventListener('click', () => this.handleBack());
219
- }
220
- }
221
- }
222
-
223
- /**
224
- * Clear the post view
225
- */
226
- clear() {
227
- this.container.innerHTML = '';
228
- this.article = null;
229
- }
230
- }
@@ -1,40 +0,0 @@
1
- /**
2
- * Content Growth Widget - Vanilla JavaScript
3
- * Can be used in any framework or vanilla JS project
4
- */
5
-
6
- import { ContentGrowthWidget } from './widget.js';
7
- import { ContentGrowthAPI } from './utils/api-client.js';
8
-
9
- // Export main widget class
10
- export { ContentGrowthWidget };
11
-
12
- // Export API client for advanced usage
13
- export { ContentGrowthAPI };
14
-
15
- // Export helper functions
16
- export * from './utils/helpers.js';
17
-
18
- /**
19
- * Auto-initialize widgets on page load
20
- * Only runs if this is loaded as a script (not imported as module)
21
- */
22
- if (typeof document !== 'undefined' && !import.meta.url.includes('node_modules')) {
23
- if (document.readyState === 'loading') {
24
- document.addEventListener('DOMContentLoaded', () => {
25
- initializeWidgets();
26
- });
27
- } else {
28
- initializeWidgets();
29
- }
30
- }
31
-
32
- function initializeWidgets() {
33
- const containers = document.querySelectorAll('[data-cg-content]');
34
- containers.forEach(container => {
35
- new ContentGrowthWidget(container);
36
- });
37
- }
38
-
39
- // Default export for convenience
40
- export default ContentGrowthWidget;
@@ -1,154 +0,0 @@
1
- /**
2
- * Content Growth API Client
3
- * Handles fetching articles from the widget API (requires API key)
4
- */
5
- export class ContentGrowthAPI {
6
- constructor(config) {
7
- console.log('[ContentGrowthAPI] Constructor called with config:', config);
8
- this.apiKey = config.apiKey;
9
- this.baseUrl = config.baseUrl || 'https://api.content-growth.com';
10
- this.cache = new Map();
11
- this.cacheTTL = 5 * 60 * 1000; // 5 minutes
12
- console.log('[ContentGrowthAPI] Initialized with baseUrl:', this.baseUrl, 'apiKey:', this.apiKey);
13
- }
14
-
15
- /**
16
- * Fetch list of articles
17
- */
18
- async fetchArticles(options = {}) {
19
- const { page = 1, limit = 12, tags = [] } = options;
20
- console.log('[ContentGrowthAPI] fetchArticles called with options:', options);
21
-
22
- const params = new URLSearchParams({
23
- page: page.toString(),
24
- limit: limit.toString()
25
- });
26
-
27
- if (tags.length > 0) {
28
- params.set('tag', tags.join(','));
29
- }
30
-
31
- const url = `${this.baseUrl}/widget/articles?${params}`;
32
- const cacheKey = url;
33
- console.log('[ContentGrowthAPI] Request URL:', url);
34
-
35
- // Check cache
36
- const cached = this.getFromCache(cacheKey);
37
- if (cached) {
38
- console.log('[ContentGrowthAPI] Returning cached data');
39
- return cached;
40
- }
41
-
42
- try {
43
- console.log('[ContentGrowthAPI] Making fetch request with headers:', {
44
- 'X-API-Key': this.apiKey
45
- });
46
-
47
- const response = await fetch(url, {
48
- headers: {
49
- 'X-API-Key': this.apiKey
50
- }
51
- });
52
-
53
- console.log('[ContentGrowthAPI] Response status:', response.status, response.statusText);
54
-
55
- if (!response.ok) {
56
- const errorText = await response.text();
57
- console.error('[ContentGrowthAPI] Error response body:', errorText);
58
- throw new Error(`API Error: ${response.status} ${response.statusText}`);
59
- }
60
-
61
- const data = await response.json();
62
- console.log('[ContentGrowthAPI] Response data:', data);
63
-
64
- // Cache the result
65
- this.setCache(cacheKey, data);
66
-
67
- return data;
68
- } catch (error) {
69
- console.error('[ContentGrowthAPI] Failed to fetch articles:', error);
70
- throw error;
71
- }
72
- }
73
-
74
- /**
75
- * Fetch single article by UUID
76
- */
77
- async fetchArticle(uuid) {
78
- const url = `${this.baseUrl}/widget/articles/${uuid}`;
79
- const cacheKey = url;
80
- console.log('[ContentGrowthAPI] fetchArticle called for uuid:', uuid);
81
- console.log('[ContentGrowthAPI] Request URL:', url);
82
-
83
- // Check cache
84
- const cached = this.getFromCache(cacheKey);
85
- if (cached) {
86
- console.log('[ContentGrowthAPI] Returning cached article');
87
- return cached;
88
- }
89
-
90
- try {
91
- console.log('[ContentGrowthAPI] Making fetch request with headers:', {
92
- 'X-API-Key': this.apiKey
93
- });
94
-
95
- const response = await fetch(url, {
96
- headers: {
97
- 'X-API-Key': this.apiKey
98
- }
99
- });
100
-
101
- console.log('[ContentGrowthAPI] Response status:', response.status, response.statusText);
102
-
103
- if (!response.ok) {
104
- const errorText = await response.text();
105
- console.error('[ContentGrowthAPI] Error response body:', errorText);
106
- throw new Error(`API Error: ${response.status} ${response.statusText}`);
107
- }
108
-
109
- const data = await response.json();
110
- console.log('[ContentGrowthAPI] Response data:', data);
111
-
112
- // Cache the result
113
- this.setCache(cacheKey, data);
114
-
115
- return data;
116
- } catch (error) {
117
- console.error('[ContentGrowthAPI] Failed to fetch article:', error);
118
- throw error;
119
- }
120
- }
121
-
122
- /**
123
- * Get from cache if not expired
124
- */
125
- getFromCache(key) {
126
- const cached = this.cache.get(key);
127
- if (!cached) return null;
128
-
129
- const now = Date.now();
130
- if (now - cached.timestamp > this.cacheTTL) {
131
- this.cache.delete(key);
132
- return null;
133
- }
134
-
135
- return cached.data;
136
- }
137
-
138
- /**
139
- * Set cache with timestamp
140
- */
141
- setCache(key, data) {
142
- this.cache.set(key, {
143
- data,
144
- timestamp: Date.now()
145
- });
146
- }
147
-
148
- /**
149
- * Clear all cache
150
- */
151
- clearCache() {
152
- this.cache.clear();
153
- }
154
- }
@@ -1,71 +0,0 @@
1
- /**
2
- * Utility helper functions
3
- */
4
-
5
- /**
6
- * Format date to readable string
7
- */
8
- export function formatDate(timestamp) {
9
- const date = new Date(timestamp * 1000);
10
- return date.toLocaleDateString('en-US', {
11
- year: 'numeric',
12
- month: 'short',
13
- day: 'numeric'
14
- });
15
- }
16
-
17
- /**
18
- * Calculate reading time from word count or content
19
- * @param {number|string} wordCountOrContent - Word count (number) or content text (string)
20
- * @returns {string} Reading time string (e.g., "5 min read")
21
- */
22
- export function calculateReadingTime(wordCountOrContent) {
23
- if (!wordCountOrContent) return 'Unknown';
24
-
25
- const wordsPerMinute = 200;
26
- let words;
27
-
28
- // If it's a number, use it directly as word count
29
- if (typeof wordCountOrContent === 'number') {
30
- words = wordCountOrContent;
31
- if (words === 0) return 'Unknown'; // No word count available
32
- } else {
33
- // Otherwise, calculate from content text (fallback)
34
- words = wordCountOrContent.trim().split(/\s+/).filter(w => w.length > 0).length;
35
- }
36
-
37
- const minutes = Math.ceil(words / wordsPerMinute);
38
- return `${minutes} min read`;
39
- }
40
-
41
- /**
42
- * Truncate text to specified length
43
- */
44
- export function truncate(text, maxLength = 150) {
45
- if (!text || text.length <= maxLength) return text;
46
- return text.substring(0, maxLength).trim() + '...';
47
- }
48
-
49
- /**
50
- * Escape HTML to prevent XSS
51
- */
52
- export function escapeHtml(text) {
53
- const div = document.createElement('div');
54
- div.textContent = text;
55
- return div.innerHTML;
56
- }
57
-
58
- /**
59
- * Debounce function calls
60
- */
61
- export function debounce(func, wait) {
62
- let timeout;
63
- return function executedFunction(...args) {
64
- const later = () => {
65
- clearTimeout(timeout);
66
- func(...args);
67
- };
68
- clearTimeout(timeout);
69
- timeout = setTimeout(later, wait);
70
- };
71
- }
@@ -1,24 +0,0 @@
1
- /**
2
- * Type declarations for ContentGrowthWidget
3
- */
4
-
5
- export interface WidgetConfig {
6
- apiKey: string;
7
- baseUrl?: string;
8
- layoutMode?: 'cards' | 'rows';
9
- displayMode?: 'compact' | 'comfortable' | 'spacious';
10
- theme?: 'light' | 'dark';
11
- pageSize?: number;
12
- tags?: string[];
13
- category?: string;
14
- viewerMode?: 'inline' | 'modal' | 'external';
15
- mode?: 'list' | 'article-only';
16
- uuid?: string;
17
- slug?: string;
18
- articleId?: string; // Legacy support for uuid
19
- }
20
-
21
- export class ContentGrowthWidget {
22
- constructor(container: HTMLElement, config: WidgetConfig);
23
- destroy(): void;
24
- }