Formatting
All checks were successful
CI / Test and lint (push) Successful in 16s

This commit is contained in:
2025-06-03 11:38:31 +04:00
parent cd335a366e
commit d3ad136eab
7 changed files with 91 additions and 178 deletions

View File

@@ -35,22 +35,26 @@ export async function nostrUriToUrl(uri: string): Promise<string> {
}
export async function replaceNostrUris(markdown: string): Promise<string> {
const protectedRegex = /(`{3,}[\s\S]*?`{3,})|(`[^`]*`)|(<pre>[\s\S]*?<\/pre>)|(https?:\/\/[^\s<>"']+)/gi;
const protectedRegex =
/(`{3,}[\s\S]*?`{3,})|(`[^`]*`)|(<pre>[\s\S]*?<\/pre>)|(https?:\/\/[^\s<>"']+)/gi;
// Split text into segments: unprotected text and protected areas (code blocks, URLs)
const segments: string[] = [];
let lastIndex = 0;
markdown.replace(protectedRegex, (match, _fencedCode, _inlineCode, _preTag, _url, index) => {
segments.push(markdown.slice(lastIndex, index));
segments.push(match);
lastIndex = index + match.length;
return match;
});
markdown.replace(
protectedRegex,
(match, _fencedCode, _inlineCode, _preTag, _url, index) => {
segments.push(markdown.slice(lastIndex, index));
segments.push(match);
lastIndex = index + match.length;
return match;
},
);
segments.push(markdown.slice(lastIndex));
// Process each segment
let result = '';
let result = "";
for (let i = 0; i < segments.length; i++) {
if (i % 2 === 1 || protectedRegex.test(segments[i])) {
// Protected segment (code block or URL), leave unchanged
@@ -78,7 +82,9 @@ async function processUnprotectedText(text: string): Promise<string> {
parts.push(await replaceUrisInText(text.slice(lastIndex, match.index)));
// Process the link target
if (nostrUriRegex.test(target) && target.match(nostrUriRegex)![0] === target) {
if (
nostrUriRegex.test(target) && target.match(nostrUriRegex)![0] === target
) {
// Target is a Nostr URI, replace with resolved URL
const resolvedUrl = await nostrUriToUrl(target);
parts.push(`[${linkText}](${resolvedUrl})`);
@@ -93,12 +99,13 @@ async function processUnprotectedText(text: string): Promise<string> {
// Add any remaining text after the last link
parts.push(await replaceUrisInText(text.slice(lastIndex)));
return parts.join('');
return parts.join("");
}
async function replaceUrisInText(text: string): Promise<string> {
let modifiedText = text;
const replacements: { start: number; end: number; replacement: string }[] = [];
const replacements: { start: number; end: number; replacement: string }[] =
[];
// Collect all replacements for bare Nostr URIs
let match;
@@ -117,7 +124,8 @@ async function replaceUrisInText(text: string): Promise<string> {
// Apply replacements from right to left to avoid index shifting
for (let i = replacements.length - 1; i >= 0; i--) {
const { start, end, replacement } = replacements[i];
modifiedText = modifiedText.slice(0, start) + replacement + modifiedText.slice(end);
modifiedText = modifiedText.slice(0, start) + replacement +
modifiedText.slice(end);
}
return modifiedText;
@@ -125,5 +133,5 @@ async function replaceUrisInText(text: string): Promise<string> {
function cleanUriForTitle(uri: string): string {
// Remove "nostr:" prefix, keep "@" for the title
return uri.startsWith('nostr:') ? uri.replace(/^nostr:/, '') : uri;
return uri.startsWith("nostr:") ? uri.replace(/^nostr:/, "") : uri;
}