What I’m trying to do
Whenever I copy paste a reddit url like this https://www.reddit.com/r/ObsidianMD/comments/1gp3klu/obsidian_reddit_and_healthcare/
I would like that in my note it is pasted like this | [reddit post title](reddit post link) | subreddit name
So, by copy pasting the previous link, the result would be | obsidian reddit and healthcare | ObsidianMD
Things I have tried
I tried using AI and it created the following snippet that for some reason doesn’t work:
const handleRedditPaste = async (evt) => {
try {
// Get clipboard data
const clipboardData = evt.clipboardData.getData("text");
// Validate clipboard content
if (!clipboardData?.trim()) return;
// Check if it's a Reddit URL
const redditUrlRegex = /^https?:\/\/(www\.)?reddit\.com\/r\/([^\/]+)\/comments\/[^\/]+\/([^\/]+)/i;
const urlMatch = clipboardData.match(redditUrlRegex);
if (!urlMatch) return;
// Extract subreddit and title
const subreddit = urlMatch[2];
const title = decodeURIComponent(urlMatch[3])
.replace(/_/g, " ")
.replace(/-/g, " ")
.replace(/\b\w/g, c => c.toUpperCase());
// Format the text for Obsidian table
const formattedText = `| [${title}](${clipboardData}) | ${subreddit} |`;
// Get the active editor
const activeView = app.workspace.getActiveViewOfType(app.MarkdownView);
if (!activeView) return;
const editor = activeView.editor;
// Insert the formatted text at cursor position
const cursor = editor.getCursor();
editor.replaceRange(formattedText, cursor);
} catch (error) {
console.error('Error processing Reddit URL:', error);
}
};