【问题标题】:How to apply styling and html tags on hover message with vscode API?如何使用 vscode API 在悬停消息上应用样式和 html 标签?
【发布时间】:2021-05-29 09:23:07
【问题描述】:

我正在尝试使用MarkdownString 设置悬停消息的样式或格式,但它总是导致空白或转义所有内容,但我发现您可以使用span 设置样式但您只能应用colorbackground-color 和这个PR

现在,它像纯文本一样丑陋,甚至使用 table markdown 也不起作用。有什么办法可以改善吗?

我是这样写的:

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

return new Hover(markdown, range);

【问题讨论】:

    标签: visual-studio-code vscode-extensions


    【解决方案1】:

    使用 v1.61,您将拥有更多 markdownString 选项。将支持这些 html 标签:

    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'],

    因此,使用bh1 等,您可以获得一些额外的“样式”。

    /**
      * 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;
    

    一些示例代码:

    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));
    

    在我的测试中你仍然只能使用

    &lt;span style="color:#000;background-color:#fff;"&gt;

    除了colorbackground-color,没有其他样式。


    以下是一些适合我做样式、降价表、代码块等的选项:

    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));
        }
    

    • 注意:由于某种原因,markdown 表格没有任何单元格或表格分隔符。

    【讨论】:

    • 不幸的是,这可能是我们所能得到的样式。这次真是万分感谢。非常感谢。
    • 我忘了提,我也试过&lt;p&gt;&lt;code&gt; 元素。他们没有工作。
    • 是的,我包含的 PR 只允许带颜色的跨度
    • 我也有同样的问题。需要在悬停时格式化一些文本,但不能。 v1.61 什么时候发布? npm 仍将其列为 v1.60
    • @EdwinSamuelJonathan v1.61 的预计发布日期是 2021 年 10 月 6 日。
    猜你喜欢
    • 2012-09-21
    • 2019-04-05
    • 1970-01-01
    • 1970-01-01
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    相关资源
    最近更新 更多