Compare commits
No commits in common. "master" and "v1.0.0-alpha.6" have entirely different histories.
master
...
v1.0.0-alp
@ -13,6 +13,6 @@ jobs:
|
|||||||
node-version: 18
|
node-version: 18
|
||||||
- uses: denoland/setup-deno@v2
|
- uses: denoland/setup-deno@v2
|
||||||
with:
|
with:
|
||||||
deno-version: v2.5.x
|
deno-version: v2.1.x
|
||||||
- run: "deno task test"
|
- run: "deno task test"
|
||||||
- run: "deno lint"
|
- run: "deno lint"
|
||||||
|
|||||||
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,3 @@
|
|||||||
.env
|
.env
|
||||||
users.yaml
|
users.yaml
|
||||||
build/
|
build/
|
||||||
coverage/
|
|
||||||
|
|||||||
@ -45,11 +45,6 @@ pre code {
|
|||||||
padding: 0.6rem 1rem;
|
padding: 0.6rem 1rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.highlight pre {
|
|
||||||
font-size: 0.9em;
|
|
||||||
padding: 0.6rem 1rem;
|
|
||||||
}
|
|
||||||
|
|
||||||
img {
|
img {
|
||||||
max-width: 100%;
|
max-width: 100%;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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;
|
|
||||||
}
|
|
||||||
@ -47,13 +47,6 @@ pre code {
|
|||||||
color: var(--text-color-dark-bg);
|
color: var(--text-color-dark-bg);
|
||||||
}
|
}
|
||||||
|
|
||||||
.highlight pre,
|
|
||||||
.highlight code {
|
|
||||||
background-color: var(--background-color-body);
|
|
||||||
border: 2px solid #e8e3da;
|
|
||||||
color: var(--text-color-body);
|
|
||||||
}
|
|
||||||
|
|
||||||
dl dt {
|
dl dt {
|
||||||
color: var(--text-color-discreet);
|
color: var(--text-color-discreet);
|
||||||
}
|
}
|
||||||
|
|||||||
11
config.ts
11
config.ts
@ -1,5 +1,5 @@
|
|||||||
import { load } from "@std/dotenv";
|
import { load } from "@std/dotenv";
|
||||||
import { parse as parseYaml } from "@std/yaml";
|
import { parse as parseYaml } from "jsr:@std/yaml";
|
||||||
import { checkFileExists } from "./utils.ts";
|
import { checkFileExists } from "./utils.ts";
|
||||||
import { log } from "./log.ts";
|
import { log } from "./log.ts";
|
||||||
|
|
||||||
@ -52,15 +52,6 @@ const config = {
|
|||||||
},
|
},
|
||||||
query_timeout: parseInt(Deno.env.get("RELAY_TIMEOUT_MS") || "5000"),
|
query_timeout: parseInt(Deno.env.get("RELAY_TIMEOUT_MS") || "5000"),
|
||||||
njump_url: Deno.env.get("NJUMP_URL") || "https://njump.me",
|
njump_url: Deno.env.get("NJUMP_URL") || "https://njump.me",
|
||||||
prism: {
|
|
||||||
// TODO make configurable via ENV
|
|
||||||
// Supported languages: https://app.unpkg.com/prismjs@1.29.0/files/components
|
|
||||||
extraLanguages: [
|
|
||||||
"bash",
|
|
||||||
"markdown",
|
|
||||||
"typescript",
|
|
||||||
],
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const staticUsersConfigured = Object.keys(staticUsers).length > 0;
|
const staticUsersConfigured = Object.keys(staticUsers).length > 0;
|
||||||
|
|||||||
39
deno.json
39
deno.json
@ -1,9 +1,9 @@
|
|||||||
{
|
{
|
||||||
"tasks": {
|
"tasks": {
|
||||||
"dev": "deno run -P --watch server.ts",
|
"dev": "deno run --allow-all --watch server.ts",
|
||||||
"server": "deno run -P server.ts",
|
"server": "deno run --allow-all server.ts",
|
||||||
"compile": "deno compile -P --include ./assets/ --exclude ./tests/ --output ./build/substr_x86_64-unknown-linux-gnu server.ts",
|
"compile": "deno compile --allow-all --include ./assets/ --output ./build/substr_x86_64-unknown-linux-gnu server.ts",
|
||||||
"test": "DENO_ENV=test deno test -P=test"
|
"test": "DENO_ENV=test deno test --allow-read --allow-env"
|
||||||
},
|
},
|
||||||
"imports": {
|
"imports": {
|
||||||
"@deno/gfm": "jsr:@deno/gfm@^0.10.0",
|
"@deno/gfm": "jsr:@deno/gfm@^0.10.0",
|
||||||
@ -18,36 +18,7 @@
|
|||||||
},
|
},
|
||||||
"fmt": {
|
"fmt": {
|
||||||
"exclude": [
|
"exclude": [
|
||||||
"magick.ts",
|
"magick.ts"
|
||||||
"assets/css/prism.css",
|
|
||||||
"tests/fixtures/"
|
|
||||||
]
|
]
|
||||||
},
|
|
||||||
"permissions": {
|
|
||||||
"default": {
|
|
||||||
"read": [
|
|
||||||
".env",
|
|
||||||
".env.test",
|
|
||||||
"users.yaml",
|
|
||||||
"users.yaml.test",
|
|
||||||
"/etc/substr/users.yaml",
|
|
||||||
"/etc/substr/users.yaml.test",
|
|
||||||
"/tmp/substr/",
|
|
||||||
"assets/"
|
|
||||||
],
|
|
||||||
"write": ["/tmp/substr/"],
|
|
||||||
"run": ["which", "magick", "convert"],
|
|
||||||
"env": true,
|
|
||||||
"net": true
|
|
||||||
},
|
|
||||||
"test": {
|
|
||||||
"read": [
|
|
||||||
"tests/",
|
|
||||||
".env.test",
|
|
||||||
"users.yaml.test",
|
|
||||||
"/etc/substr/users.yaml.test"
|
|
||||||
],
|
|
||||||
"env": true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
3
deno.lock
generated
3
deno.lock
generated
@ -54,7 +54,6 @@
|
|||||||
"npm:nostr-tools@^2.7.0": "2.10.4",
|
"npm:nostr-tools@^2.7.0": "2.10.4",
|
||||||
"npm:nostr-wasm@0.1.0": "0.1.0",
|
"npm:nostr-wasm@0.1.0": "0.1.0",
|
||||||
"npm:path-to-regexp@6.2.1": "6.2.1",
|
"npm:path-to-regexp@6.2.1": "6.2.1",
|
||||||
"npm:prismjs@1.29.0": "1.29.0",
|
|
||||||
"npm:prismjs@^1.29.0": "1.29.0",
|
"npm:prismjs@^1.29.0": "1.29.0",
|
||||||
"npm:sanitize-html@^2.13.0": "2.13.1",
|
"npm:sanitize-html@^2.13.0": "2.13.1",
|
||||||
"npm:websocket-ts@^2.1.5": "2.1.5",
|
"npm:websocket-ts@^2.1.5": "2.1.5",
|
||||||
@ -72,7 +71,7 @@
|
|||||||
"npm:marked-alert",
|
"npm:marked-alert",
|
||||||
"npm:marked-footnote",
|
"npm:marked-footnote",
|
||||||
"npm:marked-gfm-heading-id",
|
"npm:marked-gfm-heading-id",
|
||||||
"npm:prismjs@^1.29.0",
|
"npm:prismjs",
|
||||||
"npm:sanitize-html"
|
"npm:sanitize-html"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@ -3,9 +3,7 @@ import config from "./config.ts";
|
|||||||
import { lookupUsernameByPubkey as ldapLookupUsername } from "./ldap.ts";
|
import { lookupUsernameByPubkey as ldapLookupUsername } from "./ldap.ts";
|
||||||
import { lookupPubkeyByUsername as ldapLookupPubkey } from "./ldap.ts";
|
import { lookupPubkeyByUsername as ldapLookupPubkey } from "./ldap.ts";
|
||||||
|
|
||||||
export async function lookupUsernameByPubkey(
|
export async function lookupUsernameByPubkey(pubkey: string): Promise<string | undefined> {
|
||||||
pubkey: string,
|
|
||||||
): Promise<string | undefined> {
|
|
||||||
let username;
|
let username;
|
||||||
for (const [key, value] of Object.entries(config.staticUsers)) {
|
for (const [key, value] of Object.entries(config.staticUsers)) {
|
||||||
if (value === pubkey) {
|
if (value === pubkey) {
|
||||||
@ -23,9 +21,7 @@ export async function lookupUsernameByPubkey(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function lookupPubkeyByUsername(
|
export async function lookupPubkeyByUsername(username: string): Promise<string | undefined> {
|
||||||
username: string,
|
|
||||||
): Promise<string | undefined> {
|
|
||||||
const pubkey = config.staticUsers[username];
|
const pubkey = config.staticUsers[username];
|
||||||
|
|
||||||
if (pubkey) {
|
if (pubkey) {
|
||||||
|
|||||||
12
feeds.ts
12
feeds.ts
@ -20,8 +20,7 @@ export async function profileAtomFeed(
|
|||||||
<id>${articleId}</id>
|
<id>${articleId}</id>
|
||||||
<title>${article.title}</title>
|
<title>${article.title}</title>
|
||||||
<link href="${article.url}" />
|
<link href="${article.url}" />
|
||||||
<link rel="alternate" type="text/html" href="${article.url}" />
|
<link rel="alternate" type="application/nostr+json" href="nostr:${article.naddr}">
|
||||||
<link rel="alternate" type="application/nostr+json" href="nostr:${article.naddr}" />
|
|
||||||
<updated>${isoDate(article.updatedAt)}</updated>
|
<updated>${isoDate(article.updatedAt)}</updated>
|
||||||
<published>${isoDate(article.publishedAt)}</published>
|
<published>${isoDate(article.publishedAt)}</published>
|
||||||
<summary>${article.summary}</summary>
|
<summary>${article.summary}</summary>
|
||||||
@ -34,17 +33,14 @@ export async function profileAtomFeed(
|
|||||||
|
|
||||||
return `
|
return `
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:nostr="urn:nostr:protocol">
|
<feed xmlns="http://www.w3.org/2005/Atom">
|
||||||
<title>${profile.name} on Nostr (Articles)</title>
|
<title>${profile.name} on Nostr (Articles)</title>
|
||||||
<link rel="alternate" type="text/html" href="${profile.profileUrl}" />
|
<link rel="alternate" type="application/nostr+json" href="nostr:${profile.npub}">
|
||||||
<link rel="alternate" type="application/nostr+json" href="nostr:${profile.npub}" />
|
|
||||||
<id>${feedId}</id>
|
<id>${feedId}</id>
|
||||||
<updated>${isoDate(lastUpdate)}</updated>
|
<updated>${isoDate(lastUpdate)}</updated>
|
||||||
<icon>${profile.avatarImageUrl}</icon>
|
<icon>${profile.avatarImageUrl}</icon>
|
||||||
<author>
|
<author>
|
||||||
<name>${profile.name}</name>
|
<name>${name}</name>
|
||||||
<uri>${profile.profileUrl}</uri>
|
|
||||||
<nostr:uri>nostr:${profile.nprofile}</nostr:uri>
|
|
||||||
</author>
|
</author>
|
||||||
${articlesXml}
|
${articlesXml}
|
||||||
</feed>
|
</feed>
|
||||||
|
|||||||
@ -4,7 +4,6 @@ import { fetchArticlesByAuthor, fetchProfileEvent } from "../nostr.ts";
|
|||||||
import { profileAtomFeed } from "../feeds.ts";
|
import { profileAtomFeed } from "../feeds.ts";
|
||||||
import Profile from "../models/profile.ts";
|
import Profile from "../models/profile.ts";
|
||||||
import { notFoundHandler } from "../handlers/errors.ts";
|
import { notFoundHandler } from "../handlers/errors.ts";
|
||||||
import { generateOgProfileImage } from "../magick.ts";
|
|
||||||
|
|
||||||
const userAtomFeedHandler = async function (ctx: Context) {
|
const userAtomFeedHandler = async function (ctx: Context) {
|
||||||
const username = ctx.state.username;
|
const username = ctx.state.username;
|
||||||
@ -21,7 +20,6 @@ const userAtomFeedHandler = async function (ctx: Context) {
|
|||||||
const profile = new Profile(profileEvent, username);
|
const profile = new Profile(profileEvent, username);
|
||||||
|
|
||||||
if (profile.nip05) {
|
if (profile.nip05) {
|
||||||
await generateOgProfileImage(profile);
|
|
||||||
const articles = await fetchArticlesByAuthor(pubkey, 10);
|
const articles = await fetchArticlesByAuthor(pubkey, 10);
|
||||||
const atom = await profileAtomFeed(profile, articles);
|
const atom = await profileAtomFeed(profile, articles);
|
||||||
|
|
||||||
|
|||||||
9
html.ts
9
html.ts
@ -19,7 +19,6 @@ function htmlLayout({ title, body, metaHtml }: HtmlLayoutOptions): string {
|
|||||||
<title>${title}</title>
|
<title>${title}</title>
|
||||||
${metaHtml || ""}
|
${metaHtml || ""}
|
||||||
<link rel="stylesheet" type="text/css" href="/assets/css/layout.css" />
|
<link rel="stylesheet" type="text/css" href="/assets/css/layout.css" />
|
||||||
<link rel="stylesheet" type="text/css" href="/assets/css/prism.css" />
|
|
||||||
<link rel="stylesheet" type="text/css" href="/assets/css/themes/default-light.css" />
|
<link rel="stylesheet" type="text/css" href="/assets/css/themes/default-light.css" />
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
@ -114,6 +113,9 @@ export function articleListHtml(articles: Article[]): string {
|
|||||||
function userAddressHtml(profile: Profile) {
|
function userAddressHtml(profile: Profile) {
|
||||||
let html = "";
|
let html = "";
|
||||||
|
|
||||||
|
if (profile.nip05) {
|
||||||
|
html += `<dt>Nostr address</dt><dd>${profile.nip05}</dd>\n`;
|
||||||
|
}
|
||||||
if (profile.lud16) {
|
if (profile.lud16) {
|
||||||
html += `<dt>Lightning address</dt><dd>${profile.lud16}</dd>\n`;
|
html += `<dt>Lightning address</dt><dd>${profile.lud16}</dd>\n`;
|
||||||
}
|
}
|
||||||
@ -210,8 +212,7 @@ function feedLinksHtml(profile: Profile) {
|
|||||||
function profileMetaHtml(profile: Profile) {
|
function profileMetaHtml(profile: Profile) {
|
||||||
return `
|
return `
|
||||||
<link rel="icon" href="${profile.avatarImageUrl}" type="image/png">
|
<link rel="icon" href="${profile.avatarImageUrl}" type="image/png">
|
||||||
<link rel="me" type="application/nostr+json" href="nostr:${profile.nprofile}" title="${profile.name}">
|
<link rel="alternate" type="application/nostr+json" href="nostr:${profile.npub}" title="${profile.name} on Nostr">
|
||||||
<link rel="alternate" type="application/nostr+json" href="nostr:${profile.nprofile}" title="${profile.name} on Nostr">
|
|
||||||
<meta property="og:url" content="${profile.profileUrl}">
|
<meta property="og:url" content="${profile.profileUrl}">
|
||||||
<meta property="og:type" content="website">
|
<meta property="og:type" content="website">
|
||||||
<meta property="og:title" content="${profile.name} on Nostr">
|
<meta property="og:title" content="${profile.name} on Nostr">
|
||||||
@ -232,8 +233,6 @@ function articleMetaHtml(article: Article, profile: Profile) {
|
|||||||
return `
|
return `
|
||||||
<link rel="icon" href="${profile.avatarImageUrl}" type="image/png">
|
<link rel="icon" href="${profile.avatarImageUrl}" type="image/png">
|
||||||
<link rel="alternate" type="application/nostr+json" href="nostr:${article.naddr}" title="This article on Nostr">
|
<link rel="alternate" type="application/nostr+json" href="nostr:${article.naddr}" title="This article on Nostr">
|
||||||
<link rel="author" type="text/html" href="${profile.profileUrl}" title="${profile.name}">
|
|
||||||
<link rel="author" type="application/nostr+json" href="nostr:${profile.nprofile}" title="${profile.name}">
|
|
||||||
<meta property="og:url" content="${article.url}">
|
<meta property="og:url" content="${article.url}">
|
||||||
<meta property="og:type" content="website">
|
<meta property="og:type" content="website">
|
||||||
<meta property="og:title" content="${article.title}">
|
<meta property="og:title" content="${article.title}">
|
||||||
|
|||||||
10
ldap.ts
10
ldap.ts
@ -8,9 +8,7 @@ if (ldapEnabled) {
|
|||||||
client = new Client({ url: ldap.url as string });
|
client = new Client({ url: ldap.url as string });
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function lookupPubkeyByUsername(
|
export async function lookupPubkeyByUsername(username: string): Promise<string | undefined> {
|
||||||
username: string,
|
|
||||||
): Promise<string | undefined> {
|
|
||||||
let pubkey: string | undefined;
|
let pubkey: string | undefined;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -37,9 +35,7 @@ export async function lookupPubkeyByUsername(
|
|||||||
return pubkey;
|
return pubkey;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function lookupUsernameByPubkey(
|
export async function lookupUsernameByPubkey(pubkey: string): Promise<string | undefined> {
|
||||||
pubkey: string,
|
|
||||||
): Promise<string | undefined> {
|
|
||||||
let username: string | undefined;
|
let username: string | undefined;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -53,8 +49,6 @@ export async function lookupUsernameByPubkey(
|
|||||||
if (searchEntries.length > 0) {
|
if (searchEntries.length > 0) {
|
||||||
username = searchEntries[0].cn.toString();
|
username = searchEntries[0].cn.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
await client.unbind();
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
await client.unbind();
|
await client.unbind();
|
||||||
throw e;
|
throw e;
|
||||||
|
|||||||
@ -4,10 +4,6 @@ import { NostrEvent as NEvent } from "@nostrify/nostrify";
|
|||||||
import { replaceNostrUris } from "../nostr/links.ts";
|
import { replaceNostrUris } from "../nostr/links.ts";
|
||||||
import config from "../config.ts";
|
import config from "../config.ts";
|
||||||
|
|
||||||
for (const language of config.prism.extraLanguages) {
|
|
||||||
await import(`npm:prismjs@1.29.0/components/prism-${language}.js`);
|
|
||||||
}
|
|
||||||
|
|
||||||
export default class Article {
|
export default class Article {
|
||||||
event: NEvent;
|
event: NEvent;
|
||||||
|
|
||||||
|
|||||||
@ -59,13 +59,6 @@ export default class Profile {
|
|||||||
return nip19.npubEncode(this.pubkey);
|
return nip19.npubEncode(this.pubkey);
|
||||||
}
|
}
|
||||||
|
|
||||||
get nprofile(): string {
|
|
||||||
return nip19.nprofileEncode({
|
|
||||||
pubkey: this.pubkey,
|
|
||||||
relays: [config.relay_urls[0]],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
get profileUrl(): string {
|
get profileUrl(): string {
|
||||||
return `${config.base_url}/@${this.username}`;
|
return `${config.base_url}/@${this.username}`;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -35,26 +35,22 @@ export async function nostrUriToUrl(uri: string): Promise<string> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function replaceNostrUris(markdown: string): Promise<string> {
|
export async function replaceNostrUris(markdown: string): Promise<string> {
|
||||||
const protectedRegex =
|
const protectedRegex = /(`{3,}[\s\S]*?`{3,})|(`[^`]*`)|(<pre>[\s\S]*?<\/pre>)|(https?:\/\/[^\s<>"']+)/gi;
|
||||||
/(`{3,}[\s\S]*?`{3,})|(`[^`]*`)|(<pre>[\s\S]*?<\/pre>)|(https?:\/\/[^\s<>"']+)/gi;
|
|
||||||
|
|
||||||
// Split text into segments: unprotected text and protected areas (code blocks, URLs)
|
// Split text into segments: unprotected text and protected areas (code blocks, URLs)
|
||||||
const segments: string[] = [];
|
const segments: string[] = [];
|
||||||
let lastIndex = 0;
|
let lastIndex = 0;
|
||||||
|
|
||||||
markdown.replace(
|
markdown.replace(protectedRegex, (match, _fencedCode, _inlineCode, _preTag, _url, index) => {
|
||||||
protectedRegex,
|
segments.push(markdown.slice(lastIndex, index));
|
||||||
(match, _fencedCode, _inlineCode, _preTag, _url, index) => {
|
segments.push(match);
|
||||||
segments.push(markdown.slice(lastIndex, index));
|
lastIndex = index + match.length;
|
||||||
segments.push(match);
|
return match;
|
||||||
lastIndex = index + match.length;
|
});
|
||||||
return match;
|
|
||||||
},
|
|
||||||
);
|
|
||||||
segments.push(markdown.slice(lastIndex));
|
segments.push(markdown.slice(lastIndex));
|
||||||
|
|
||||||
// Process each segment
|
// Process each segment
|
||||||
let result = "";
|
let result = '';
|
||||||
for (let i = 0; i < segments.length; i++) {
|
for (let i = 0; i < segments.length; i++) {
|
||||||
if (i % 2 === 1 || protectedRegex.test(segments[i])) {
|
if (i % 2 === 1 || protectedRegex.test(segments[i])) {
|
||||||
// Protected segment (code block or URL), leave unchanged
|
// Protected segment (code block or URL), leave unchanged
|
||||||
@ -82,9 +78,7 @@ async function processUnprotectedText(text: string): Promise<string> {
|
|||||||
parts.push(await replaceUrisInText(text.slice(lastIndex, match.index)));
|
parts.push(await replaceUrisInText(text.slice(lastIndex, match.index)));
|
||||||
|
|
||||||
// Process the link target
|
// Process the link target
|
||||||
if (
|
if (nostrUriRegex.test(target) && target.match(nostrUriRegex)![0] === target) {
|
||||||
nostrUriRegex.test(target) && target.match(nostrUriRegex)![0] === target
|
|
||||||
) {
|
|
||||||
// Target is a Nostr URI, replace with resolved URL
|
// Target is a Nostr URI, replace with resolved URL
|
||||||
const resolvedUrl = await nostrUriToUrl(target);
|
const resolvedUrl = await nostrUriToUrl(target);
|
||||||
parts.push(`[${linkText}](${resolvedUrl})`);
|
parts.push(`[${linkText}](${resolvedUrl})`);
|
||||||
@ -99,13 +93,12 @@ async function processUnprotectedText(text: string): Promise<string> {
|
|||||||
// Add any remaining text after the last link
|
// Add any remaining text after the last link
|
||||||
parts.push(await replaceUrisInText(text.slice(lastIndex)));
|
parts.push(await replaceUrisInText(text.slice(lastIndex)));
|
||||||
|
|
||||||
return parts.join("");
|
return parts.join('');
|
||||||
}
|
}
|
||||||
|
|
||||||
async function replaceUrisInText(text: string): Promise<string> {
|
async function replaceUrisInText(text: string): Promise<string> {
|
||||||
let modifiedText = text;
|
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
|
// Collect all replacements for bare Nostr URIs
|
||||||
let match;
|
let match;
|
||||||
@ -124,8 +117,7 @@ async function replaceUrisInText(text: string): Promise<string> {
|
|||||||
// Apply replacements from right to left to avoid index shifting
|
// Apply replacements from right to left to avoid index shifting
|
||||||
for (let i = replacements.length - 1; i >= 0; i--) {
|
for (let i = replacements.length - 1; i >= 0; i--) {
|
||||||
const { start, end, replacement } = replacements[i];
|
const { start, end, replacement } = replacements[i];
|
||||||
modifiedText = modifiedText.slice(0, start) + replacement +
|
modifiedText = modifiedText.slice(0, start) + replacement + modifiedText.slice(end);
|
||||||
modifiedText.slice(end);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return modifiedText;
|
return modifiedText;
|
||||||
@ -133,5 +125,5 @@ async function replaceUrisInText(text: string): Promise<string> {
|
|||||||
|
|
||||||
function cleanUriForTitle(uri: string): string {
|
function cleanUriForTitle(uri: string): string {
|
||||||
// Remove "nostr:" prefix, keep "@" for the title
|
// Remove "nostr:" prefix, keep "@" for the title
|
||||||
return uri.startsWith("nostr:") ? uri.replace(/^nostr:/, "") : uri;
|
return uri.startsWith('nostr:') ? uri.replace(/^nostr:/, '') : uri;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -69,7 +69,7 @@ describe("Article", () => {
|
|||||||
describe("#naddr", () => {
|
describe("#naddr", () => {
|
||||||
it("returns a bech32 addressable event ID", () => {
|
it("returns a bech32 addressable event ID", () => {
|
||||||
expect(article.naddr).toMatch(
|
expect(article.naddr).toMatch(
|
||||||
/^naddr1qvzqqqr4gupzq8meqkx80g3yuklzymy0qf/,
|
/naddr1qvzqqqr4gupzq8meqkx80g3yuklzymy0qf/,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -40,12 +40,4 @@ describe("Profile", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("#nprofile", () => {
|
|
||||||
it("returns a bech32 profile ID", () => {
|
|
||||||
expect(profile.nprofile).toMatch(
|
|
||||||
/^nprofile1qyt8wumn8ghj7mn0wd68ytntdaek6mmn9ehhyecqyq0hjpvvw73zfed7yf/,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|||||||
@ -3,94 +3,59 @@ import { expect } from "@std/expect";
|
|||||||
import { replaceNostrUris } from "../../nostr/links.ts";
|
import { replaceNostrUris } from "../../nostr/links.ts";
|
||||||
|
|
||||||
describe("Nostr links", () => {
|
describe("Nostr links", () => {
|
||||||
|
|
||||||
describe("#replaceNostrUris", () => {
|
describe("#replaceNostrUris", () => {
|
||||||
let mdContent: string;
|
let mdContent: string;
|
||||||
let result: string;
|
let result: string;
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
mdContent = Deno.readTextFileSync("tests/fixtures/article-2.md");
|
mdContent = Deno.readTextFileSync("tests/fixtures/article-2.md"),
|
||||||
result = await replaceNostrUris(mdContent);
|
result = await replaceNostrUris(mdContent);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("does not replace URIs in URLs", () => {
|
it("does not replace URIs in URLs", () => {
|
||||||
expect(result).toMatch(
|
expect(result).toMatch(new RegExp("https://badges.page/p/npub1cpmvpsqtzxl4px44dp4544xwgu0ryv2lscl3qexq42dfakuza02s4fsapc"));
|
||||||
new RegExp(
|
|
||||||
"https://badges.page/p/npub1cpmvpsqtzxl4px44dp4544xwgu0ryv2lscl3qexq42dfakuza02s4fsapc",
|
|
||||||
),
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("does not replace URIs in fenced code blocks", () => {
|
it("does not replace URIs in fenced code blocks", () => {
|
||||||
expect(result).toMatch(
|
expect(result).toMatch(new RegExp("Follow nostr:npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7"));
|
||||||
new RegExp(
|
|
||||||
"Follow nostr:npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7",
|
|
||||||
),
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("does not replace URIs in inline code blocks", () => {
|
it("does not replace URIs in inline code blocks", () => {
|
||||||
expect(result).toMatch(
|
expect(result).toMatch(new RegExp("raucao: nostr:npub1raustrrh5gjwt03zdj8syn9vmt2dwsv9t467m8c3gua636uxu89svgdees"));
|
||||||
new RegExp(
|
|
||||||
"raucao: nostr:npub1raustrrh5gjwt03zdj8syn9vmt2dwsv9t467m8c3gua636uxu89svgdees",
|
|
||||||
),
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("for unknown usernames", () => {
|
describe("for unknown usernames", () => {
|
||||||
it("replaces plain nostr:id URIs with a markdown link", () => {
|
it("replaces plain nostr:id URIs with a markdown link", () => {
|
||||||
expect(result).toMatch(
|
expect(result).toMatch(/Amber scheme 1\: \[npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\]\(https\:\/\/njump\.me\/npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\)/);
|
||||||
/Amber scheme 1\: \[npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\]\(https\:\/\/njump\.me\/npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\)/,
|
expect(result).toMatch(/Amber scheme 2\: \[npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\]\(https\:\/\/njump\.me\/npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\)/);
|
||||||
);
|
|
||||||
expect(result).toMatch(
|
|
||||||
/Amber scheme 2\: \[npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\]\(https\:\/\/njump\.me\/npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\)/,
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("replaces @id URIs with a markdown link", () => {
|
it("replaces @id URIs with a markdown link", () => {
|
||||||
expect(result).toMatch(
|
expect(result).toMatch(/Amber at 1\: \[@npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\]\(https\:\/\/njump\.me\/npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\)/);
|
||||||
/Amber at 1\: \[@npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\]\(https\:\/\/njump\.me\/npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\)/,
|
expect(result).toMatch(/Amber at 2\: \[@npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\]\(https\:\/\/njump\.me\/npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\)/);
|
||||||
);
|
|
||||||
expect(result).toMatch(
|
|
||||||
/Amber at 2\: \[@npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\]\(https\:\/\/njump\.me\/npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\)/,
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("replaces nostr links with external links", () => {
|
it("replaces nostr links with external links", () => {
|
||||||
expect(result).toMatch(
|
expect(result).toMatch(/Amber scheme link 1\: \[Amber\]\(https\:\/\/njump\.me\/npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\)/);
|
||||||
/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 2\: \[Amber\]\(https\:\/\/njump\.me\/npub1am3ermkr250dywukzqnaug64cred3x5jht6f3kdhfp3h0rgtjlpqecxrv7\)/,
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("for known usernames", () => {
|
describe("for known usernames", () => {
|
||||||
it("replaces plain nostr:id URIs with a markdown link", () => {
|
it("replaces plain nostr:id URIs with a markdown link", () => {
|
||||||
expect(result).toMatch(
|
expect(result).toMatch(/raucao scheme 1\: \[npub1raustrrh5gjwt03zdj8syn9vmt2dwsv9t467m8c3gua636uxu89svgdees\]\(\/@raucao\)/);
|
||||||
/raucao scheme 1\: \[npub1raustrrh5gjwt03zdj8syn9vmt2dwsv9t467m8c3gua636uxu89svgdees\]\(\/@raucao\)/,
|
expect(result).toMatch(/raucao scheme 2\: \[npub1raustrrh5gjwt03zdj8syn9vmt2dwsv9t467m8c3gua636uxu89svgdees\]\(\/@raucao\)/);
|
||||||
);
|
|
||||||
expect(result).toMatch(
|
|
||||||
/raucao scheme 2\: \[npub1raustrrh5gjwt03zdj8syn9vmt2dwsv9t467m8c3gua636uxu89svgdees\]\(\/@raucao\)/,
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("replaces @id URIs with a markdown link", () => {
|
it("replaces @id URIs with a markdown link", () => {
|
||||||
expect(result).toMatch(
|
expect(result).toMatch(/raucao at 1\: \[@npub1raustrrh5gjwt03zdj8syn9vmt2dwsv9t467m8c3gua636uxu89svgdees\]\(\/@raucao\)/);
|
||||||
/raucao at 1\: \[@npub1raustrrh5gjwt03zdj8syn9vmt2dwsv9t467m8c3gua636uxu89svgdees\]\(\/@raucao\)/,
|
expect(result).toMatch(/raucao at 2\: \[@npub1raustrrh5gjwt03zdj8syn9vmt2dwsv9t467m8c3gua636uxu89svgdees\]\(\/@raucao\)/);
|
||||||
);
|
|
||||||
expect(result).toMatch(
|
|
||||||
/raucao at 2\: \[@npub1raustrrh5gjwt03zdj8syn9vmt2dwsv9t467m8c3gua636uxu89svgdees\]\(\/@raucao\)/,
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("replaces scheme links with internal links", () => {
|
it("replaces scheme links with internal links", () => {
|
||||||
expect(result).toMatch(
|
expect(result).toMatch(/raucao scheme link 1\: \[raucao\]\(\/@raucao\)/);
|
||||||
/raucao scheme link 1\: \[raucao\]\(\/@raucao\)/,
|
expect(result).toMatch(/raucao scheme link 2\: \[raucao\]\(\/@raucao\)/);
|
||||||
);
|
|
||||||
expect(result).toMatch(
|
|
||||||
/raucao scheme link 2\: \[raucao\]\(\/@raucao\)/,
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user