Spaces:
Running
Running
File size: 1,560 Bytes
35ef307 6cca3b1 35ef307 |
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 |
/**
* Injects the DeepSite badge script into HTML content before the closing </body> tag.
* If the script already exists, it ensures only one instance is present.
*
* @param html - The HTML content to inject the script into
* @returns The HTML content with the badge script injected
*/
export function injectDeepSiteBadge(html: string): string {
const badgeScript = '<script src="https://huggingface.co/deepsite/deepsite-badge.js"></script>';
// Remove any existing badge script to avoid duplicates
const cleanedHtml = html.replace(
/<script\s+src=["']https:\/\/deepsite\.hf\.co\/deepsite-badge\.js["']\s*><\/script>\s*/gi,
''
);
// Check if there's a closing body tag
const bodyCloseIndex = cleanedHtml.lastIndexOf('</body>');
if (bodyCloseIndex !== -1) {
// Inject the script before the closing </body> tag
return (
cleanedHtml.slice(0, bodyCloseIndex) +
badgeScript +
'\n' +
cleanedHtml.slice(bodyCloseIndex)
);
}
// If no closing body tag, append the script at the end
return cleanedHtml + '\n' + badgeScript;
}
/**
* Checks if a page path represents the main index page
*
* @param path - The page path to check
* @returns True if the path represents the main index page
*/
export function isIndexPage(path: string): boolean {
const normalizedPath = path.toLowerCase();
return (
normalizedPath === '/' ||
normalizedPath === 'index' ||
normalizedPath === '/index' ||
normalizedPath === 'index.html' ||
normalizedPath === '/index.html'
);
}
|