当我在 html 中手动输入代码时,highlight.js 可以完美运行
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="asciidoc.css">
<link rel="stylesheet" href="pygments.css">
<link rel="stylesheet" href="highlightjs-theme.css">
</head>
<body>
<pre><code>
using System;
static void Main(string[] args)
{
// Display the number of command line arguments.
Console.WriteLine(args.Length);
}
</code></pre>
<script type="text/javascript" src="highlight.min.js"></script>
<script type="text/javascript">hljs.initHighlightingOnLoad();</script>
</body>
</html>
但是当我使用 asciidoctor.js 将标记转换为 html 荧光笔不适用于生成的 html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="asciidoc.css">
<link rel="stylesheet" href="pygments.css">
<link rel="stylesheet" href="highlightjs-theme.css">
</head>
<body>
<!-- static c# code-->
<pre><code class="csharp">
using System;
static void Main(string[] args)
{
// Display the number of command line arguments.
Console.WriteLine(args.Length);
}
</code></pre>
<textarea col=20 rows=3 id="raw_code" onkeyup="preview()"></textarea>
<!-- Dynamic c#-->
<div id="target"></div>
<script src="asciidoctor.min.js"></script>
<script type="text/javascript" src="highlight.min.js"></script>
<script type="text/javascript">
hljs.initHighlightingOnLoad();
function preview(){
var content = document.getElementById('raw_code').value
var asciidoctor = Asciidoctor();
const html = asciidoctor.convert(content,{doctype: 'article' ,safe: 'safe',header_footer: false, attributes: { 'source-highlighter': 'highlightjs' }})
document.getElementById('target').innerHTML = html;
}
</script>
</body>
</html>
上面代码后的结果 我们可以看到按键插入的代码没有高亮,但是页面加载时手动插入的代码是高亮的。