Last active
November 8, 2023 06:42
-
-
Save HananoshikaYomaru/8861576f9a8e1471623d7e204bb9956b to your computer and use it in GitHub Desktop.
obsidian link preview
This file contains hidden or 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
class Dedent { | |
dedent(templ) { | |
var values = []; | |
for (var _i = 1; _i < arguments.length; _i++) { | |
values[_i - 1] = arguments[_i]; | |
} | |
var strings = Array.from(typeof templ === 'string' ? [templ] : templ); | |
strings[strings.length - 1] = strings[strings.length - 1].replace(/\r?\n([\t ]*)$/, ''); | |
var indentLengths = strings.reduce(function (arr, str) { | |
var matches = str.match(/\n([\t ]+|(?!\s).)/g); | |
if (matches) { | |
return arr.concat(matches.map(function (match) { var _a, _b; return (_b = (_a = match.match(/[\t ]/g)) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0; })); | |
} | |
return arr; | |
}, []); | |
if (indentLengths.length) { | |
var pattern_1 = new RegExp("\n[\t ]{" + Math.min.apply(Math, indentLengths) + "}", 'g'); | |
strings = strings.map(function (str) { return str.replace(pattern_1, '\n'); }); | |
} | |
strings[0] = strings[0].replace(/^\r?\n/, ''); | |
var string = strings[0]; | |
values.forEach(function (value, i) { | |
var endentations = string.match(/(?:^|\n)( *)$/); | |
var endentation = endentations ? endentations[1] : ''; | |
var indentedValue = value; | |
if (typeof value === 'string' && value.includes('\n')) { | |
indentedValue = String(value) | |
.split('\n') | |
.map(function (str, i) { | |
return i === 0 ? str : "" + endentation + str; | |
}) | |
.join('\n'); | |
} | |
string += indentedValue + strings[i + 1]; | |
}); | |
return string; | |
} | |
} |
This file contains hidden or 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
// code from https://github.com/x-Ai/obsidian-nifty-links/blob/master/main.ts | |
class LinkPreview { | |
isUrl(text) { | |
const urlRegex = new RegExp( | |
"^(http:\\/\\/www\\.|https:\\/\\/www\\.|http:\\/\\/|https:\\/\\/)?[a-z0-9]+([\\-.]{1}[a-z0-9]+)*\\.[a-z]{2,5}(:[0-9]{1,5})?(\\/.*)?$" | |
); | |
return urlRegex.test(text); | |
} | |
async getLinkPreviewFromUrl(url) { | |
if (!this.isUrl(url)) { | |
return "this is not a url"; | |
} | |
const response = await requestUrl( | |
`http://iframely.server.crestify.com/iframely?url=${url}` | |
); | |
const data = response.json; | |
let imageLink = data.links.find( | |
// @ts-ignore | |
(value) => value.type.startsWith("image") && value.rel.includes("twitter") | |
); | |
imageLink = imageLink ? imageLink.href : ""; | |
let cardTextStyle = imageLink ? "" : ' style="width: 100%;"'; | |
let imageContainerHTML = imageLink | |
? `<div class="nifty-link-image-container"> | |
<div class="nifty-link-image" style="background-image: url('${imageLink}')"> | |
</div> | |
</div>` | |
: ""; | |
let iconLink = data.links.find( | |
// @ts-ignore | |
(value) => value.type.startsWith("image") && value.rel.includes("icon") | |
); | |
iconLink = iconLink ? iconLink.href : ""; | |
return customJS.Dedent.dedent`<div class="nifty-link-card-container"> | |
<a class="nifty-link-card" href="${url}" target="_blank"> | |
<div class="nifty-link-card-text"${cardTextStyle}> | |
<div class="nifty-link-card-title line-clamp-2">${( | |
data.meta.title || "" | |
) | |
.replace(/\s{3,}/g, " ") | |
.trim()}</div> | |
<div class="nifty-link-card-description">${( | |
data.meta.description || "" | |
) | |
.replace(/\s{3,}/g, " ") | |
.trim()}</div> | |
<div class="nifty-link-href"> | |
<img class="nifty-link-icon" src="${iconLink}"> | |
${url} | |
</div> | |
</div> | |
${imageContainerHTML} | |
</a> | |
</div> | |
\n`; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment