Replace Timestamps with YouTube Link #255
Replies: 5 comments 6 replies
-
Hi, I think it looks good - note that you can actually add a link to the exact timestamp by adding to the "url" &t=XXmYYs where you put in the minutes and seconds i.e. |
Beta Was this translation helpful? Give feedback.
-
Wow this is such a nice and useful script! I take notes with youtube often as well but after a while I end up just giving up on hyperlinking the time stamps lol. Thanks for thinking of this and creating a solution via templater! |
Beta Was this translation helpful? Give feedback.
-
I am having issues using the script provided. As right now, when I call the script, the text is not replaced properly:
Maybe I am not calling the script properly? Am I doing something wrong? Thank you in advance. |
Beta Was this translation helpful? Give feedback.
-
This script doesn't work. I am wondering if Obsidian make some change after OP share this snippet. But the fix is simple, we'll simple need to add some
|
Beta Was this translation helpful? Give feedback.
-
I've updated this a bit to allow for using any timestamps (not just up to <%*
// define function to replace timestamps in file
async function replaceTimestamp () {
const regexHHMM = /YT=((\d+):([0-5]\d))/g;
// change "YT=" if you want a different search term. currently videos cannot be longer than 99m59s
var content = tp.file.content; // *content of file*
var matches = content.matchAll(regexHHMM); // *find all regex matches in file*
for(let match of matches){
content = content.replace(
match[0],
"["+match[1]+"]("+urlPrompt+"&t="+match[2]+"m"+match[3]+"s)"
); // *make YouTube URL from first match*
}
var file = app.workspace.activeLeaf.view.file; // *get current file*
await app.vault.modify(file, content); // *replace content with new content*
}
// if url is in frontmatter and contains youtube, check if it contains http then run function, else prompt user for url
var url = tp.frontmatter.url; // *get url field from frontmatter*
if(url != undefined && url.contains("youtube.com/watch?v=") === true){
if (url.includes("https://www.") === false){
url = "https://www." + url;
};
urlPrompt = url;
await replaceTimestamp();
} else {
if(url === undefined){url = "Enter a YouTube URL"}; // *if url doesnt exist*
var urlPrompt;
while(urlPrompt == null || urlPrompt.length < 1 || urlPrompt.contains("youtube.com/watch?v=") === false){urlPrompt = (await tp.system.prompt("YouTube URL:", url, 1))};
// *proper conditions are not met, reprompt*
if (urlPrompt.includes("https://www.") === false){urlPrompt = "https://www." + urlPrompt}
await replaceTimestamp();
}
%> |
Beta Was this translation helpful? Give feedback.
-
I make a fair amount of notes from YouTube and dislike manually adding the url to each reference to the video timestamp where a note is. I know there are services that do this but I like to just put in the timestamps myself. This templater script will look through the content of a page for timestamps prepended with "YT=" and replace all of them with a link to a YouTube video. If there is a
url
frontmatter field it will use that. Otherwise, it will prompt the user for the link.So assuming you have a file that looks like:
this script will replace it with:
and here is the code to do so
templater.replace.youtube.md
Beta Was this translation helpful? Give feedback.
All reactions