fix: resolve all typescript errors in sv check (#715)

This commit is contained in:
xylight authored and GitHub committed 2026-02-08 23:43:50 +00:00
1 parent 2a5fb886ae
commit 7bddf16559
38 files changed
+233 -287

No files matched your search

+1 -1
View File
@@ -101,7 +101,7 @@ export function getClient(
export async function validateInstance(
instance: string,
type: ClientType,
type: ClientType = DEFAULT_CLIENT_TYPE,
): Promise<boolean> {
if (instance == '') return false
@@ -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 `
<details>
<summary>${marked.parseInline(token.title)}</summary>
@@ -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
)
}
},
}
@@ -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]?(?<type>[a-z0-9-]+)(?<options>.*)?\n(?<content>(?:.|\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 = /(?<name>[a-z0-9]+)(?:="(?<value>(?:.*?))")?/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
}
@@ -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<string, string | boolean> = {}
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
}
+17 -28
View File
@@ -57,40 +57,29 @@
>
{#if !children}
{#if community.community.deleted}
<Icon
src={Trash}
class="text-red-500 inline"
micro
size="14"
title={$t('post.badges.deleted')}
/>
<span title={$t('post.badges.deleted')}>
<Icon src={Trash} class="text-red-500 inline" micro size="14" />
</span>
{/if}
{#if community.community.nsfw}
<Icon
src={ExclamationTriangle}
class="text-red-500 inline"
micro
size="14"
title={$t('post.badges.nsfw')}
/>
<span title={$t('post.badges.nsfw')}>
<Icon
src={ExclamationTriangle}
class="text-red-500 inline"
micro
size="14"
/>
</span>
{/if}
{#if community.banned_from_community}
<Icon
src={NoSymbol}
class="text-red-500 inline"
micro
size="14"
title={$t('comment.banned')}
/>
<span title={$t('comment.banned')}>
<Icon src={NoSymbol} class="text-red-500 inline" micro size="14" />
</span>
{/if}
{#if community.community.visibility == 'LocalOnly'}
<Icon
src={MapPin}
class="text-green-500 inline"
micro
size="14"
title={$t('routes.admin.config.listingType.local')}
/>
<span title={$t('routes.admin.config.listingType.local')}>
<Icon src={MapPin} class="text-green-500 inline" micro size="14" />
</span>
{/if}
<Button
rounding="xl"
@@ -89,40 +89,29 @@
/>
</div>
{#if community.community.deleted}
<Icon
src={Trash}
class="text-red-500 inline"
micro
size="14"
title={$t('post.badges.deleted')}
/>
<span title={$t('post.badges.deleted')}>
<Icon src={Trash} class="text-red-500 inline" micro size="14" />
</span>
{/if}
{#if community.community.nsfw}
<Icon
src={ExclamationTriangle}
class="text-red-500 inline"
micro
size="14"
title={$t('post.badges.nsfw')}
/>
<span title={$t('post.badges.nsfw')}>
<Icon
src={ExclamationTriangle}
class="text-red-500 inline"
micro
size="14"
/>
</span>
{/if}
{#if community.banned_from_community}
<Icon
src={NoSymbol}
class="text-red-500 inline"
micro
size="14"
title={$t('comment.banned')}
/>
<span title={$t('comment.banned')}>
<Icon src={NoSymbol} class="text-red-500 inline" micro size="14" />
</span>
{/if}
{#if community.community.visibility == 'LocalOnly'}
<Icon
src={MapPin}
class="text-green-500 inline"
micro
size="14"
title={$t('routes.admin.config.listingType.local')}
/>
<span title={$t('routes.admin.config.listingType.local')}>
<Icon src={MapPin} class="text-green-500 inline" micro size="14" />
</span>
{/if}
<div class="flex gap-2 items-center justify-end w-full z-30 mt-auto">
<Button
@@ -67,13 +67,14 @@
{/if}
{#if badges}
{#if badges.nsfw}
<Icon
src={ExclamationTriangle}
size="14"
micro
title={$t('post.badges.nsfw')}
class="text-red-600 dark:text-red-400"
/>
<span title={$t('post.badges.nsfw')}>
<Icon
src={ExclamationTriangle}
size="14"
micro
class="text-red-600 dark:text-red-400"
/>
</span>
{/if}
{/if}
</a>
+1 -1
View File
@@ -5,6 +5,7 @@ import type {
Community,
CommunityView,
PersonView,
Post,
PostReportView,
PostView,
PrivateMessage,
@@ -13,7 +14,6 @@ import type {
} from '$lib/api/types'
import { profile } from '$lib/app/auth'
import { writable } from 'svelte/store'
import type { Post } from '../post'
export type Result =
| PostView
@@ -199,7 +199,6 @@
comment_view: item,
depth: 1,
}}
postId={item.post.id}
actions={false}
/>
{:else if isPostView(item)}
+1 -1
View File
@@ -118,7 +118,7 @@ export class PostFormState {
).post_view
}
if (api instanceof PiefedClient) {
if (api instanceof PiefedClient && api.assignFlair) {
const flairRes = await api.assignFlair({
flair_id_list: this.flairList.map((i) => i.id),
post_id: res.post.id,
+3 -1
View File
@@ -103,6 +103,7 @@
opened?: boolean
autoplay?: boolean
title?: string
class?: string
}
let {
@@ -112,6 +113,7 @@
title,
opened = $bindable(!settings.embeds.clickToView),
autoplay = settings.embeds.clickToView,
class: clazz = '',
}: Props = $props()
let data = $derived(typeData(type))
@@ -122,7 +124,7 @@
@component
Displays a video file or embedded video iframe.
-->
<div class="iframe-container">
<div class={['iframe-container', clazz]}>
{#if opened}
{#if type == 'video'}
<video {autoplay} controls>
+1 -1
View File
@@ -91,7 +91,7 @@
>
{#if post.alt_text}
<Button
onclick={(e) => {
onclick={(e: MouseEvent) => {
e.stopPropagation()
modal({
title: 'Alt',
+12 -10
View File
@@ -60,16 +60,18 @@
canVote = false
if (typeof id !== 'number') {
await api.voteOnPoll({
post_id: post.id,
choice_id: id,
})
} else {
await api.voteOnPoll({
post_id: post.id,
choice_id: [id],
})
if (api.voteOnPoll) {
if (typeof id !== 'number') {
await api.voteOnPoll({
post_id: post.id,
choice_id: id,
})
} else {
await api.voteOnPoll({
post_id: post.id,
choice_id: [id],
})
}
}
} catch (err) {
console.error(err)
+2 -8
View File
@@ -10,11 +10,10 @@
let switching: boolean = $state(false)
interface Props {
prof: ProfileInfo
index: number
guest?: boolean
}
let { prof, index, guest = false }: Props = $props()
let { prof, guest = false }: Props = $props()
</script>
<SidebarButton
@@ -39,12 +38,7 @@
class="w-full font-normal"
>
{#snippet customIcon()}
<ProfileAvatar
profile={prof}
{index}
{guest}
selected={profile.current?.id == prof.id}
/>
<ProfileAvatar profile={prof} selected={profile.current?.id == prof.id} />
{/snippet}
<span
class="inline-flex flex-col gap-0 {profile.current?.id == prof.id
+4 -1
View File
@@ -15,7 +15,8 @@
showWhenEmpty?: boolean
hideOwnUser?: boolean
placeholder?: string
onselect?: (e: Person) => void
label?: string
onselect?: (e?: Person) => void
}
let {
@@ -24,6 +25,7 @@
listing_type = 'Subscribed',
showWhenEmpty = false,
hideOwnUser = false,
label,
...rest
}: Props = $props()
@@ -50,6 +52,7 @@
}}
extractName={(c) => `${c.name}@${new URL(c.actor_id).hostname}`}
bind:query={q}
{label}
{...rest}
>
{#snippet noresults()}
+6 -3
View File
@@ -32,10 +32,13 @@
: ''}
{url != undefined ? 'rounded-b-none' : ''}
"
ondrop={preventDefault((event) => (files = event.dataTransfer?.files))}
ondrop={preventDefault(
(event) => (files = (event as DragEvent).dataTransfer?.files ?? null),
)}
ondragover={preventDefault((event) => {
if (event.dataTransfer) {
event.dataTransfer.dropEffect = 'copy'
const dragEvent = event as DragEvent
if (dragEvent.dataTransfer) {
dragEvent.dataTransfer.dropEffect = 'copy'
dragover = true
}
})}
+3
View File
@@ -13,6 +13,7 @@
children?: import('svelte').Snippet
style?: 'header' | 'subpage'
margin?: boolean
class?: string
}
let {
@@ -22,6 +23,7 @@
children,
style = 'header',
margin = true,
class: clazz = '',
}: Props = $props()
let matchType = $derived<('search' | 'pathname')[]>(
@@ -59,6 +61,7 @@
style == 'header' ? 'bar-header' : 'gap-2',
margin && 'my-2',
margin && style == 'subpage' && 'mb-3',
clazz,
]}
>
{#each routes as route, index (route.href)}
+3 -8
View File
@@ -2,11 +2,7 @@
import { Spinner } from 'mono-svelte'
import type { Snippet } from 'svelte'
import { type IconSource, Icon } from 'svelte-hero-icons/dist'
import type {
ClassValue,
HTMLAnchorAttributes,
HTMLButtonAttributes,
} from 'svelte/elements'
import type { ClassValue } from 'svelte/elements'
export type ButtonColor = keyof typeof buttonColor
export type ButtonAlignment = keyof typeof buttonAlignment
@@ -77,8 +73,7 @@
}
type ButtonGap = keyof typeof buttonGap
interface Props
extends Omit<HTMLButtonAttributes | HTMLAnchorAttributes, 'prefix'> {
interface Props extends Record<string, any> {
loading?: boolean
submit?: boolean
type?: 'button' | 'none'
@@ -94,7 +89,7 @@
prefix?: Snippet
children?: Snippet
suffix?: Snippet
onclick?: HTMLButtonAttributes['onclick']
onclick?: any
icon?: IconSource
weight?: ButtonWeight
}
@@ -9,7 +9,7 @@
title={$shownModal.title}
dismissable={$shownModal.dismissable}
ondismissed={() => shownModal.set(undefined)}
bind:open={$shownModal}
open={!!$shownModal}
>
{#if $shownModal.snippet}
{@render $shownModal.snippet?.()}
+3 -2
View File
@@ -9,7 +9,7 @@
selected?: T | undefined
search: (query: string) => Promise<T[]>
extractName: (item: T) => string
select?: (item: T) => void
select?: (item?: T) => void
input?: import('svelte').Snippet
noresults?: import('svelte').Snippet
required?: boolean
@@ -43,7 +43,8 @@
selected = $bindable(undefined),
search,
extractName,
select = (item: T) => {
select = (item?: T) => {
if (item == null) return
selected = item
query = extractName(item)
onselect?.(item)