-
-
Notifications
You must be signed in to change notification settings - Fork 27
Plaintext RSS fixer upper #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+176
−2
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /** | ||
| * Starting around episode 223, the RSS feed changed from HTML to plain text. | ||
| * This transformer converts the new plain-text format to match the old HTML structure exactly. | ||
| */ | ||
| export function transformPlainTextToHtml(text: string): string { | ||
| const lines = text.split('\n').map(l => l.trim()).filter(Boolean); | ||
| const html: string[] = []; | ||
| let i = 0; | ||
|
|
||
| while (i < lines.length) { | ||
| const line = lines[i]; | ||
|
|
||
| // Check if this is a timestamp line like "(00:00) - Intro" | ||
| if (/^\(\d{2}:\d{2}(?::\d{2})?\)\s*-/.test(line)) { | ||
| // Start collecting all consecutive timestamp lines into a list | ||
| const listItems: string[] = []; | ||
| while (i < lines.length && /^\(\d{2}:\d{2}(?::\d{2})?\)\s*-/.test(lines[i])) { | ||
| listItems.push(escapeHtml(lines[i])); | ||
| i++; | ||
| } | ||
| html.push('<ul>'); | ||
| listItems.forEach(item => html.push(`<li>${item}</li>`)); | ||
| html.push('</ul>'); | ||
| continue; | ||
| } | ||
|
|
||
| // Check if this is a section header (bold text like "**Links**" or just "Links") | ||
| if (/^\*\*(.+?)\*\*$/.test(line)) { | ||
| const text = line.replace(/^\*\*(.+?)\*\*$/, '$1'); | ||
| html.push(`<p><strong>${escapeHtml(text)}</strong></p>`); | ||
| i++; | ||
| continue; | ||
| } | ||
|
|
||
| // Check if this looks like a link list item (e.g., "CodeRabbit: https://...") | ||
| if (/:?\s*https?:\/\//.test(line)) { | ||
| // Collect all consecutive link lines into a list | ||
| const linkItems: string[] = []; | ||
| while (i < lines.length && /:?\s*https?:\/\//.test(lines[i])) { | ||
| linkItems.push(lines[i]); | ||
| i++; | ||
| } | ||
| html.push('<ul>'); | ||
| linkItems.forEach(item => { | ||
| // Parse "Label: URL" or just "URL" | ||
| const match = item.match(/^(.+?):\s*(https?:\/\/.+)$/); | ||
| if (match) { | ||
| const label = escapeHtml(match[1].trim()); | ||
| const url = escapeHtml(match[2].trim()); | ||
| html.push(`<li>${label}: <a href="${url}">${url}</a></li>`); | ||
| } else { | ||
| const urlMatch = item.match(/(https?:\/\/.+)/); | ||
| if (urlMatch) { | ||
| const url = escapeHtml(urlMatch[1].trim()); | ||
| html.push(`<li><a href="${url}">${url}</a></li>`); | ||
| } else { | ||
| html.push(`<li>${escapeHtml(item)}</li>`); | ||
| } | ||
| } | ||
| }); | ||
| html.push('</ul>'); | ||
| continue; | ||
| } | ||
|
|
||
| // Default: regular paragraph | ||
| html.push(`<p>${escapeHtml(line)}</p>`); | ||
| i++; | ||
| } | ||
|
|
||
| return html.join('\n'); | ||
| } | ||
|
|
||
| export function escapeHtml(str: string): string { | ||
| return str | ||
| .replace(/&/g, '&') | ||
| .replace(/</g, '<') | ||
| .replace(/>/g, '>') | ||
| .replace(/\"/g, '"') | ||
| .replace(/'/g, '''); | ||
| } | ||
|
|
||
| export function looksLikeHtml(text: string): boolean { | ||
| return /<[a-z][\s\S]*>/i.test(text.trim()); | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,10 +3,16 @@ import parseFeed from 'rss-to-json'; | |
| import { array, number, object, optional, parse, string } from 'valibot'; | ||
|
|
||
| import { optimizeImage } from './optimize-episode-image'; | ||
| import { | ||
| escapeHtml, | ||
| looksLikeHtml, | ||
| transformPlainTextToHtml | ||
| } from './rss-transform'; | ||
| import { dasherize } from '../utils/dasherize'; | ||
| import { truncate } from '../utils/truncate'; | ||
| import starpodConfig from '../../starpod.config'; | ||
|
|
||
|
|
||
| export interface Show { | ||
| title: string; | ||
| description: string; | ||
|
|
@@ -102,7 +108,9 @@ export async function getAllEpisodes() { | |
| return { | ||
| id, | ||
| title: `${title}`, | ||
| content: description, | ||
| content: looksLikeHtml(description) | ||
| ? description | ||
| : transformPlainTextToHtml(description), | ||
| description: truncate(htmlToText(description), 260), | ||
| duration: itunes_duration, | ||
|
Comment on lines
+111
to
115
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Plain-text descriptions still go through ✅ Suggested fix- return {
+ const isHtml = looksLikeHtml(description);
+ const descriptionText = isHtml ? htmlToText(description) : description;
+ return {
id,
title: `${title}`,
- content: looksLikeHtml(description)
- ? description
- : transformPlainTextToHtml(description),
- description: truncate(htmlToText(description), 260),
+ content: isHtml ? description : transformPlainTextToHtml(description),
+ description: truncate(descriptionText, 260),🤖 Prompt for AI Agents |
||
| episodeImage: itunes_image?.href, | ||
|
|
@@ -121,4 +129,4 @@ export async function getAllEpisodes() { | |
|
|
||
| episodesCache = episodes; | ||
| return episodes; | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { | ||
| escapeHtml, | ||
| looksLikeHtml, | ||
| transformPlainTextToHtml | ||
| } from '../../src/lib/rss-transform'; | ||
|
|
||
| describe('RSS Transformation Functions', () => { | ||
| describe('escapeHtml', () => { | ||
| it('escapes multiple special characters', () => { | ||
| expect(escapeHtml('<script>alert("XSS & stuff")</script>')).toBe( | ||
| '<script>alert("XSS & stuff")</script>' | ||
| ); | ||
| }); | ||
|
|
||
| it('returns unchanged string without special characters', () => { | ||
| expect(escapeHtml('Hello World')).toBe('Hello World'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('looksLikeHtml', () => { | ||
| it('detects HTML tags vs plain text', () => { | ||
| expect(looksLikeHtml('<p>Hello</p>')).toBe(true); | ||
| expect(looksLikeHtml('Just plain text')).toBe(false); | ||
| expect(looksLikeHtml('5 < 10 and 10 > 5')).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('transformPlainTextToHtml', () => { | ||
| it('handles mixed content with lists, headers, and paragraphs', () => { | ||
| const input = `**Episode Summary** | ||
|
|
||
| This episode covers many topics & details. | ||
|
|
||
| **Timestamps** | ||
|
|
||
| (00:00) - Introduction | ||
| (05:30) - Main discussion | ||
| (00:00:15) - With seconds | ||
|
|
||
| **Links** | ||
|
|
||
| GitHub: https://github.com/example | ||
| https://example.com | ||
| Company & Co: https://example.com?foo=bar&baz=qux | ||
|
|
||
| Thanks for listening!`; | ||
|
|
||
| const output = transformPlainTextToHtml(input); | ||
|
|
||
| expect(output).toContain('<p><strong>Episode Summary</strong></p>'); | ||
| expect(output).toContain('<p>This episode covers many topics & details.</p>'); | ||
| expect(output).toContain('<li>(00:00) - Introduction</li>'); | ||
| expect(output).toContain('<li>(00:00:15) - With seconds</li>'); | ||
| expect(output.match(/<ul>/g)).toHaveLength(2); | ||
| expect(output).toContain( | ||
| '<li>GitHub: <a href="https://github.com/example">https://github.com/example</a></li>' | ||
| ); | ||
| expect(output).toContain( | ||
| '<li>Company & Co: <a href="https://example.com?foo=bar&baz=qux">https://example.com?foo=bar&baz=qux</a></li>' | ||
| ); | ||
| expect(output).toContain('<p>Thanks for listening!</p>'); | ||
| }); | ||
|
|
||
| it('separates non-consecutive timestamp groups', () => { | ||
| const input = `(00:00) - Intro | ||
| (05:00) - Part 1 | ||
|
|
||
| Some text in between | ||
|
|
||
| (10:00) - Part 2 | ||
| (15:00) - Part 3`; | ||
| const output = transformPlainTextToHtml(input); | ||
| expect(output.match(/<ul>/g)).toHaveLength(2); | ||
| }); | ||
|
|
||
| it('returns empty string for blank input', () => { | ||
| expect(transformPlainTextToHtml('')).toBe(''); | ||
| expect(transformPlainTextToHtml(' \n \n ')).toBe(''); | ||
| }); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inline-URL sentences get coerced into link lists (drops surrounding text).
Any line containing
httpbecomes a list item, so a sentence like “Sponsor: https://x (use code …)” loses the trailing text. If that’s not intended, tighten detection to only match lines that are just a URL or “Label: URL”.🔧 Safer link-line detection
📝 Committable suggestion
🤖 Prompt for AI Agents