How to apply styling and html tags on hover message with vscode API?

Viewed 1643

I am trying to style or format the hover message with MarkdownString but it always resulted in empty space or escaped everything but I found out that you can use span to style it but you can only apply color and background-color with this PR.

Right now, it's ugly as plain text can be, and even using table markdown is not working. Is there a way to improve this?

Plain text hover message

I'm writing it as follow:

const markdown = new MarkdownString(`<p> Some label: <code>${value}</code></p>`);
markdown.isTrusted = true;

return new Hover(markdown, range);
1 Answers

With v1.61 you will have more markdownString options. There will be suport for these html tags:

allowedTags: ['ul', 'li', 'p', 'code', 'blockquote', 'ol', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'em', 'pre', 'table', 'thead', 'tbody', 'tr', 'th', 'td', 'div', 'del', 'a', 'strong', 'br', 'img', 'span'],

So with b, h1, etc. you can get a bit of extra "styling".

/**
  * Indicates that this markdown string can contain raw html tags. Default to false.
  *
  * When `supportHtml` is false, the markdown renderer will strip out any raw html tags
  * that appear in the markdown text. This means you can only use markdown syntax for rendering.
  *
  * When `supportHtml` is true, the markdown render will also allow a safe subset of html tags
  * and attributes to be rendered. See https://github.com/microsoft/vscode/blob/6d2920473c6f13759c978dd89104c4270a83422d/src/vs/base/browser/markdownRenderer.ts#L296
  * for a list of all supported tags and attributes.
*/
  supportHtml?: boolean;

Some sample code:

const value = "Jello";

const content = new vscode.MarkdownString(`<span style="color:#000;background-color:#fff;">Howdy there.</span>`);
content.appendMarkdown(`<p><b> Some label: <code>${value}</code></b></p>`)
content.supportHtml = true;

content.isTrusted = true;

return new vscode.Hover(content, new vscode.Range(position, position));

In my testing you can still only use

<span style="color:#000;background-color:#fff;">

with no other styles but color and background-color.


Here are a few options that work for me to do styling, markdown tables, code blocks, etc.:

vscode.languages.registerHoverProvider('javascript', {
    provideHover(document, position, token) {
        
        // a markdown table, wrapping in a styled span did not work
        // had to style each "cell" separately
        // html entity &nbsp; works

        const markdown = new vscode.MarkdownString(`
|    <span style="color:#ff0;background-color:#000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Table&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>|    Header     |
|    :----:    |    :----:     |
|first cell    |second cell  |
|third cell    |<span style="color:#f00;background-color:#fff;">&nbsp;&nbsp;fourth cell&nbsp;&nbsp;</span>  |
        \n\n\n`);  // the newline is necessary for any following appends to work correctly, multiple newlines are reduced to one
        
        const styledString = `<span style="color:#fff;background-color:#666;">&nbsp;&nbsp;&nbsp;NASA code follows:&nbsp;&nbsp;&nbsp;</span>`;

        const codeBlock = `const a = 12;
if (a) return;`;    // any preceding tabs will be rendered in a template literal, so flush left

        // const codeBlock2 = `const c = 12;\nif (c) return;`;  // works, alternate form with newline

        markdown.appendText("______________________________\n");  // a fake separator
        markdown.appendMarkdown(styledString);
        markdown.appendCodeblock(codeBlock, "javascript");
        markdown.appendMarkdown(
`**Bold Text**
* some note
* another note
* final note`
        );

        markdown.isTrusted = true;

        return new vscode.Hover(markdown, new vscode.Range(position, position));
    }

hover styling demo

  • Note: the markdown table doesn't get any cell or table dividers for some reason.
Related