Repository navigation
Expand file tree
/
Copy pathstructured-data.js
More file actions
113 lines (99 loc) · 3.69 KB
/
Copy pathstructured-data.js
File metadata and controls
113 lines (99 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
(function() {
// Helper function to get meta content
function getMetaContent(name) {
const meta = document.querySelector(`meta[name="${name}"], meta[property="${name}"]`);
return meta ? meta.getAttribute('content') : '';
}
// Helper function to get page summary
function getPageSummary() {
const description = getMetaContent('description') || getMetaContent('og:description');
if (description) return description;
// Fallback: get first paragraph text from content area
const firstP = document.querySelector('.mdx-content p, #content-area p, [id*="content"] p');
return firstP ? firstP.textContent.substring(0, 160) + '...' : '';
}
// Helper function to extract tags/keywords
function getKeywords() {
const keywords = getMetaContent('keywords');
if (keywords) {
return keywords.split(',').map(k => k.trim()).filter(k => k);
}
// Try to extract from page title or content for basic keywords
const title = document.title.toLowerCase();
const basicKeywords = [];
// Add some common Tyk-related keywords based on title
if (title.includes('api')) basicKeywords.push('API');
if (title.includes('gateway')) basicKeywords.push('API Gateway');
if (title.includes('dashboard')) basicKeywords.push('Dashboard');
if (title.includes('tyk')) basicKeywords.push('Tyk');
if (title.includes('documentation')) basicKeywords.push('Documentation');
return basicKeywords;
}
// Helper function to get dates
function getPublishDate() {
const datePublished = getMetaContent('article:published_time');
if (datePublished) {
return new Date(datePublished).toISOString().split('T')[0];
}
// Default to a reasonable date for documentation
return '2024-01-01';
}
function getModifiedDate() {
const dateModified = getMetaContent('article:modified_time');
if (dateModified) {
return new Date(dateModified).toISOString().split('T')[0];
}
// Use current date as modified date
return new Date().toISOString().split('T')[0];
}
// Wait for DOM to be ready
function initStructuredData() {
// Build structured data object
const structuredData = {
"@context": "https://schema.org",
"@type": "WebPage",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": window.location.origin
},
"name": document.title,
"headline": document.title,
"description": getPageSummary(),
"inLanguage": "en-US",
"author": {
"@type": "Organization",
"name": "Tyk"
},
"copyrightHolder": {
"@type": "Organization",
"name": "Tyk"
},
"url": window.location.href,
"keywords": getKeywords(),
"datePublished": getPublishDate(),
"dateModified": getModifiedDate()
};
// Remove empty keywords array if no keywords found
if (structuredData.keywords.length === 0) {
delete structuredData.keywords;
}
// Check if structured data already exists to avoid duplicates
const existingScript = document.querySelector('script[type="application/ld+json"]');
if (existingScript) {
return; // Don't add duplicate structured data
}
// Inject structured data into head
const script = document.createElement('script');
script.type = 'application/ld+json';
script.textContent = JSON.stringify(structuredData, null, 2);
document.head.appendChild(script);
// Debug log (remove in production)
console.log('Tyk Docs: Structured data added', structuredData);
}
// Initialize when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initStructuredData);
} else {
initStructuredData();
}
})();