From d3ad136eaba397f7835ddd31da54c48eef18b4fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Tue, 3 Jun 2025 11:38:31 +0400 Subject: [PATCH] Formatting --- config.ts | 6 +- deno.json | 4 +- directory.ts | 8 ++- ldap.ts | 8 ++- nostr/links.ts | 34 +++++---- prism.css | 140 -------------------------------------- tests/nostr/links_test.ts | 69 ++++++++++++++----- 7 files changed, 91 insertions(+), 178 deletions(-) delete mode 100644 prism.css diff --git a/config.ts b/config.ts index 0d1630e..73ae5cb 100644 --- a/config.ts +++ b/config.ts @@ -58,9 +58,9 @@ const config = { extraLanguages: [ "bash", "markdown", - "typescript" - ] - } + "typescript", + ], + }, }; const staticUsersConfigured = Object.keys(staticUsers).length > 0; diff --git a/deno.json b/deno.json index 1d55a29..863d35c 100644 --- a/deno.json +++ b/deno.json @@ -18,7 +18,9 @@ }, "fmt": { "exclude": [ - "magick.ts" + "magick.ts", + "assets/css/prism.css", + "tests/fixtures/" ] } } diff --git a/directory.ts b/directory.ts index ac4e3c8..46436c0 100644 --- a/directory.ts +++ b/directory.ts @@ -3,7 +3,9 @@ import config from "./config.ts"; import { lookupUsernameByPubkey as ldapLookupUsername } from "./ldap.ts"; import { lookupPubkeyByUsername as ldapLookupPubkey } from "./ldap.ts"; -export async function lookupUsernameByPubkey(pubkey: string): Promise { +export async function lookupUsernameByPubkey( + pubkey: string, +): Promise { let username; for (const [key, value] of Object.entries(config.staticUsers)) { if (value === pubkey) { @@ -21,7 +23,9 @@ export async function lookupUsernameByPubkey(pubkey: string): Promise { +export async function lookupPubkeyByUsername( + username: string, +): Promise { const pubkey = config.staticUsers[username]; if (pubkey) { diff --git a/ldap.ts b/ldap.ts index 2785052..02ff37d 100644 --- a/ldap.ts +++ b/ldap.ts @@ -8,7 +8,9 @@ if (ldapEnabled) { client = new Client({ url: ldap.url as string }); } -export async function lookupPubkeyByUsername(username: string): Promise { +export async function lookupPubkeyByUsername( + username: string, +): Promise { let pubkey: string | undefined; try { @@ -35,7 +37,9 @@ export async function lookupPubkeyByUsername(username: string): Promise { +export async function lookupUsernameByPubkey( + pubkey: string, +): Promise { let username: string | undefined; try { diff --git a/nostr/links.ts b/nostr/links.ts index c01c992..8d2b0cc 100644 --- a/nostr/links.ts +++ b/nostr/links.ts @@ -35,22 +35,26 @@ export async function nostrUriToUrl(uri: string): Promise { } export async function replaceNostrUris(markdown: string): Promise { - const protectedRegex = /(`{3,}[\s\S]*?`{3,})|(`[^`]*`)|(
[\s\S]*?<\/pre>)|(https?:\/\/[^\s<>"']+)/gi;
+  const protectedRegex =
+    /(`{3,}[\s\S]*?`{3,})|(`[^`]*`)|(
[\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 {
     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 {
   // 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 {
   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 {
   // 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 {
 
 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;
 }
diff --git a/prism.css b/prism.css
deleted file mode 100644
index 5b8ed2d..0000000
--- a/prism.css
+++ /dev/null
@@ -1,140 +0,0 @@
-/**
- * prism.js default theme for JavaScript, CSS and HTML
- * Based on dabblet (http://dabblet.com)
- * @author Lea Verou
- */
-
-code[class*="language-"],
-pre[class*="language-"] {
-	color: black;
-	background: none;
-	text-shadow: 0 1px white;
-	font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
-	font-size: 1em;
-	text-align: left;
-	white-space: pre;
-	word-spacing: normal;
-	word-break: normal;
-	word-wrap: normal;
-	line-height: 1.5;
-
-	-moz-tab-size: 4;
-	-o-tab-size: 4;
-	tab-size: 4;
-
-	-webkit-hyphens: none;
-	-moz-hyphens: none;
-	-ms-hyphens: none;
-	hyphens: none;
-}
-
-pre[class*="language-"]::-moz-selection, pre[class*="language-"] ::-moz-selection,
-code[class*="language-"]::-moz-selection, code[class*="language-"] ::-moz-selection {
-	text-shadow: none;
-	background: #b3d4fc;
-}
-
-pre[class*="language-"]::selection, pre[class*="language-"] ::selection,
-code[class*="language-"]::selection, code[class*="language-"] ::selection {
-	text-shadow: none;
-	background: #b3d4fc;
-}
-
-@media print {
-	code[class*="language-"],
-	pre[class*="language-"] {
-		text-shadow: none;
-	}
-}
-
-/* Code blocks */
-pre[class*="language-"] {
-	padding: 1em;
-	margin: .5em 0;
-	overflow: auto;
-}
-
-:not(pre) > code[class*="language-"],
-pre[class*="language-"] {
-	background: #f5f2f0;
-}
-
-/* Inline code */
-:not(pre) > code[class*="language-"] {
-	padding: .1em;
-	border-radius: .3em;
-	white-space: normal;
-}
-
-.token.comment,
-.token.prolog,
-.token.doctype,
-.token.cdata {
-	color: slategray;
-}
-
-.token.punctuation {
-	color: #999;
-}
-
-.token.namespace {
-	opacity: .7;
-}
-
-.token.property,
-.token.tag,
-.token.boolean,
-.token.number,
-.token.constant,
-.token.symbol,
-.token.deleted {
-	color: #905;
-}
-
-.token.selector,
-.token.attr-name,
-.token.string,
-.token.char,
-.token.builtin,
-.token.inserted {
-	color: #690;
-}
-
-.token.operator,
-.token.entity,
-.token.url,
-.language-css .token.string,
-.style .token.string {
-	color: #9a6e3a;
-	/* This background color was intended by the author of this theme. */
-	background: hsla(0, 0%, 100%, .5);
-}
-
-.token.atrule,
-.token.attr-value,
-.token.keyword {
-	color: #07a;
-}
-
-.token.function,
-.token.class-name {
-	color: #DD4A68;
-}
-
-.token.regex,
-.token.important,
-.token.variable {
-	color: #e90;
-}
-
-.token.important,
-.token.bold {
-	font-weight: bold;
-}
-.token.italic {
-	font-style: italic;
-}
-
-.token.entity {
-	cursor: help;
-}
diff --git a/tests/nostr/links_test.ts b/tests/nostr/links_test.ts
index 3125e0a..b4e5718 100644
--- a/tests/nostr/links_test.ts
+++ b/tests/nostr/links_test.ts
@@ -3,59 +3,94 @@ import { expect } from "@std/expect";
 import { replaceNostrUris } from "../../nostr/links.ts";
 
 describe("Nostr links", () => {
-
   describe("#replaceNostrUris", () => {
     let mdContent: string;
     let result: string;
 
     beforeAll(async () => {
-      mdContent = Deno.readTextFileSync("tests/fixtures/article-2.md"),
+      mdContent = Deno.readTextFileSync("tests/fixtures/article-2.md");
       result = await replaceNostrUris(mdContent);
     });
 
     it("does not replace URIs in URLs", () => {
-      expect(result).toMatch(new RegExp("https://badges.page/p/npub1cpmvpsqtzxl4px44dp4544xwgu0ryv2lscl3qexq42dfakuza02s4fsapc"));
+      expect(result).toMatch(
+        new RegExp(
+          "https://badges.page/p/npub1cpmvpsqtzxl4px44dp4544xwgu0ryv2lscl3qexq42dfakuza02s4fsapc",
+        ),
+      );
     });
 
     it("does not replace URIs in fenced code blocks", () => {
-      expect(result).toMatch(new RegExp("Follow nostr:npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7"));
+      expect(result).toMatch(
+        new RegExp(
+          "Follow nostr:npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7",
+        ),
+      );
     });
 
     it("does not replace URIs in inline code blocks", () => {
-      expect(result).toMatch(new RegExp("raucao: nostr:npub1raustrrh5gjwt03zdj8syn9vmt2dwsv9t467m8c3gua636uxu89svgdees"));
+      expect(result).toMatch(
+        new RegExp(
+          "raucao: nostr:npub1raustrrh5gjwt03zdj8syn9vmt2dwsv9t467m8c3gua636uxu89svgdees",
+        ),
+      );
     });
 
     describe("for unknown usernames", () => {
       it("replaces plain nostr:id URIs with a markdown link", () => {
-        expect(result).toMatch(/Amber scheme 1\: \[npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\]\(https\:\/\/njump\.me\/npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\)/);
-        expect(result).toMatch(/Amber scheme 2\: \[npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\]\(https\:\/\/njump\.me\/npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\)/);
+        expect(result).toMatch(
+          /Amber scheme 1\: \[npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\]\(https\:\/\/njump\.me\/npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\)/,
+        );
+        expect(result).toMatch(
+          /Amber scheme 2\: \[npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\]\(https\:\/\/njump\.me\/npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\)/,
+        );
       });
 
       it("replaces @id URIs with a markdown link", () => {
-        expect(result).toMatch(/Amber at 1\: \[@npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\]\(https\:\/\/njump\.me\/npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\)/);
-        expect(result).toMatch(/Amber at 2\: \[@npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\]\(https\:\/\/njump\.me\/npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\)/);
+        expect(result).toMatch(
+          /Amber at 1\: \[@npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\]\(https\:\/\/njump\.me\/npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\)/,
+        );
+        expect(result).toMatch(
+          /Amber at 2\: \[@npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\]\(https\:\/\/njump\.me\/npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\)/,
+        );
       });
 
       it("replaces nostr links with external links", () => {
-        expect(result).toMatch(/Amber scheme link 1\: \[Amber\]\(https\:\/\/njump\.me\/npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\)/);
-        expect(result).toMatch(/Amber scheme link 2\: \[Amber\]\(https\:\/\/njump\.me\/npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\)/);
+        expect(result).toMatch(
+          /Amber scheme link 1\: \[Amber\]\(https\:\/\/njump\.me\/npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\)/,
+        );
+        expect(result).toMatch(
+          /Amber scheme link 2\: \[Amber\]\(https\:\/\/njump\.me\/npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\)/,
+        );
       });
     });
 
     describe("for known usernames", () => {
       it("replaces plain nostr:id URIs with a markdown link", () => {
-        expect(result).toMatch(/raucao scheme 1\: \[npub1raustrrh5gjwt03zdj8syn9vmt2dwsv9t467m8c3gua636uxu89svgdees\]\(\/@raucao\)/);
-        expect(result).toMatch(/raucao scheme 2\: \[npub1raustrrh5gjwt03zdj8syn9vmt2dwsv9t467m8c3gua636uxu89svgdees\]\(\/@raucao\)/);
+        expect(result).toMatch(
+          /raucao scheme 1\: \[npub1raustrrh5gjwt03zdj8syn9vmt2dwsv9t467m8c3gua636uxu89svgdees\]\(\/@raucao\)/,
+        );
+        expect(result).toMatch(
+          /raucao scheme 2\: \[npub1raustrrh5gjwt03zdj8syn9vmt2dwsv9t467m8c3gua636uxu89svgdees\]\(\/@raucao\)/,
+        );
       });
 
       it("replaces @id URIs with a markdown link", () => {
-        expect(result).toMatch(/raucao at 1\: \[@npub1raustrrh5gjwt03zdj8syn9vmt2dwsv9t467m8c3gua636uxu89svgdees\]\(\/@raucao\)/);
-        expect(result).toMatch(/raucao at 2\: \[@npub1raustrrh5gjwt03zdj8syn9vmt2dwsv9t467m8c3gua636uxu89svgdees\]\(\/@raucao\)/);
+        expect(result).toMatch(
+          /raucao at 1\: \[@npub1raustrrh5gjwt03zdj8syn9vmt2dwsv9t467m8c3gua636uxu89svgdees\]\(\/@raucao\)/,
+        );
+        expect(result).toMatch(
+          /raucao at 2\: \[@npub1raustrrh5gjwt03zdj8syn9vmt2dwsv9t467m8c3gua636uxu89svgdees\]\(\/@raucao\)/,
+        );
       });
 
       it("replaces scheme links with internal links", () => {
-        expect(result).toMatch(/raucao scheme link 1\: \[raucao\]\(\/@raucao\)/);
-        expect(result).toMatch(/raucao scheme link 2\: \[raucao\]\(\/@raucao\)/);
+        expect(result).toMatch(
+          /raucao scheme link 1\: \[raucao\]\(\/@raucao\)/,
+        );
+        expect(result).toMatch(
+          /raucao scheme link 2\: \[raucao\]\(\/@raucao\)/,
+        );
       });
     });
   });