-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
177 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "module" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,17 @@ | ||
import { Context } from "@netlify/edge-functions" | ||
|
||
import { setSocialMetadata } from "./lib/util.ts" | ||
export default async (request: Request, context: Context) => { | ||
const url = new URL(request.url) | ||
// request. | ||
// Since this function is only triggered by the path defined in netlify.toml, no need to check the URL path | ||
const metaTags = ` | ||
<meta name="description" content="Special content for ${url.pathname}"> | ||
<meta property="og:title" content="Custom Page ${url.pathname}"> | ||
<meta property="og:description" content="Detailed description for ${url.pathname}"> | ||
` | ||
|
||
const response = await context.next() | ||
const text = await response.text() | ||
for (const [key] of url.searchParams) { | ||
if (key !== "index") { | ||
url.searchParams.delete(key) | ||
} | ||
} | ||
|
||
// Modify the HTML response | ||
let text = await response.text() | ||
text = text.replace("</head>", `${metaTags}</head>`) | ||
|
||
return new Response(text, response) | ||
const segments = url.pathname.split("/") | ||
const id = segments[2] | ||
const updatedHtml = setSocialMetadata(text, id || "image id", "My Description", "https://api.fiddl.art/images/07f9e0f4-cb76-4408-9ed4-7815781bd957-lg.webp", url.toString()) | ||
return new Response(updatedHtml, response) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
export function setSocialMetadata(html: string, title: string, description: string, imageUrl: string, canonicalUrl: string): string { | ||
// Helper to create or update meta tags in HTML content | ||
const updateMetaTag = (html: string, property: string, content: string, attribute: string = "content"): string => { | ||
// Matching meta tags with flexible attribute formats | ||
const metaTagRegex = new RegExp(`<meta\\s+[^>]*?property=["']?${property}["']?\\s+[^>]*${attribute}=["']?[^"']*["']?\\s*/?>`, "gi") | ||
const newMetaTag = `<meta property="${property}" ${attribute}="${content}" />` | ||
|
||
if (html.search(metaTagRegex) !== -1) { | ||
// Replace existing meta tag | ||
return html.replace(metaTagRegex, newMetaTag) | ||
} else { | ||
// Append new meta tag if not found | ||
return html.replace("</head>", ` ${newMetaTag}\n</head>`) | ||
} | ||
} | ||
|
||
const updateLinkTag = (html: string, rel: string, href: string): string => { | ||
// Matching link tags with flexible attribute formats | ||
const linkTagRegex = new RegExp(`<link\\s+[^>]*?rel=["']?${rel}["']?\\s+[^>]*href=["']?[^"']*["']?\\s*/?>`, "gi") | ||
const newLinkTag = `<link rel="${rel}" href="${href}" />` | ||
|
||
if (html.search(linkTagRegex) !== -1) { | ||
// Replace existing link tag | ||
return html.replace(linkTagRegex, newLinkTag) | ||
} else { | ||
// Append new link tag if not found | ||
return html.replace("</head>", ` ${newLinkTag}\n</head>`) | ||
} | ||
} | ||
|
||
// Update meta tags | ||
html = updateMetaTag(html, "og:title", title) | ||
html = updateMetaTag(html, "og:description", description) | ||
html = updateMetaTag(html, "og:image", imageUrl) | ||
html = updateMetaTag(html, "twitter:title", title) | ||
html = updateMetaTag(html, "twitter:description", description) | ||
html = updateMetaTag(html, "twitter:image", imageUrl) | ||
html = updateLinkTag(html, "canonical", canonicalUrl) | ||
|
||
return html | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
"module": "NodeNext", | ||
"strict": true, | ||
"esModuleInterop": true, | ||
"allowImportingTsExtensions": true, | ||
"noEmit": true, | ||
"moduleResolution": "NodeNext" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.