Fix a number of typescript issues (#32459)

Fixes 69 typescript errors found in the `admin` and `markup` folders.

---------

Co-authored-by: Giteabot <teabot@gitea.io>
This commit is contained in:
silverwind 2024-11-11 12:13:57 +01:00 committed by GitHub
parent f888e45432
commit f35e2b0cd1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 109 additions and 113 deletions

View file

@ -12,21 +12,20 @@ type ProcessorContext = {
function prepareProcessors(ctx:ProcessorContext): Processors {
const processors = {
H1(el) {
H1(el: HTMLHeadingElement) {
const level = parseInt(el.tagName.slice(1));
el.textContent = `${'#'.repeat(level)} ${el.textContent.trim()}`;
},
STRONG(el) {
STRONG(el: HTMLElement) {
return `**${el.textContent}**`;
},
EM(el) {
EM(el: HTMLElement) {
return `_${el.textContent}_`;
},
DEL(el) {
DEL(el: HTMLElement) {
return `~~${el.textContent}~~`;
},
A(el) {
A(el: HTMLAnchorElement) {
const text = el.textContent || 'link';
const href = el.getAttribute('href');
if (/^https?:/.test(text) && text === href) {
@ -34,7 +33,7 @@ function prepareProcessors(ctx:ProcessorContext): Processors {
}
return href ? `[${text}](${href})` : text;
},
IMG(el) {
IMG(el: HTMLImageElement) {
const alt = el.getAttribute('alt') || 'image';
const src = el.getAttribute('src');
const widthAttr = el.hasAttribute('width') ? ` width="${htmlEscape(el.getAttribute('width') || '')}"` : '';
@ -44,32 +43,29 @@ function prepareProcessors(ctx:ProcessorContext): Processors {
}
return `![${alt}](${src})`;
},
P(el) {
P(el: HTMLParagraphElement) {
el.textContent = `${el.textContent}\n`;
},
BLOCKQUOTE(el) {
BLOCKQUOTE(el: HTMLElement) {
el.textContent = `${el.textContent.replace(/^/mg, '> ')}\n`;
},
OL(el) {
OL(el: HTMLElement) {
const preNewLine = ctx.listNestingLevel ? '\n' : '';
el.textContent = `${preNewLine}${el.textContent}\n`;
},
LI(el) {
LI(el: HTMLElement) {
const parent = el.parentNode;
const bullet = parent.tagName === 'OL' ? `1. ` : '* ';
const bullet = (parent as HTMLElement).tagName === 'OL' ? `1. ` : '* ';
const nestingIdentLevel = Math.max(0, ctx.listNestingLevel - 1);
el.textContent = `${' '.repeat(nestingIdentLevel * 4)}${bullet}${el.textContent}${ctx.elementIsLast ? '' : '\n'}`;
return el;
},
INPUT(el) {
INPUT(el: HTMLInputElement) {
return el.checked ? '[x] ' : '[ ] ';
},
CODE(el) {
CODE(el: HTMLElement) {
const text = el.textContent;
if (el.parentNode && el.parentNode.tagName === 'PRE') {
if (el.parentNode && (el.parentNode as HTMLElement).tagName === 'PRE') {
el.textContent = `\`\`\`\n${text}\n\`\`\`\n`;
return el;
}
@ -86,7 +82,7 @@ function prepareProcessors(ctx:ProcessorContext): Processors {
return processors;
}
function processElement(ctx :ProcessorContext, processors: Processors, el: HTMLElement) {
function processElement(ctx :ProcessorContext, processors: Processors, el: HTMLElement): string | void {
if (el.hasAttribute('data-markdown-generated-content')) return el.textContent;
if (el.tagName === 'A' && el.children.length === 1 && el.children[0].tagName === 'IMG') {
return processElement(ctx, processors, el.children[0] as HTMLElement);