note: don't console.error on bad link, fix `isLinkURL` function
This commit is contained in:
parent
4d05398be1
commit
8d312bdbea
14
src/note.ts
14
src/note.ts
|
@ -43,7 +43,7 @@ interface ParsedNoteContent {
|
||||||
function parseNoteContent(content: string): ParsedNoteContent {
|
function parseNoteContent(content: string): ParsedNoteContent {
|
||||||
// Parsing twice is ineffecient, but I don't know how to do only once.
|
// Parsing twice is ineffecient, but I don't know how to do only once.
|
||||||
const html = linkifyStr(content, linkifyOpts);
|
const html = linkifyStr(content, linkifyOpts);
|
||||||
const links = linkify.find(content).filter(isValidLink);
|
const links = linkify.find(content).filter(isLinkURL);
|
||||||
const firstUrl = links.find(isNonMediaLink)?.href;
|
const firstUrl = links.find(isNonMediaLink)?.href;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -77,15 +77,9 @@ function isNonMediaLink({ href }: Link): boolean {
|
||||||
return /^https?:\/\//.test(href) && !getUrlMimeType(href);
|
return /^https?:\/\//.test(href) && !getUrlMimeType(href);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Ensures the URL can be parsed. Why linkifyjs doesn't already guarantee this, idk... */
|
/** Ensures the Link is a URL so it can be parsed. */
|
||||||
function isValidLink(link: Link): boolean {
|
function isLinkURL(link: Link): boolean {
|
||||||
try {
|
return link.type === 'url';
|
||||||
new URL(link.href);
|
|
||||||
return true;
|
|
||||||
} catch (_e) {
|
|
||||||
console.error(`Invalid link: ${link.href}`);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** `npm:mime` treats `.com` as a file extension, so parse the full URL to get its path first. */
|
/** `npm:mime` treats `.com` as a file extension, so parse the full URL to get its path first. */
|
||||||
|
|
Loading…
Reference in New Issue