【问题标题】:PrismJS no line breaksPrismJS 没有换行符
【发布时间】:2018-06-04 14:44:43
【问题描述】:

不确定是否有人遇到过这种情况。我正在使用 PrismJS 语法荧光笔来突出显示代码。应用程序是用 Reactjs 编写的,我想做的是在 WYSIWYG 编辑器中,当用户想要插入代码块时,我用 pre + 代码包装用户选择的文本。 PrismJS 似乎像您期望的那样正确地标记了元素:

但正如您从上图中可能看到的那样,所有内容都放在一行中。而不是漂亮的代码块:

我不确定出了什么问题,使用 prismjs 网站的 css:

code[class*="language-"],
pre[class*="language-"] {
    color: black;
    background: none;
    text-shadow: 0 1px white;
    font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
    text-align: left;
    white-space: pre;
    word-spacing: normal;
    word-break: normal;
    word-wrap: normal;
    line-height: 1.5;

    -moz-tab-size: 4;
    -o-tab-size: 4;
    tab-size: 4;

    -webkit-hyphens: none;
    -moz-hyphens: none;
    -ms-hyphens: none;
    hyphens: none;
}

pre[class*="language-"]::-moz-selection,
pre[class*="language-"] ::-moz-selection,
code[class*="language-"]::-moz-selection,
code[class*="language-"] ::-moz-selection {
    text-shadow: none;
    background: #b3d4fc;
}

pre[class*="language-"]::selection,
pre[class*="language-"] ::selection,
code[class*="language-"]::selection,
code[class*="language-"] ::selection {
    text-shadow: none;
    background: #b3d4fc;
}

@media print {
    code[class*="language-"],
    pre[class*="language-"] {
        text-shadow: none;
    }
}

/* Code blocks */
pre[class*="language-"] {
    padding: 1em;
    margin: .5em 0;
    overflow: auto;
}

:not(pre) > code[class*="language-"],
pre[class*="language-"] {
    background: #f5f2f0;
}

/* Inline code */
:not(pre) > code[class*="language-"] {
    padding: .1em;
    border-radius: .3em;
    white-space: normal;
}

.token.comment,
.token.prolog,
.token.doctype,
.token.cdata {
    color: slategray;
}

.token.punctuation {
    color: #999;
}

.namespace {
    opacity: .7;
}

.token.property,
.token.tag,
.token.boolean,
.token.number,
.token.constant,
.token.symbol,
.token.deleted {
    color: #905;
}

.token.selector,
.token.attr-name,
.token.string,
.token.char,
.token.builtin,
.token.inserted {
    color: #690;
}

.token.operator,
.token.entity,
.token.url,
.language-css .token.string,
.style .token.string {
    color: #9a6e3a;
    background: hsla(0, 0%, 100%, .5);
}

.token.atrule,
.token.attr-value,
.token.keyword {
    color: #07a;
}

.token.function,
.token.class-name {
    color: #dd4a68;
}

.token.regex,
.token.important,
.token.variable {
    color: #e90;
}

.token.important,
.token.bold {
    font-weight: bold;
}
.token.italic {
    font-style: italic;
}

.token.entity {
    cursor: help;
}

这里是输出的html:

编辑:

如果添加 word-wrap: pre-wrap 这是结果:

【问题讨论】:

  • 我也遇到了同样的问题...你找到解决方案了吗?

标签: javascript reactjs prismjs


【解决方案1】:

尝试更新 CSS 文件:

white-space: pre-wrap

https://github.com/PrismJS/prism/issues/1237

【讨论】:

  • 我之前在看这个问题,但它没有解决问题:/ 查看编辑的问题
【解决方案2】:

我在手动初始化元素时遇到了类似的问题。我偶然发现了这个讨论,它有一个对我有用的修复:https://github.com/PrismJS/prism/issues/1764

HTML - 使用标志数据手册加载脚本:

<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.20.0/prism.min.js" data-manual></script>

JS - 添加以下钩子:

Prism.hooks.add("before-highlight", function (env) {
    env.code = env.element.innerText;
});
Prism.highlightElement(code);

工作示例: https://codepen.io/Ukmasmu/pen/xxZLwxG?editors=1010

【讨论】:

    【解决方案3】:

    如果这对其他人有帮助,我有一个 textarea,它会在您键入时更新代码块,这对我有用:

    <textarea onkeyup="this.onchange();" onchange="document.getElementById('query-highlighted').textContent = this.value; Prism.highlightAll();"></textarea>
    <pre><code class="language-sql" id="query-highlighted"></code></pre>
    

    也就是说,我使用.textContent = 而不是.innerText =(后者没有按预期保留换行符)。

    Sever van Snugg's answer 和他关联的 issue 帮助了我。

    【讨论】:

      【解决方案4】:

      1。激活规范化空白插件

      我建议你激活 normalize whitespace 插件并设置 break-lines 属性而不是像这样操作 prism.css 文件来使用 white-space: pre-wrap

      Prism.plugins.NormalizeWhitespace.setDefaults({
                  'remove-trailing': true,
                  'remove-indent': true,
                  'left-trim': true,
                  'right-trim': true,
                  'break-lines': 60, //max number of characters in each line before break
      });
      

      我在我的博客中使用了上述方法,它就像一个魅力。当然你可以根据自己的喜好调整break-lines的值。

      2。插入换行标签&lt;br&gt;,随意换行

      既然您在某个最大字符数之后设置了break-line 属性,您可能希望随意换行以使代码更简洁。为此,您需要在要设置换行符的位置插入 &lt;br&gt; 标记。

      注意:如果您使用 html 解析器通过 prism 解析动态内容

      如果您使用解析器将动态生成的 html 代码解析为字符串(例如来自数据库)并且 prims 没有解析您的 &lt;br&gt; 标签,您将不得不像这样使用 before-sanity-check prism hook :

      Prism.hooks.add('before-sanity-check', function (env) {
        env.element.innerHTML = env.element.innerHTML.replace(/<br>/g, '\n');
        env.code = env.element.textContent;
      });

      在突出显示之前,上面的代码所做的是将&lt;br&gt; 标记替换为\n,因为prism 无法将&lt;br&gt; 解析为换行符。

      【讨论】:

        【解决方案5】:

        与 Sever van Snugg 的回答类似,我使用以下解决方案,其中 forEach 循环根据所使用的 Prism CSS 样式表的样式规则突出显示所有代码节点(因为我在单个页面上有多个代码标签)。我在 HTML 正文的底部找到了这些脚本:

        <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.27.0/prism.min.js" data-manual></script>
        
        <script>
            Prism.hooks.add("before-highlight", function (env) {
                env.code = env.element.innerText;
            });
        
            code = document.getElementsByTagName('code');
            Array.from(code).forEach(el => { Prism.highlightElement(el) });
        </script>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-18
          • 2011-06-14
          • 2011-09-22
          相关资源
          最近更新 更多