diff --git a/static/ansi-to-html.ts b/static/ansi-to-html.ts index dfad84cbc..5684c99c3 100644 --- a/static/ansi-to-html.ts +++ b/static/ansi-to-html.ts @@ -30,7 +30,7 @@ import _ from 'underscore'; import {AnsiToHtmlOptions, ColorCodes} from './ansi-to-html.interfaces.js'; import {assert, unwrap} from './assert.js'; -import {isString} from '../shared/common-utils.js'; +import {isString, escapeHTML} from '../shared/common-utils.js'; const defaults: AnsiToHtmlOptions = { fg: '#FFF', @@ -132,7 +132,11 @@ function generateOutput(stack: string[], token: string, data: string | number, o } else if (token === 'rgb') { assert(isString(data), "Param 'data' must be a string at this point"); return handleRgb(stack, data, options); + } else if (token === 'url') { + assert(isString(data), "Param 'data' must be a string at this point"); + return handleUrl(stack, data, options); } + return ''; } @@ -197,6 +201,11 @@ function handleDisplay(stack: string[], code: string | number, options: AnsiToHt return 'Unknown code'; } +function handleUrl(stack: string[], data: string, options: AnsiToHtmlOptions): string { + const [url, text] = data.split(/\x1b\\|\x07/); + return `${escapeHTML(text)}`; +} + /** * Clear all the styles */ @@ -317,7 +326,7 @@ interface Token { function tokenize(text: string, options: AnsiToHtmlOptions, callback: TokenizeCallback) { let ansiMatch = false; - const ansiHandler = 3; + const ansiHandler = 4; function remove(): string { return ''; @@ -364,8 +373,17 @@ function tokenize(text: string, options: AnsiToHtmlOptions, callback: TokenizeCa return ''; } + function hyperlink(_m: string, captureGroup: string): string { + callback('url', captureGroup); + return ''; + } + /* eslint no-control-regex:0 */ const tokens: Token[] = [ + { + pattern: /^\x1b]8;;(.*?(\x1b\\|\x07).*?)\x1b]8;;\2/, + sub: hyperlink, + }, { pattern: /^\x08+/, sub: remove, @@ -456,7 +474,7 @@ function updateStickyStack( token: string, data: string | number, ): StickyStackElement[] { - if (token !== 'text') { + if (token !== 'text' && token !== 'url') { stickyStack = stickyStack.filter(notCategory(categoryForCode(data))); stickyStack.push({ token: token, diff --git a/static/panes/output.ts b/static/panes/output.ts index 053b2fd0f..6b67445f7 100644 --- a/static/panes/output.ts +++ b/static/panes/output.ts @@ -285,6 +285,9 @@ export class Output extends Pane { e.preventDefault(); return false; }) + .on('click', '.diagnostic-url', e => { + e.stopPropagation(); + }) .on('mouseover', () => { this.emitEditorLinkLine(lineNum, column, filename, false); }) diff --git a/test/ansi-to-html-tests.ts b/test/ansi-to-html-tests.ts index b7fa6af89..6ae0132ac 100644 --- a/test/ansi-to-html-tests.ts +++ b/test/ansi-to-html-tests.ts @@ -90,4 +90,32 @@ describe('ansi-to-html', () => { 'foobar', ); }); + + // hyperlinks + it('should parse terminal hyperlinks', () => { + const filter = new Filter(filterOpts); + expect( + filter.toHtml( + 'error[\x1B]8;;https://doc.rust-lang.org/error_codes/E0425.html\x07E0425\x1B]8;;\x07]: cannot find value `x` in this scope', + ), + ).toEqual( + 'error[E0425]: cannot find value `x` in this scope', + ); + expect( + filter.toHtml( + 't.c:3:1: warning: control reaches end of non-void function [\x1B]8;;https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/Warning-Options.html#index-Wno-return-type\x1B\\-Wreturn-type\x1B]8;;\x1B\\]', + ), + ).toEqual( + 't.c:3:1: warning: control reaches end of non-void function [-Wreturn-type]', + ); + }); + + it('should properly escape hyperlinks', () => { + const filter = new Filter(filterOpts); + expect( + filter.toHtml('\x1B]8;;https://example.org/boldlink\x07italic\x1B]8;;\x07'), + ).toEqual( + '<i>italic</i>', + ); + }); });