13

不确定是否有人遇到过这种情况。我正在使用 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 这是结果: 在此处输入图像描述

4

4 回答 4

5

尝试使用以下命令更新 CSS 文件:

white-space: pre-wrap

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

于 2018-06-04T15:55:58.143 回答
5

手动初始化元素时我遇到了类似的问题。我偶然发现了这个讨论,它有一个对我有用的修复: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

于 2020-06-26T11:41:06.390 回答
1

如果这对其他人有帮助,我有一个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 的回答和他联系的问题帮助了我。

于 2020-08-28T18:36:32.060 回答
0

1.激活 normalize whitespace 插件

我建议您激活 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.插入换行标签<br>,随意换行

既然您break-line在某个最大字符数之后设置了该属性,您可能希望随意中断一些行以获得更简洁的代码。为此,您需要<br>在想要换行的位置插入标签。

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

如果您使用解析器将动态生成的 html 代码解析为字符串(例如来自数据库)并且 prims 不解析您的<br>标签,则必须before-sanity-check像这样使用 prism 钩子:

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

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

于 2021-05-05T06:42:08.500 回答