[dyad] Added keyword highlighting - wrote 3 file(s)
This commit is contained in:
35
src/components/keyword-highlighter.tsx
Normal file
35
src/components/keyword-highlighter.tsx
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import React from "react";
|
||||||
|
|
||||||
|
interface KeywordHighlighterProps {
|
||||||
|
text: string;
|
||||||
|
keyword: string | null | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function KeywordHighlighter({ text, keyword }: KeywordHighlighterProps) {
|
||||||
|
if (!keyword || !text || keyword.trim() === "") {
|
||||||
|
return <>{text}</>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const escapedKeyword = keyword.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
|
||||||
|
const regex = new RegExp(`(${escapedKeyword})`, "gi");
|
||||||
|
const parts = text.split(regex);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{parts.map((part, i) =>
|
||||||
|
regex.test(part) ? (
|
||||||
|
<mark
|
||||||
|
key={i}
|
||||||
|
className="bg-yellow-200 text-yellow-900 dark:bg-yellow-700 dark:text-yellow-50 rounded px-1 mx-px font-semibold"
|
||||||
|
>
|
||||||
|
{part}
|
||||||
|
</mark>
|
||||||
|
) : (
|
||||||
|
part
|
||||||
|
)
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -29,6 +29,7 @@ import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
|
|||||||
import { ImageAltDisplay } from "./image-alt-display";
|
import { ImageAltDisplay } from "./image-alt-display";
|
||||||
import { TabIndicator } from "./tab-indicator";
|
import { TabIndicator } from "./tab-indicator";
|
||||||
import { getLengthIndicatorColor, type IndicatorColor } from "@/lib/analysis";
|
import { getLengthIndicatorColor, type IndicatorColor } from "@/lib/analysis";
|
||||||
|
import { KeywordHighlighter } from "./keyword-highlighter";
|
||||||
|
|
||||||
interface MetaData {
|
interface MetaData {
|
||||||
title: string;
|
title: string;
|
||||||
@@ -259,6 +260,7 @@ export function MetaForm() {
|
|||||||
title={editableTitle}
|
title={editableTitle}
|
||||||
description={editableDescription}
|
description={editableDescription}
|
||||||
url={url}
|
url={url}
|
||||||
|
keyword={metaData.keyword}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
{metaData.image && (
|
{metaData.image && (
|
||||||
@@ -331,7 +333,14 @@ export function MetaForm() {
|
|||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
<p className="text-muted-foreground bg-muted p-3 rounded-md min-h-[40px] break-words">
|
<p className="text-muted-foreground bg-muted p-3 rounded-md min-h-[40px] break-words">
|
||||||
{editableTitle || "Not found"}
|
{editableTitle ? (
|
||||||
|
<KeywordHighlighter
|
||||||
|
text={editableTitle}
|
||||||
|
keyword={metaData.keyword}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
"Not found"
|
||||||
|
)}
|
||||||
</p>
|
</p>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
@@ -385,7 +394,14 @@ export function MetaForm() {
|
|||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
<p className="text-muted-foreground bg-muted p-3 rounded-md min-h-[100px] break-words">
|
<p className="text-muted-foreground bg-muted p-3 rounded-md min-h-[100px] break-words">
|
||||||
{editableDescription || "Not found"}
|
{editableDescription ? (
|
||||||
|
<KeywordHighlighter
|
||||||
|
text={editableDescription}
|
||||||
|
keyword={metaData.keyword}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
"Not found"
|
||||||
|
)}
|
||||||
</p>
|
</p>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,14 +1,21 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { Globe } from "lucide-react";
|
import { Globe } from "lucide-react";
|
||||||
|
import { KeywordHighlighter } from "./keyword-highlighter";
|
||||||
|
|
||||||
interface SerpPreviewProps {
|
interface SerpPreviewProps {
|
||||||
title: string;
|
title: string;
|
||||||
description: string;
|
description: string;
|
||||||
url: string;
|
url: string;
|
||||||
|
keyword?: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function SerpPreview({ title, description, url }: SerpPreviewProps) {
|
export function SerpPreview({
|
||||||
|
title,
|
||||||
|
description,
|
||||||
|
url,
|
||||||
|
keyword,
|
||||||
|
}: SerpPreviewProps) {
|
||||||
const formatUrl = (fullUrl: string) => {
|
const formatUrl = (fullUrl: string) => {
|
||||||
try {
|
try {
|
||||||
let formattedUrl = fullUrl;
|
let formattedUrl = fullUrl;
|
||||||
@@ -18,7 +25,7 @@ export function SerpPreview({ title, description, url }: SerpPreviewProps) {
|
|||||||
const urlObject = new URL(formattedUrl);
|
const urlObject = new URL(formattedUrl);
|
||||||
const path = urlObject.pathname === "/" ? "" : urlObject.pathname;
|
const path = urlObject.pathname === "/" ? "" : urlObject.pathname;
|
||||||
// remove trailing slash
|
// remove trailing slash
|
||||||
const displayPath = path.endsWith('/') ? path.slice(0, -1) : path;
|
const displayPath = path.endsWith("/") ? path.slice(0, -1) : path;
|
||||||
return `${urlObject.hostname}${displayPath}`;
|
return `${urlObject.hostname}${displayPath}`;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return fullUrl;
|
return fullUrl;
|
||||||
@@ -41,11 +48,16 @@ export function SerpPreview({ title, description, url }: SerpPreviewProps) {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<h3 className="text-xl text-blue-600 dark:text-blue-400 mt-2 truncate font-medium">
|
<h3 className="text-xl text-blue-600 dark:text-blue-400 mt-2 truncate font-medium">
|
||||||
{title || "Meta Title Preview"}
|
<KeywordHighlighter text={title || "Meta Title Preview"} keyword={keyword} />
|
||||||
</h3>
|
</h3>
|
||||||
<p className="text-sm text-muted-foreground mt-1 line-clamp-2">
|
<p className="text-sm text-muted-foreground mt-1 line-clamp-2">
|
||||||
{description ||
|
<KeywordHighlighter
|
||||||
"This is where the meta description will be displayed. It provides a brief summary of the page's content for search engine users."}
|
text={
|
||||||
|
description ||
|
||||||
|
"This is where the meta description will be displayed. It provides a brief summary of the page's content for search engine users."
|
||||||
|
}
|
||||||
|
keyword={keyword}
|
||||||
|
/>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user