mirror of
https://github.com/go-gitea/gitea.git
synced 2026-05-20 19:11:09 +09:00
Linkify URLs in Actions workflow logs (#36986)
Detect URLs in Actions log output and render them as clickable links, similar to how GitHub Actions handles this. Pre-existing links from ansi_up's OSC 8 parsing are also kept intact. --------- Signed-off-by: silverwind <me@silverwind.io> Co-authored-by: Claude (claude-opus-4-6) <noreply@anthropic.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
@@ -17,4 +17,9 @@ test('renderAnsi', () => {
|
||||
// treat "\033[0K" and "\033[0J" (Erase display/line) as "\r", then it will be covered to "\n" finally.
|
||||
expect(renderAnsi('a\x1b[Kb\x1b[2Jc')).toEqual('a\nb\nc');
|
||||
expect(renderAnsi('\x1b[48;5;88ma\x1b[38;208;48;5;159mb\x1b[m')).toEqual(`<span style="background-color:rgb(135,0,0)">a</span><span style="background-color:rgb(175,255,255)">b</span>`);
|
||||
|
||||
// URLs in ANSI output become clickable links
|
||||
const link = (url: string) => `<a href="${url}" target="_blank">${url}</a>`;
|
||||
expect(renderAnsi('Downloading https://github.com/actions/upload-artifact/releases')).toEqual(`Downloading ${link('https://github.com/actions/upload-artifact/releases')}`);
|
||||
expect(renderAnsi('\x1b[32mhttps://proxy.golang.org/cached-only\x1b[0m')).toEqual(`<span class="ansi-green-fg">${link('https://proxy.golang.org/cached-only')}</span>`);
|
||||
});
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import {AnsiUp} from 'ansi_up';
|
||||
import {linkifyURLs} from '../utils/url.ts';
|
||||
|
||||
const replacements: Array<[RegExp, string]> = [
|
||||
[/\x1b\[\d+[A-H]/g, ''], // Move cursor, treat them as no-op
|
||||
@@ -25,21 +26,23 @@ export function renderAnsi(line: string): string {
|
||||
}
|
||||
}
|
||||
|
||||
let result: string;
|
||||
if (!line.includes('\r')) {
|
||||
return ansi_up.ansi_to_html(line);
|
||||
}
|
||||
|
||||
// handle "\rReading...1%\rReading...5%\rReading...100%",
|
||||
// convert it into a multiple-line string: "Reading...1%\nReading...5%\nReading...100%"
|
||||
const lines: Array<string> = [];
|
||||
for (const part of line.split('\r')) {
|
||||
if (part === '') continue;
|
||||
const partHtml = ansi_up.ansi_to_html(part);
|
||||
if (partHtml !== '') {
|
||||
lines.push(partHtml);
|
||||
result = ansi_up.ansi_to_html(line);
|
||||
} else {
|
||||
// handle "\rReading...1%\rReading...5%\rReading...100%",
|
||||
// convert it into a multiple-line string: "Reading...1%\nReading...5%\nReading...100%"
|
||||
const lines: Array<string> = [];
|
||||
for (const part of line.split('\r')) {
|
||||
if (part === '') continue;
|
||||
const partHtml = ansi_up.ansi_to_html(part);
|
||||
if (partHtml !== '') {
|
||||
lines.push(partHtml);
|
||||
}
|
||||
}
|
||||
// the log message element is with "white-space: break-spaces;", so use "\n" to break lines
|
||||
result = lines.join('\n');
|
||||
}
|
||||
|
||||
// the log message element is with "white-space: break-spaces;", so use "\n" to break lines
|
||||
return lines.join('\n');
|
||||
return linkifyURLs(result);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user