Skip to content

Commit 47ba32e

Browse files
committed
Handle link images in article viewer
1 parent 621446a commit 47ba32e

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

src/components/PrimalMarkdown/PrimalMarkdown.module.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@
192192
margin-top: 0;
193193
margin-bottom: 20px;
194194
overflow: hidden;
195+
border-radius: 12px;
195196
}
196197

197198
img + sup {

src/components/PrimalMarkdown/PrimalMarkdown.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ const PrimalMarkdown: Component<{
230230

231231
const [html, setHTML] = createSignal<string>();
232232

233-
234233
const regexIndexOf = (text: string, regex: RegExp, startpos: number) => {
235234
var indexOf = text.substring(startpos || 0).search(regex);
236235
return (indexOf >= 0) ? (indexOf + (startpos || 0)) : indexOf;
@@ -307,6 +306,11 @@ const PrimalMarkdown: Component<{
307306
continue;
308307
}
309308

309+
if (token.value.startsWith('[![')) {
310+
tokens.push({ ...token });
311+
continue;
312+
}
313+
310314
const parsedTokens = tokenizeByRegex(token.value, regex, type, token.index);
311315

312316
tokens.push(...parsedTokens);
@@ -517,6 +521,14 @@ const PrimalMarkdown: Component<{
517521
const onMouseClick= (e: MouseEvent) => {
518522
const el = e.target as HTMLElement;
519523

524+
const parent = el.parentElement;
525+
526+
if (el.tagName === 'IMG' && parent && parent.tagName === 'A' && !parent.classList.contains('noteimage')) {
527+
e.preventDefault();
528+
window.open(parent.getAttribute('href') || '', '_blank')
529+
return false;
530+
}
531+
520532
if (el.tagName === 'A') {
521533
const href = el.getAttribute('href') || '';
522534
const highlight = el.getAttribute('data-highlight') || '';
@@ -597,7 +609,6 @@ const PrimalMarkdown: Component<{
597609
return false;
598610
}
599611

600-
601612
return true;
602613
}
603614
};

src/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ export const eventRegexG = /(?:\s|^)(nostr:)?((note|nevent|naddr)1[qpzry9x8gf2tv
357357
export const eventRegexNostrless = /((note|nevent|naddr)1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]+)\b/;
358358
export const mentionRegexNostrless = /((note|nevent|naddr|nprofile|npub)1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]+)\b/;
359359

360-
export const mdImageRegex = /(https?:\/\/.*\.(?:png|jpg))|\!\[(.*?)\]\((https?:\/\/.*\.(?:png|jpg))\)/i;
360+
export const mdImageRegex = /(https?:\/\/.*\.(?:png|jpg|jpeg|webp|gif|format=png))|\!\[(.*?)\]\((https?:\/\/.*)\)/i;
361361

362362
export const specialCharsRegex = /[^A-Za-z0-9]/;
363363
export const hashtagCharsRegex = /[^A-Za-z0-9\-\_]/;

0 commit comments

Comments
 (0)