From b7fea4f31f287fd5c3b90bb08c49054197daf773 Mon Sep 17 00:00:00 2001 From: overcuriousity Date: Fri, 25 Jul 2025 12:23:56 +0200 Subject: [PATCH] consolidation --- src/components/ShareButton.astro | 10 ++--- src/components/ToolMatrix.astro | 34 ++++------------ src/layouts/BaseLayout.astro | 3 ++ src/pages/index.astro | 22 ++--------- src/scripts/tool-utils.ts | 52 ++++++++++++++++++++++++ src/utils/toolHelpers.ts | 68 ++++++++++++++++++++++++++++++++ 6 files changed, 138 insertions(+), 51 deletions(-) create mode 100644 src/scripts/tool-utils.ts create mode 100644 src/utils/toolHelpers.ts diff --git a/src/components/ShareButton.astro b/src/components/ShareButton.astro index ace019f..9c7120e 100644 --- a/src/components/ShareButton.astro +++ b/src/components/ShareButton.astro @@ -1,4 +1,6 @@ --- +import { createToolSlug } from '../utils/toolHelpers.js'; + export interface Props { toolName: string; context: 'card' | 'modal-primary' | 'modal-secondary'; @@ -7,12 +9,8 @@ export interface Props { const { toolName, context, size = 'small' } = Astro.props; -// Create URL-safe slug from tool name -const toolSlug = toolName.toLowerCase() - .replace(/[^a-z0-9\s-]/g, '') // Remove special characters - .replace(/\s+/g, '-') // Replace spaces with hyphens - .replace(/-+/g, '-') // Remove duplicate hyphens - .replace(/^-|-$/g, ''); // Remove leading/trailing hyphens +// AFTER: Single line with centralized function +const toolSlug = createToolSlug(toolName); const iconSize = size === 'small' ? '14' : '16'; --- diff --git a/src/components/ToolMatrix.astro b/src/components/ToolMatrix.astro index d9bc440..79faed3 100644 --- a/src/components/ToolMatrix.astro +++ b/src/components/ToolMatrix.astro @@ -286,26 +286,12 @@ domains.forEach((domain: any) => { // ===== SHARING FUNCTIONALITY ===== - // Create tool slug from name (same logic as ShareButton.astro) - function createToolSlug(toolName) { - return toolName.toLowerCase() - .replace(/[^a-z0-9\s-]/g, '') // Remove special characters - .replace(/\s+/g, '-') // Replace spaces with hyphens - .replace(/-+/g, '-') // Remove duplicate hyphens - .replace(/^-|-$/g, ''); // Remove leading/trailing hyphens - } - - // Find tool by name or slug - function findTool(identifier) { - return toolsData.find(tool => - tool.name === identifier || - createToolSlug(tool.name) === identifier.toLowerCase() - ); - } + // REMOVED: createToolSlug function - now using window.createToolSlug + // REMOVED: findTool function - now using window.findToolByIdentifier // Generate share URLs function generateShareURL(toolName, view, modal = null) { - const toolSlug = createToolSlug(toolName); + const toolSlug = window.createToolSlug(toolName); const baseUrl = window.location.origin + window.location.pathname; const params = new URLSearchParams(); params.set('tool', toolSlug); @@ -517,10 +503,7 @@ domains.forEach((domain: any) => { elements.description.textContent = tool.description; // Badges - const hasValidProjectUrl = tool.projectUrl !== undefined && - tool.projectUrl !== null && - tool.projectUrl !== "" && - tool.projectUrl.trim() !== ""; + const hasValidProjectUrl = window.isToolHosted(tool); elements.badges.innerHTML = ''; if (isConcept) { @@ -645,7 +628,7 @@ domains.forEach((domain: any) => { } if (tool.knowledgebase === true) { - const kbId = tool.name.toLowerCase().replace(/\s+/g, '-'); + const kbId = window.createToolSlug(tool.name); linksHTML += ` @@ -665,7 +648,7 @@ domains.forEach((domain: any) => { // ===== POPULATE SHARE BUTTON ===== const shareButtonContainer = document.getElementById(`share-button-${modalType}`); if (shareButtonContainer) { - const toolSlug = createToolSlug(tool.name); + const toolSlug = window.createToolSlug(tool.name); shareButtonContainer.innerHTML = `