Created
August 19, 2026 21:43
-
-
Save digitaldrreamer/38b2d43938ebc3f27b804b06f3562ddf to your computer and use it in GitHub Desktop.
Helper Functions for Dev.to RSS Integration
This file contains hidden or 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
| // 1. Sanitize XML special characters | |
| export function escapeXml(str: string): string { | |
| return str | |
| .replace(/&/g, "&") | |
| .replace(/</g, "<") | |
| .replace(/>/g, ">") | |
| .replace(/"/g, """) | |
| .replace(/'/g, "'") | |
| } | |
| // 2. Escape CDATA closing tags | |
| export function escapeCdata(str: string): string { | |
| return str.replace(/]]>/g, "]]]]><![CDATA[>") | |
| } | |
| // 3. Format tags for DEV.to (Alphanumeric only, max 20 chars, max 4 tags) | |
| export function formatDevTags(rawTags: string[]): string[] { | |
| const seen = new Set<string>() | |
| const result: string[] = [] | |
| for (const raw of rawTags) { | |
| const clean = raw.replace(/[^a-zA-Z0-9]/g, "").slice(0, 20) | |
| const key = clean.toLowerCase() | |
| if (!clean || seen.has(key)) continue | |
| seen.add(key) | |
| result.push(clean) | |
| if (result.length === 4) break | |
| } | |
| return result | |
| } | |
| // 4. Convert all relative URLs (src, href, srcset) to absolute URLs | |
| export function makeUrlsAbsolute(html: string, baseUrl: string): string { | |
| return html.replace(/<[a-zA-Z][^<>]*>/g, (tag) => | |
| tag | |
| .replace( | |
| /\b(href|src)="(\/[^"]*)"/g, | |
| (_, attr, val) => `${attr}="${new URL(val, baseUrl).toString()}"` | |
| ) | |
| .replace( | |
| /\bsrcset="([^"]*)"/g, | |
| (_, val) => `srcset="${val.replace(/\/[^\s,]*/g, (p) => new URL(p, baseUrl).toString())}"` | |
| ) | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment