Sfoglia il codice sorgente

website: fix highlighting again (#1757)

* website: fix unhighlighted code block detection

* website: fix html inside inline code tags
Renée Kooi 5 anni fa
parent
commit
95a13ff52f
1 ha cambiato i file con 13 aggiunte e 3 eliminazioni
  1. 13 3
      website/scripts/highlight.js

+ 13 - 3
website/scripts/highlight.js

@@ -13,7 +13,15 @@ global.Prism = Prism
 require('prismjs/components/')()
 delete global.Prism
 
-const unhighlightedCodeRx = /<pre><code class="(.*)?">([\s\S]*?)<\/code><\/pre>/igm
+const rawInlineCodeRx = /<code>([^\n]+?)<\/code>/ig
+const unhighlightedCodeRx = /<pre><code class="([^"]*)?">([\s\S]*?)<\/code><\/pre>/igm
+
+function escapeInlineCodeBlocks (data) {
+  data.content = data.content.replace(rawInlineCodeRx, (_, code) => {
+    return `<code>${entities.encode(code)}</code>`
+  })
+  return data
+}
 
 function highlight (lang, code) {
   const startTag = `<figure class="highlight ${lang}"><table><tr><td class="code"><pre>`
@@ -44,14 +52,15 @@ function code (args, content) {
   return highlight(lang, content)
 }
 
-function includeCode (args) {
+async function includeCode (args) {
   let lang = ''
   if (args[0].startsWith('lang:')) {
     lang = args.shift().replace(/^lang:/, '')
   }
 
   const file = path.join(hexo.source_dir, hexo.config.code_dir, args.join(' '))
-  return readFile(file, 'utf8').then((code) => highlight(lang, code.trim()))
+  const content = await readFile(file, 'utf8')
+  return highlight(lang, content.trim())
 }
 
 // Highlight as many things as we possibly can
@@ -60,6 +69,7 @@ hexo.extend.tag.register('codeblock', code, true)
 hexo.extend.tag.register('include_code', includeCode, { async: true })
 hexo.extend.tag.register('include-code', includeCode, { async: true })
 
+hexo.extend.filter.register('after_post_render', escapeInlineCodeBlocks)
 // Hexo includes its own code block handling by default which may
 // cause the above to miss some things, so do another pass when the page
 // is done rendering to pick up any code blocks we may have missed.