-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
extract function
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
packages/zudoku/src/lib/plugins/openapi/util/sanitizeMarkdownForMetatag.tsx
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,32 @@ | ||
export function sanitizeMarkdownForMetatag( | ||
description: string, | ||
maxLength: number = 160, | ||
): string { | ||
if (!description) { | ||
return ""; | ||
} | ||
|
||
return ( | ||
description | ||
// Replace Markdown links [text](url) with just "text" | ||
.replace(/\[([^\]]+)\]\([^)]+\)/g, "$1") | ||
// Remove Markdown image syntax: ![alt](url) | ||
.replace(/!\[.*?\]\(.*?\)/g, "") | ||
// Remove other Markdown syntax (e.g., **bold**, _italic_, `code`) | ||
.replace(/[_*`~]/g, "") | ||
// Remove headings (# Heading), blockquotes (> Quote), and horizontal rules (--- or ***) | ||
.replace(/^(?:>|\s*#+|-{3,}|\*{3,})/gm, "") | ||
// Remove any remaining formatting characters | ||
.replace(/[|>{}[\]]/g, "") | ||
// Collapse multiple spaces and trim the text | ||
.replace(/\s+/g, " ") | ||
.trim() | ||
// Limit to the specified maximum length | ||
.substring(0, maxLength) | ||
.replace(/&/g, "&") | ||
.replace(/</g, "<") | ||
.replace(/>/g, ">") | ||
.replace(/"/g, """) | ||
.replace(/'/g, "'") | ||
); | ||
} |