<% tp.user.gh_title(tp.file.selection()) %>
Last active
October 24, 2024 12:46
-
-
Save sbliven/f819f9b29ad0b5ae13820de9ab987afe to your computer and use it in GitHub Desktop.
Custom function for the Obsidian Templater plugin (https://github.com/SilentVoid13/Templater/) to convert a github issue URL to a link with the issue title. Only works for public repos.
This file contains 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
async function getGitHubIssueTitle(owner, repo, issueNumber) { | |
const url = `https://api.github.com/repos/${owner}/${repo}/issues/${issueNumber}`; | |
const response = await fetch(url); | |
if (!response.ok) { | |
throw new Error(`Error fetching issue: ${response.status}`); | |
} | |
const issueData = await response.json(); | |
return issueData.title; // Return or use the title as needed | |
} | |
function parseGitHubIssueUrl(url) { | |
const parsedUrl = new URL(url); | |
// Ensure it's a valid GitHub issue URL | |
if (parsedUrl.hostname !== 'github.com') { | |
throw new Error('Not a valid GitHub URL'); | |
} | |
const pathParts = parsedUrl.pathname.split('/'); | |
// Validate if the URL has the right structure | |
if (pathParts.length < 5 || !pathParts[3].match(/issues|pulls?/)) { | |
throw new Error('URL structure is incorrect for a GitHub issue'); | |
} | |
// Extract owner, repo, and issue number | |
const owner = pathParts[1]; | |
const repo = pathParts[2]; | |
const issueNumber = pathParts[4]; | |
return { owner, repo, issueNumber }; | |
} | |
async function gh_title(url) { | |
let owner, repo, issueNumber; | |
try { | |
({owner, repo, issueNumber} = parseGitHubIssueUrl(url)); | |
} catch (error) { | |
console.log(`Error parsing github link for ${url}: ${error}`); | |
return `[${url}](${url})` | |
} | |
try { | |
const title = await getGitHubIssueTitle(owner, repo, issueNumber); | |
return `[${title} (#${issueNumber})](${url})`; | |
} catch (error) { | |
console.log(`Error getting github issue title for ${url}: ${error}`); | |
return `[${owner}/${repo}#${issueNumber}](${url})` | |
} | |
} | |
module.exports = gh_title; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Installation:
Install as a user script. The js file should go in your 'scripts' directory. The .md file should go in your templates directory.
Usage:
It should replace the highlighted text with
[Emoji's are not supportet (#9)](https://github.com/SilentVoid13/Templater/issues/9)
If something goes wrong fetching the title from github it will use a format like
[SilentVoid13/Templater#9](https://github.com/SilentVoid13/Templater/issues/9)