Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"json-with-bigint": "^3.4.4",
"solid-js": "^1.9.6",
"stoat-api": "0.8.9-4",
"ulid": "^2.4.0"
"ulid": "^3.0.2"
},
"devDependencies": {
"@mxssfd/typedoc-theme": "^1.1.7",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

102 changes: 100 additions & 2 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ import type { HydratedMessage } from "./hydration/message.js";
import type { HydratedServer } from "./hydration/server.js";
import type { HydratedServerMember } from "./hydration/serverMember.js";
import type { HydratedUser } from "./hydration/user.js";
import { RE_CHANNELS, RE_MENTIONS, RE_SPOILER } from "./lib/regex.js";
import {
RE_CHANNELS,
RE_CUSTOM_EMOJI,
RE_MENTIONS,
RE_SPOILER,
} from "./lib/regex.js";

export type Session = { _id: string; token: string; user_id: string } | string;

Expand Down Expand Up @@ -242,7 +247,9 @@ export class Client extends AsyncEventEmitter<Events> {
baseURL: this.options.baseURL,
});

const [configured, setConfigured] = createSignal(configuration !== undefined);
const [configured, setConfigured] = createSignal(
configuration !== undefined,
);
this.configured = configured;
this.#setConfigured = setConfigured;

Expand Down Expand Up @@ -416,6 +423,97 @@ export class Client extends AsyncEventEmitter<Events> {
.replace(RE_SPOILER, "<spoiler>");
}

/**
* Prepare a markdown-based message to be displayed to the user as plain text. This method will fetch each user or channel if they are missing. Useful for serviceworkers.
* @param source Source markdown text
* @returns Modified plain text
*/
async markdownToTextFetch(source: string): Promise<string> {
// Get all user matches, create a map to dedupe
const userMatches = Object.fromEntries(
Array.from(source.matchAll(RE_MENTIONS), (match) => {
return [match[0], match[1]];
}),
);

// Get all channel matches, create a map to dedupe
const channelMatches = Object.fromEntries(
Array.from(source.matchAll(RE_CHANNELS), (match) => {
return [match[0], match[1]];
}),
);

// Get all custom emoji matches, create a map to dedupe
const customEmojiMatches = Object.fromEntries(
Array.from(source.matchAll(RE_CUSTOM_EMOJI), (match) => {
return [match[0], match[1]];
}),
);

// Send requests to replace user ids
const userReplacementPromises = Object.keys(userMatches).map(
async (key) => {
const substr = userMatches[key];
if (substr) {
const user = await this.users.fetch(substr);

if (user) {
return [key, `@${user.username}`];
}
}

return [key, key];
},
);

// Send requests to replace channel ids
const channelReplacementPromises = Object.keys(channelMatches).map(
async (key) => {
const substr = channelMatches[key];
if (substr) {
const channel = await this.channels.fetch(substr);

if (channel) {
return [key, `#${channel.displayName}`];
}
}

return [key, key];
},
);

// Send requests to replace custom emojis
const customEmojiReplacementPromises = Object.keys(customEmojiMatches).map(
async (key) => {
const substr = customEmojiMatches[key];
if (substr) {
const emoji = await this.emojis.fetch(substr);

if (emoji) {
return [key, `:${emoji.name}:`];
}
}

return [key, key];
},
);

// Await for all promises to get the strings to replace with.
const replacements = await Promise.all([
...userReplacementPromises,
...channelReplacementPromises,
...customEmojiReplacementPromises,
]);

const replacementsMap = Object.fromEntries(replacements);

return source
.replace(RE_MENTIONS, (match) => replacementsMap[match])
.replace(RE_CHANNELS, (match) => replacementsMap[match])
.replace(RE_CUSTOM_EMOJI, (match) => replacementsMap[match])
.replace(RE_SPOILER, "<spoiler>");
}

/**
* Proxy a file through January.
* @param url URL to proxy
Expand Down
5 changes: 5 additions & 0 deletions src/lib/regex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export const RE_MENTIONS = /<@([0-9ABCDEFGHJKMNPQRSTVWXYZ]{26})>/g;
*/
export const RE_CHANNELS = /<#([0-9ABCDEFGHJKMNPQRSTVWXYZ]{26})>/g;

/**
* Regular expression for stripping custom emojis.
*/
export const RE_CUSTOM_EMOJI = /:([0-9ABCDEFGHJKMNPQRSTVWXYZ]{26}):/g;

/**
* Regular expression for spoilers.
*/
Expand Down
Loading