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,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
}