Convert URLs to md Links | Jihad

Updated 05092025-152551


Description

Convert all Plain URLs in the note to Markdown links with the website actual page title. Including Youtube links.

Source

function extractUrls(text) {
  // Match anything that looks like a URL up to the first whitespace
  let urlRegex = /(?<!\()https?:\/\/\S+/g;
  return text.match(urlRegex);
}

function extractTitleFromHtml(html) {
  let match = html.match(/<title>(.*?)<\/title>/i);
  return match ? match[1].trim().replace(/\s+/g, " ") : null;
}

function extractYouTubeTitleFromJson(html) {
  let match = html.match(/"title":"([^"]+?)","lengthSeconds":/);
  return match ? match[1] : null;
}

function extractRedditTitleFromHtml(html) {
  // Match the title attribute in shreddit-title tag
  let match = html.match(/<shreddit-title[^>]*title="([^"]+)"/);
  return match ? match[1] : null;
}

function extractMediumTitleFromHtml(html) {
  // Match meta tag with property="og:title" and get its content
  let match = html.match(/<meta[^>]*property="og:title"[^>]*content="([^"]+)"/);
  return match ? match[1] : null;
}

function processUrls(urls, originalText) {
  let http = HTTP.create();
  let updatedText = originalText;

  for (let url of urls) {
    let title = null;

    if (url.includes("youtu.be") || url.includes("youtube.com")) {
      let resp = http.request({ url: url, method: "GET" });
      if (resp.success) {
        title = extractYouTubeTitleFromJson(resp.responseText);
      }
    } else if (url.includes("redd.it") || url.includes("reddit.com")) {
      let resp = http.request({ url: url, method: "GET" });
      if (resp.success) {
        title = extractRedditTitleFromHtml(resp.responseText);
      }
    } else if (url.includes("medium.com")) {
      let resp = http.request({ url: url, method: "GET" });
      if (resp.success) {
        title = extractMediumTitleFromHtml(resp.responseText);
      }
    }

    // Fallback to normal <title> tag
    if (!title) {
      let resp = http.request({ url: url, method: "GET" });
      if (resp.success) {
        title = extractTitleFromHtml(resp.responseText);
      }
    }

    if (!title) title = "Link";
    updatedText = updatedText.replace(url, `[${title}](${url})`);
  }

  return updatedText;
}

function main() {
  let urls = extractUrls(draft.content);
  if (!urls) {
    alert("No Plain URLs found.");
    context.cancel();
    return;
  }

  draft.content = processUrls(urls, draft.content);
  draft.update();
}

main();


Babylon (ChatGPT)

This JavaScript script is intended to extract URLs from a given text (presumably within the Drafts app), retrieve the page titles for those URLs, and replace the URLs in the text with markdown-style links, using the title as the link text. The script specifically handles titles from YouTube, Reddit, Medium, and general HTML pages. Let's break down each part of the script.

1.

Extracting URLs:

function extractUrls(text) {
  // Match anything that looks like a URL up to the first whitespace
  let urlRegex = /(?<!\()https?:\/\/\S+/g;
  return text.match(urlRegex);
}

Explanation:


2.

Extracting Titles from HTML:

General HTML Title:

function extractTitleFromHtml(html) {
  let match = html.match(/<title>(.*?)<\/title>/i);
  return match ? match[1].trim().replace(/\s+/g, " ") : null;
}

YouTube JSON Title:

function extractYouTubeTitleFromJson(html) {
  let match = html.match(/"title":"([^"]+?)","lengthSeconds":/);
  return match ? match[1] : null;
}

Reddit Title:

function extractRedditTitleFromHtml(html) {
  let match = html.match(/<shreddit-title[^>]*title="([^"]+)"/);
  return match ? match[1] : null;
}

Medium Title:

function extractMediumTitleFromHtml(html) {
  let match = html.match(/<meta[^>]*property="og:title"[^>]*content="([^"]+)"/);
  return match ? match[1] : null;
}

3.

Processing URLs and Fetching Titles:

function processUrls(urls, originalText) {
  let http = HTTP.create();
  let updatedText = originalText;
    
  for (let url of urls) {
    let title = null;
    
    if (url.includes("youtu.be") || url.includes("youtube.com")) {
      let resp = http.request({ url: url, method: "GET" });
      if (resp.success) {
        title = extractYouTubeTitleFromJson(resp.responseText);
      }
    } else if (url.includes("redd.it") || url.includes("reddit.com")) {
      let resp = http.request({ url: url, method: "GET" });
      if (resp.success) {
        title = extractRedditTitleFromHtml(resp.responseText);
      }
    } else if (url.includes("medium.com")) {
      let resp = http.request({ url: url, method: "GET" });
      if (resp.success) {
        title = extractMediumTitleFromHtml(resp.responseText);
      }
    }
    
    // Fallback to normal <title> tag
    if (!title) {
      let resp = http.request({ url: url, method: "GET" });
      if (resp.success) {
        title = extractTitleFromHtml(resp.responseText);
      }
    }
    
    if (!title) title = "Link";
    updatedText = updatedText.replace(url, `[${title}](${url})`);
  }
    
  return updatedText;
}

Explanation:


4.

Main Function:

function main() {
  let urls = extractUrls(draft.content);
  if (!urls) {
    alert("No Plain URLs found.");
    context.cancel();
    return;
  }
    
  draft.content = processUrls(urls, draft.content);
  draft.update();
}
    
main();

Explanation:


Contextual Notes:


Potential Issues:

  1. Error Handling:

  2. Dynamic Content:

  3. Performance:

  4. Title Extraction Robustness: