mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-11-04 01:34:27 +00:00 
			
		
		
		
	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:
		@@ -1,11 +1,11 @@
 | 
			
		||||
import {svg} from '../svg.ts';
 | 
			
		||||
 | 
			
		||||
const addPrefix = (str) => `user-content-${str}`;
 | 
			
		||||
const removePrefix = (str) => str.replace(/^user-content-/, '');
 | 
			
		||||
const hasPrefix = (str) => str.startsWith('user-content-');
 | 
			
		||||
const addPrefix = (str: string): string => `user-content-${str}`;
 | 
			
		||||
const removePrefix = (str: string): string => str.replace(/^user-content-/, '');
 | 
			
		||||
const hasPrefix = (str: string): boolean => str.startsWith('user-content-');
 | 
			
		||||
 | 
			
		||||
// scroll to anchor while respecting the `user-content` prefix that exists on the target
 | 
			
		||||
function scrollToAnchor(encodedId) {
 | 
			
		||||
function scrollToAnchor(encodedId: string): void {
 | 
			
		||||
  if (!encodedId) return;
 | 
			
		||||
  const id = decodeURIComponent(encodedId);
 | 
			
		||||
  const prefixedId = addPrefix(id);
 | 
			
		||||
@@ -24,7 +24,7 @@ function scrollToAnchor(encodedId) {
 | 
			
		||||
  el?.scrollIntoView();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function initMarkupAnchors() {
 | 
			
		||||
export function initMarkupAnchors(): void {
 | 
			
		||||
  const markupEls = document.querySelectorAll('.markup');
 | 
			
		||||
  if (!markupEls.length) return;
 | 
			
		||||
 | 
			
		||||
@@ -39,7 +39,7 @@ export function initMarkupAnchors() {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // remove `user-content-` prefix from links so they don't show in url bar when clicked
 | 
			
		||||
    for (const a of markupEl.querySelectorAll('a[href^="#"]')) {
 | 
			
		||||
    for (const a of markupEl.querySelectorAll<HTMLAnchorElement>('a[href^="#"]')) {
 | 
			
		||||
      const href = a.getAttribute('href');
 | 
			
		||||
      if (!href.startsWith('#user-content-')) continue;
 | 
			
		||||
      a.setAttribute('href', `#${removePrefix(href.substring(1))}`);
 | 
			
		||||
@@ -47,15 +47,15 @@ export function initMarkupAnchors() {
 | 
			
		||||
 | 
			
		||||
    // add `user-content-` prefix to user-generated `a[name]` link targets
 | 
			
		||||
    // TODO: this prefix should be added in backend instead
 | 
			
		||||
    for (const a of markupEl.querySelectorAll('a[name]')) {
 | 
			
		||||
    for (const a of markupEl.querySelectorAll<HTMLAnchorElement>('a[name]')) {
 | 
			
		||||
      const name = a.getAttribute('name');
 | 
			
		||||
      if (!name) continue;
 | 
			
		||||
      a.setAttribute('name', addPrefix(a.name));
 | 
			
		||||
      a.setAttribute('name', addPrefix(name));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (const a of markupEl.querySelectorAll('a[href^="#"]')) {
 | 
			
		||||
    for (const a of markupEl.querySelectorAll<HTMLAnchorElement>('a[href^="#"]')) {
 | 
			
		||||
      a.addEventListener('click', (e) => {
 | 
			
		||||
        scrollToAnchor(e.currentTarget.getAttribute('href')?.substring(1));
 | 
			
		||||
        scrollToAnchor((e.currentTarget as HTMLAnchorElement).getAttribute('href')?.substring(1));
 | 
			
		||||
      });
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,13 +1,13 @@
 | 
			
		||||
import {svg} from '../svg.ts';
 | 
			
		||||
 | 
			
		||||
export function makeCodeCopyButton() {
 | 
			
		||||
export function makeCodeCopyButton(): HTMLButtonElement {
 | 
			
		||||
  const button = document.createElement('button');
 | 
			
		||||
  button.classList.add('code-copy', 'ui', 'button');
 | 
			
		||||
  button.innerHTML = svg('octicon-copy');
 | 
			
		||||
  return button;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function renderCodeCopy() {
 | 
			
		||||
export function renderCodeCopy(): void {
 | 
			
		||||
  const els = document.querySelectorAll('.markup .code-block code');
 | 
			
		||||
  if (!els.length) return;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1,8 @@
 | 
			
		||||
export function displayError(el, err) {
 | 
			
		||||
export function displayError(el: Element, err: Error): void {
 | 
			
		||||
  el.classList.remove('is-loading');
 | 
			
		||||
  const errorNode = document.createElement('pre');
 | 
			
		||||
  errorNode.setAttribute('class', 'ui message error markup-block-error');
 | 
			
		||||
  errorNode.textContent = err.str || err.message || String(err);
 | 
			
		||||
  errorNode.textContent = err.message || String(err);
 | 
			
		||||
  el.before(errorNode);
 | 
			
		||||
  el.setAttribute('data-render-done', 'true');
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -5,7 +5,7 @@ import {renderAsciicast} from './asciicast.ts';
 | 
			
		||||
import {initMarkupTasklist} from './tasklist.ts';
 | 
			
		||||
 | 
			
		||||
// code that runs for all markup content
 | 
			
		||||
export function initMarkupContent() {
 | 
			
		||||
export function initMarkupContent(): void {
 | 
			
		||||
  renderMermaid();
 | 
			
		||||
  renderMath();
 | 
			
		||||
  renderCodeCopy();
 | 
			
		||||
@@ -13,6 +13,6 @@ export function initMarkupContent() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// code that only runs for comments
 | 
			
		||||
export function initCommentContent() {
 | 
			
		||||
export function initCommentContent(): void {
 | 
			
		||||
  initMarkupTasklist();
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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 ``;
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
    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);
 | 
			
		||||
 
 | 
			
		||||
@@ -1,12 +1,12 @@
 | 
			
		||||
import {displayError} from './common.ts';
 | 
			
		||||
 | 
			
		||||
function targetElement(el) {
 | 
			
		||||
function targetElement(el: Element) {
 | 
			
		||||
  // The target element is either the current element if it has the
 | 
			
		||||
  // `is-loading` class or the pre that contains it
 | 
			
		||||
  return el.classList.contains('is-loading') ? el : el.closest('pre');
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export async function renderMath() {
 | 
			
		||||
export async function renderMath(): void {
 | 
			
		||||
  const els = document.querySelectorAll('.markup code.language-math');
 | 
			
		||||
  if (!els.length) return;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -10,7 +10,7 @@ body {margin: 0; padding: 0; overflow: hidden}
 | 
			
		||||
#mermaid {display: block; margin: 0 auto}
 | 
			
		||||
blockquote, dd, dl, figure, h1, h2, h3, h4, h5, h6, hr, p, pre {margin: 0}`;
 | 
			
		||||
 | 
			
		||||
export async function renderMermaid() {
 | 
			
		||||
export async function renderMermaid(): Promise<void> {
 | 
			
		||||
  const els = document.querySelectorAll('.markup code.language-mermaid');
 | 
			
		||||
  if (!els.length) return;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
import {POST} from '../modules/fetch.ts';
 | 
			
		||||
import {showErrorToast} from '../modules/toast.ts';
 | 
			
		||||
 | 
			
		||||
const preventListener = (e) => e.preventDefault();
 | 
			
		||||
const preventListener = (e: Event) => e.preventDefault();
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Attaches `input` handlers to markdown rendered tasklist checkboxes in comments.
 | 
			
		||||
@@ -10,10 +10,10 @@ const preventListener = (e) => e.preventDefault();
 | 
			
		||||
 * is set accordingly and sent to the server. On success it updates the raw-content on
 | 
			
		||||
 * error it resets the checkbox to its original value.
 | 
			
		||||
 */
 | 
			
		||||
export function initMarkupTasklist() {
 | 
			
		||||
export function initMarkupTasklist(): void {
 | 
			
		||||
  for (const el of document.querySelectorAll(`.markup[data-can-edit=true]`) || []) {
 | 
			
		||||
    const container = el.parentNode;
 | 
			
		||||
    const checkboxes = el.querySelectorAll(`.task-list-item input[type=checkbox]`);
 | 
			
		||||
    const checkboxes = el.querySelectorAll<HTMLInputElement>(`.task-list-item input[type=checkbox]`);
 | 
			
		||||
 | 
			
		||||
    for (const checkbox of checkboxes) {
 | 
			
		||||
      if (checkbox.hasAttribute('data-editable')) {
 | 
			
		||||
@@ -52,7 +52,7 @@ export function initMarkupTasklist() {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
          const editContentZone = container.querySelector('.edit-content-zone');
 | 
			
		||||
          const editContentZone = container.querySelector<HTMLDivElement>('.edit-content-zone');
 | 
			
		||||
          const updateUrl = editContentZone.getAttribute('data-update-url');
 | 
			
		||||
          const context = editContentZone.getAttribute('data-context');
 | 
			
		||||
          const contentVersion = editContentZone.getAttribute('data-content-version');
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user