diff --git a/bun.lock b/bun.lock index 84d4ba8e..db8636c5 100644 --- a/bun.lock +++ b/bun.lock @@ -18,9 +18,10 @@ "@sveltejs/vite-plugin-svelte": "^6.2.4", "@tailwindcss/container-queries": "^0.1.1", "@tailwindcss/vite": "^4.1.18", + "@types/node": "^25.2.2", "@types/nprogress": "^0.2.3", "@types/ua-parser-js": "^0.7.39", - "@xylightdev/svelte-hero-icons": "^2.2.7", + "@xylightdev/svelte-hero-icons": "^2.2.9", "eslint": "^9.39.2", "eslint-plugin-svelte": "^3.14.0", "jsdom": "^22.1.0", @@ -357,6 +358,8 @@ "@types/linkify-it": ["@types/linkify-it@5.0.0", "", {}, "sha512-sVDA58zAw4eWAffKOaQH5/5j3XeayukzDk+ewSsnv3p4yJEZHCCzMDiZM8e0OUrRvmpGZ85jf4yDHkHsgBNr9Q=="], + "@types/node": ["@types/node@25.2.2", "", { "dependencies": { "undici-types": "~7.16.0" } }, "sha512-BkmoP5/FhRYek5izySdkOneRyXYN35I860MFAGupTdebyE66uZaR+bXLHq8k4DirE5DwQi3NuhvRU1jqTVwUrQ=="], + "@types/nprogress": ["@types/nprogress@0.2.3", "", {}, "sha512-k7kRA033QNtC+gLc4VPlfnue58CM1iQLgn1IMAU8VPHGOj7oIHPp9UlhedEnD/Gl8evoCjwkZjlBORtZ3JByUA=="], "@types/resolve": ["@types/resolve@1.20.2", "", {}, "sha512-60BCwRFOZCQhDncwQdxxeOEEkbc5dIMccYLwbxsS4TUNeVECQ/pBJ0j09mrHOl/JJvpRPGwO9SvE4nR2Nb/a4Q=="], @@ -383,7 +386,7 @@ "@typescript-eslint/visitor-keys": ["@typescript-eslint/visitor-keys@8.54.0", "", { "dependencies": { "@typescript-eslint/types": "8.54.0", "eslint-visitor-keys": "^4.2.1" } }, "sha512-VFlhGSl4opC0bprJiItPQ1RfUhGDIBokcPwaFH4yiBCaNPeld/9VeXbiPO1cLyorQi1G1vL+ecBk1x8o1axORA=="], - "@xylightdev/svelte-hero-icons": ["@xylightdev/svelte-hero-icons@2.2.7", "", { "peerDependencies": { "svelte": "^5.0.0" } }, "sha512-txlrlCf68r6DE43hITVN/ep8dYP2ops0AVtW+HAxIdWgAkae/L7fFJtbhb9iZIDSgHB7HV/5eV+UCVGdEXz3xw=="], + "@xylightdev/svelte-hero-icons": ["@xylightdev/svelte-hero-icons@2.2.9", "", { "peerDependencies": { "svelte": "^5.0.0" } }, "sha512-P3cQPL8e3LNGLq1F4Pltg6/iMMf68wHmO89ovBmNl35WPu3KT3Ne4cv4OWLsQK2W0RgbSpzHbCr5ksBSnXISHA=="], "abab": ["abab@2.0.6", "", {}, "sha512-j2afSsaIENvHZN2B8GOpF566vZ5WVk5opAiMTvWgaQT8DkbOqsTfvNAvHoRGU2zzP8cPoqys+xHTRDWW8L+/BA=="], @@ -805,6 +808,8 @@ "undici": ["undici@7.18.2", "", {}, "sha512-y+8YjDFzWdQlSE9N5nzKMT3g4a5UBX1HKowfdXh0uvAnTaqqwqB92Jt4UXBAeKekDs5IaDKyJFR4X1gYVCgXcw=="], + "undici-types": ["undici-types@7.16.0", "", {}, "sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw=="], + "unenv": ["unenv@2.0.0-rc.24", "", { "dependencies": { "pathe": "^2.0.3" } }, "sha512-i7qRCmY42zmCwnYlh9H2SvLEypEFGye5iRmEMKjcGi7zk9UquigRjFtTLz0TYqr0ZGLZhaMHl/foy1bZR+Cwlw=="], "universalify": ["universalify@0.2.0", "", {}, "sha512-CJ1QgKmNg3CwvAv/kOFmtnEN05f0D/cn9QntgNOQlQF9dgvVTHj3t+8JPdjqawCHk7V/KA+fbUqzZ9XWhcqPUg=="], diff --git a/package.json b/package.json index 1b6142bd..ad08e28d 100644 --- a/package.json +++ b/package.json @@ -16,9 +16,10 @@ "@sveltejs/vite-plugin-svelte": "^6.2.4", "@tailwindcss/container-queries": "^0.1.1", "@tailwindcss/vite": "^4.1.18", + "@types/node": "^25.2.2", "@types/nprogress": "^0.2.3", "@types/ua-parser-js": "^0.7.39", - "@xylightdev/svelte-hero-icons": "^2.2.7", + "@xylightdev/svelte-hero-icons": "^2.2.10", "eslint": "^9.39.2", "eslint-plugin-svelte": "^3.14.0", "jsdom": "^22.1.0", diff --git a/src/lib/api/client.svelte.ts b/src/lib/api/client.svelte.ts index aac1688b..2cb9a068 100644 --- a/src/lib/api/client.svelte.ts +++ b/src/lib/api/client.svelte.ts @@ -101,7 +101,7 @@ export function getClient( export async function validateInstance( instance: string, - type: ClientType, + type: ClientType = DEFAULT_CLIENT_TYPE, ): Promise { if (instance == '') return false diff --git a/src/lib/app/markdown/renderers/plugins.js b/src/lib/app/markdown/renderers/plugins.ts similarity index 72% rename from src/lib/app/markdown/renderers/plugins.js rename to src/lib/app/markdown/renderers/plugins.ts index f8dedf97..c5111ad3 100644 --- a/src/lib/app/markdown/renderers/plugins.js +++ b/src/lib/app/markdown/renderers/plugins.ts @@ -1,14 +1,13 @@ -import { marked } from 'marked' +import { marked, type TokenizerAndRendererExtension, type Tokens } from 'marked' import markedLinkifyIt from 'marked-linkify-it' -export const spoilerPlugin = { +export const spoilerPlugin: TokenizerAndRendererExtension = { name: 'spoiler', level: 'block', - start(src) { + start(src: string) { return src.match(/:::/)?.index }, - // eslint-disable-next-line - tokenizer(src, tokens) { + tokenizer(src: string) { const rule = /::: ?spoiler(?: ?(.*))\n([\s\S]*?)\n:::/ const match = rule.exec(src) if (match) { @@ -17,10 +16,11 @@ export const spoilerPlugin = { raw: match[0], title: match[1].trim(), text: match[2].trim(), + tokens: [], } } }, - renderer(token) { + renderer(token: Tokens.Generic) { return `
${marked.parseInline(token.title)} @@ -30,11 +30,11 @@ export const spoilerPlugin = { }, } -export const linkify = markedLinkifyIt( +export const linkify = (markedLinkifyIt as any)( { '!': { - validate: function (text, pos, self) { - var tail = text.slice(pos) + validate: function (text: string, pos: number, self: any) { + const tail = text.slice(pos) if (!self.re.community) { self.re.community = new RegExp( @@ -47,11 +47,12 @@ export const linkify = markedLinkifyIt( if (pos >= 2 && tail[pos - 2] === '!') { return false } - return tail.match(self.re.community)[0].length + const match = tail.match(self.re.community) + return match ? match[0].length : 0 } return 0 }, - normalize: function (match) { + normalize: function (match: any) { let prefix = match.url prefix = prefix.startsWith('c/') ? prefix.slice(2) : prefix.slice(1) @@ -59,8 +60,8 @@ export const linkify = markedLinkifyIt( }, }, '@': { - validate: function (text, pos, self) { - var tail = text.slice(pos) + validate: function (text: string, pos: number, self: any) { + const tail = text.slice(pos) if (!self.re.user) { self.re.user = new RegExp( @@ -73,11 +74,12 @@ export const linkify = markedLinkifyIt( if (pos >= 2 && tail[pos - 2] === '!') { return false } - return tail.match(self.re.user)[0].length + const match = tail.match(self.re.user) + return match ? match[0].length : 0 } return 0 }, - normalize: function (match) { + normalize: function (match: any) { let prefix = match.url prefix = prefix.startsWith('u/') ? prefix.slice(2) : prefix.slice(1) @@ -103,7 +105,7 @@ export { regexes as CONTENT_REGEXES } /** * Convert links to photon links */ -export const photonify = (link) => { +export const photonify = (link: string) => { if (regexes.community.test(link)) { const match = link.match(regexes.community) if (!match) return @@ -139,36 +141,41 @@ export const photonify = (link) => { } } -export function subSupscriptExtension(tokensExtractor) { +export function subSupscriptExtension( + tokensExtractor: (params: any) => Tokens.Generic | null, +): TokenizerAndRendererExtension { return { name: 'subscriptSuperscript', level: 'inline', - start(src) { + start(src: string) { return src.match(/[~^]/)?.index }, - // eslint-disable-next-line - tokenizer(src, tokens) { + tokenizer(this: any, src: string) { const subscriptRule = /^~([^~\s](?:[^~]*[^~\s])?)~/ const superscriptRule = /^\^([^^\s](?:[^^]*[^^\s])?)\^/ let match if ((match = subscriptRule.exec(src))) { - return tokensExtractor({ - type: 'subscript', - content: match[1], - raw: match[0], - lexer: this.lexer, - }) + return ( + tokensExtractor({ + type: 'subscript', + content: match[1], + raw: match[0], + lexer: this.lexer, + }) ?? undefined + ) } if ((match = superscriptRule.exec(src))) { - return tokensExtractor({ - type: 'superscript', - content: match[1], - raw: match[0], - lexer: this.lexer, - }) + return ( + tokensExtractor({ + type: 'superscript', + content: match[1], + raw: match[0], + lexer: this.lexer, + }) ?? undefined + ) } }, } diff --git a/src/lib/app/markdown/renderers/spoiler/spoiler.js b/src/lib/app/markdown/renderers/spoiler/spoiler.js deleted file mode 100644 index a5c9835e..00000000 --- a/src/lib/app/markdown/renderers/spoiler/spoiler.js +++ /dev/null @@ -1,100 +0,0 @@ -/** - * Container extension is a marked extension that parses a block of text surrounded by ::: - * - * @param tokensExtractor Converts the container input into a custom token that svelte components will receive. - * If this function returns a value that contains an array of `tokens`, - * this array will be populated with the tokens that are found inside the container, - * allowing to nest markdown inside the container. - * - * @example - * - * Given the following markdown: - * - * :::container option="option-1" option-2="option-2" boolean-option - * Something inside the container - * ::: - * - * The token extractor would receive the following parameters: - * **type**: `container` - * **content**: `Something inside the container` - * **options**: { 'option': 'option-1', 'option-2': 'option-2', 'boolean-option': true } - */ -export default function (tokensExtractor) { - return { - name: 'container', - level: 'block', - start(src) { - return src.match(/:::[^:\n]/)?.index - }, - tokenizer(src) { - const rule = - /:::[\s]?(?[a-z0-9-]+)(?.*)?\n(?(?:.|\n)*)\n:::(?:\n)*/i - - const match = rule.exec(findRawContainer(src)) - if (!match || !match.groups) return null - - const type = match.groups.type.toLocaleLowerCase() - const options = parseOptions(match.groups.options || '') - const content = match.groups.content || '' - - const result = tokensExtractor({ - type: type, - content: content, - raw: match[0], - options: options, - lexer: this.lexer, - }) - - if (result?.tokens) { - this.lexer.blockTokens(content, result.tokens) - } - - return result - }, - } -} - -function findRawContainer(src) { - if (!src.startsWith(':::')) return undefined - const lines = src.split('\n') - let open = 1 - let lineNumber = 1 - - for (lineNumber = 1; lineNumber < lines.length; lineNumber++) { - const line = lines[lineNumber] - if (line.startsWith(':::')) { - if (/:::[^:\n\s]/.test(line)) { - open++ - } else if (/^:::(\n|$)/.test(line)) { - open -= 1 - } - } - - if (open === 0) { - break - } - } - - return lines.slice(0, lineNumber + 1).join('\n') -} - -function parseOptions(options) { - const output = {} - let remaining = options - - while (true) { - const regex = /(?[a-z0-9]+)(?:="(?(?:.*?))")?/i - const match = regex.exec(remaining) - if (!match) break - - const name = match?.groups?.name - - if (name) { - output[name] = match?.groups?.value ?? true - } - - remaining = remaining.slice(match.index + match[0].length) - } - - return output -} diff --git a/src/lib/app/markdown/renderers/spoiler/spoiler.ts b/src/lib/app/markdown/renderers/spoiler/spoiler.ts new file mode 100644 index 00000000..449bd6fa --- /dev/null +++ b/src/lib/app/markdown/renderers/spoiler/spoiler.ts @@ -0,0 +1,57 @@ +import type { TokenizerAndRendererExtension, Tokens } from 'marked' + +/** + * spoiler extension for marked + */ +export default function ( + tokensExtractor: (params: any) => Tokens.Generic | null, +): TokenizerAndRendererExtension { + return { + name: 'spoiler', + level: 'block', + start(src: string) { + return src.match(/:::[^:\n]/)?.index + }, + tokenizer(this: any, src: string) { + const rule = + /^:::[ \t]*spoiler[ \t]*([^\n]*)\n([\s\S]*?)\n[ \t]*:::[ \t]*(?:\n|$)/ + const match = rule.exec(src) + + if (match) { + const title = match[1].trim() + const content = match[2].trim() + + const options = parseOptions(title) + + const result = tokensExtractor({ + type: 'spoiler', + raw: match[0], + content: content, + options: options, + lexer: this.lexer, + }) + + if (result?.tokens) { + this.lexer.blockTokens(content, result.tokens) + } + + return result ?? undefined + } + }, + } +} + +function parseOptions(options: string) { + const output: Record = {} + + const rule = /([a-z0-9]+)(?:="([^"]*)")?/gi + let match + while ((match = rule.exec(options)) !== null) { + const name = match[1] + if (name) { + output[name] = match?.groups?.value ?? true + } + } + + return output +} diff --git a/src/lib/feature/community/CommunityItem.svelte b/src/lib/feature/community/CommunityItem.svelte index 941c44fc..fe268ef2 100644 --- a/src/lib/feature/community/CommunityItem.svelte +++ b/src/lib/feature/community/CommunityItem.svelte @@ -57,40 +57,29 @@ > {#if !children} {#if community.community.deleted} - + + + {/if} {#if community.community.nsfw} - + + + {/if} {#if community.banned_from_community} - + + + {/if} {#if community.community.visibility == 'LocalOnly'} - + + + {/if}