Преглед на файлове

feat: add default language option

Timothy Lin преди 2 години
родител
ревизия
3a8ba258b4
променени са 2 файла, в които са добавени 27 реда и са изтрити 7 реда
  1. 16 7
      src/generator.js
  2. 11 0
      test.js

+ 16 - 7
src/generator.js

@@ -7,6 +7,9 @@
  *   Set `showLineNumbers` to `true` to always display line number
  * @property {boolean} [ignoreMissing]
  *   Set `ignoreMissing` to `true` to ignore unsupported languages and line highlighting when no language is specified
+ * @property {string} [defaultLanguage]
+ *   Uses the specified language as the default if none is specified. Takes precedence over `ignoreMissing`.
+ *   Note: The language must be registered with refractor.
  */
 
 import { visit } from 'unist-util-visit'
@@ -188,7 +191,7 @@ const rehypePrismGenerator = (refractor) => {
         node.properties.className = []
       }
       node.properties.className.push('code-highlight')
-      const lang = getLanguage(node)
+      const lang = getLanguage(node) || options.defaultLanguage
 
       /** @type {Element} */
       let refractorRoot
@@ -197,10 +200,10 @@ const rehypePrismGenerator = (refractor) => {
       if (lang) {
         try {
           let rootLang
-          if (lang?.includes('diff-')){
-            rootLang=lang.split('-')[1]
-          } else{
-            rootLang=lang
+          if (lang?.includes('diff-')) {
+            rootLang = lang.split('-')[1]
+          } else {
+            rootLang = lang
           }
           // @ts-ignore
           refractorRoot = refractor.highlight(toString(node), rootLang)
@@ -273,9 +276,15 @@ const rehypePrismGenerator = (refractor) => {
         }
 
         // Diff classes
-        if ((lang === 'diff' || lang?.includes('diff-')) && toString(line).substring(0, 1) === '-') {
+        if (
+          (lang === 'diff' || lang?.includes('diff-')) &&
+          toString(line).substring(0, 1) === '-'
+        ) {
           line.properties.className.push('deleted')
-        } else if ((lang === 'diff' || lang?.includes('diff-')) && toString(line).substring(0, 1) === '+') {
+        } else if (
+          (lang === 'diff' || lang?.includes('diff-')) &&
+          toString(line).substring(0, 1) === '+'
+        ) {
           line.properties.className.push('inserted')
         }
       }

+ 11 - 0
test.js

@@ -346,6 +346,17 @@ test('with options.ignoreMissing, does nothing to code block with fake language-
   assert.is(result, expected)
 })
 
+test('with options.defaultLanguage, it adds the correct language class tag', () => {
+  const result = processHtml(
+    dedent`
+    <pre><code>x = 6</code></pre>
+  `,
+    { defaultLanguage: 'py' }
+  )
+  const expected = dedent`<pre class="language-py"><code class="code-highlight"><span class="code-line">x <span class="token operator">=</span> <span class="token number">6</span></span></code></pre>`
+  assert.is(result, expected)
+})
+
 test('should work with multiline code / comments', () => {
   const result = processHtml(
     dedent`