Skip to content

Instantly share code, notes, and snippets.

@replete
Created April 9, 2024 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save replete/8385e131e80ea4c87758b466dc759c5e to your computer and use it in GitHub Desktop.
Save replete/8385e131e80ea4c87758b466dc759c5e to your computer and use it in GitHub Desktop.
Mirriam-Webster Word of The Day - Templater User Script for Obsidian.md
// Mirriam-Webster WOTD Templater User Script:
// Include this file in your Templater plugin User Scripts folder
// Use within your daily note templater template like this:
// <% tp.user.getWOTD(tp.date.now("YYYY-MM-DD", 0, tp.file.title, "YYYY-MM-DD")) %>
function fetchWordOfTheDay() {
const rssUrl = 'https://www.merriam-webster.com/wotd/feed/rss2';
return new Promise((resolve, reject) => {
// this lets us get around CORS problems with use of fetch
require('https').get(rssUrl, (response) => {
let data = '';
response.on('data', (chunk) => {
data += chunk;
});
response.on('end', () => {
resolve(data);
});
response.on('error', (error) => {
reject(error);
});
});
}).then((xmlText) => {
const itemRegex = /<item>([\s\S]*?)<\/item>/g;
const guidRegex = /<guid[^>]*>(?:<!\[CDATA\[)?([\s\S]*?)(?:\]\]>)?<\/guid>/;
const pubDateRegex = /<pubDate[^>]*>([\s\S]*?)<\/pubDate>/;
const titleRegex = /<title[^>]*>(?:<!\[CDATA\[)?([\s\S]*?)(?:\]\]>)?<\/title>/;
const shortDefRegex = /<merriam:shortdef[^>]*>(?:<!\[CDATA\[)?([\s\S]*?)(?:\]\]>)?<\/merriam:shortdef>/;
const summaryRegex = /<itunes:summary[^>]*>(?:<!\[CDATA\[)?([\s\S]*?)(?:\]\]>)?<\/itunes:summary>/;
const wordOfTheDayArray = [];
let match;
while ((match = itemRegex.exec(xmlText)) !== null) {
const itemXml = match[1];
const guidMatch = guidRegex.exec(itemXml);
const pubDateMatch = pubDateRegex.exec(itemXml);
const titleMatch = titleRegex.exec(itemXml);
const shortDefMatch = shortDefRegex.exec(itemXml);
const summaryMatch = summaryRegex.exec(itemXml);
if (
guidMatch &&
pubDateMatch &&
titleMatch &&
shortDefMatch &&
summaryMatch
) {
const guid = guidMatch[1];
const pubDate = pubDateMatch[1];
const title = titleMatch[1];
const shortDef = shortDefMatch[1];
const summary = summaryMatch[1];
// Extract the date from pubDate in the format "YYYY-MM-DD"
const date = new Date(pubDate).toISOString().slice(0, 10);
const wordOfTheDay = {
date,
word: title,
shortDesc: shortDef,
summary,
};
wordOfTheDayArray.push(wordOfTheDay);
}
}
return wordOfTheDayArray;
});
}
async function getWOTD(dateString) {
try {
const wordOfTheDayArray = await fetchWordOfTheDay();
const wordOfTheDay = wordOfTheDayArray.find((word) => word.date === dateString);
if (wordOfTheDay) {
const { word, shortDesc, summary } = wordOfTheDay;
const formattedSummary = summary
.split('\n')
.map((line) => `> ${line}`)
.join('\n').replace('//','> ');
const truncatedMatches = formattedSummary.match(/^.*?\[See the entry >\]\(.*?\)/s);
const truncatedSummary = truncatedMatches ? truncatedMatches[0] : formattedSummary;
return `> [!${word.charAt(0).toUpperCase()+word.slice(1)} – ${shortDesc}]
${truncatedSummary}`;
} else {
return '> No word of the day found for the given date.';
}
} catch (error) {
return '> An error occurred while retrieving the word of the day.';
}
}
// (async function(){
// console.log(await getWOTD('2024-04-09'));
// })()
module.exports = getWOTD;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment