Răsfoiți Sursa

Merge pull request #59 from timlrx/feat/default-language

Feature - Add default language option
Timothy 2 ani în urmă
părinte
comite
5d5e0ef1a5
6 a modificat fișierele cu 8676 adăugiri și 27 ștergeri
  1. 1 1
      .github/workflows/unit.yml
  2. 4 0
      .husky/pre-commit
  3. 8611 2
      package-lock.json
  4. 8 13
      package.json
  5. 28 11
      src/generator.js
  6. 24 0
      test.js

+ 1 - 1
.github/workflows/unit.yml

@@ -12,7 +12,7 @@ jobs:
       - uses: actions/checkout@v2
       - uses: actions/setup-node@v2
         with:
-          node-version: '14.x'
+          node-version: '16.x'
       - name: Install dependencies
         run: npm ci
       - name: Run unit tests

+ 4 - 0
.husky/pre-commit

@@ -0,0 +1,4 @@
+#!/usr/bin/env sh
+. "$(dirname "$0")/_/husky.sh"
+
+npx lint-staged

Fișier diff suprimat deoarece este prea mare
+ 8611 - 2
package-lock.json


+ 8 - 13
package.json

@@ -1,6 +1,6 @@
 {
   "name": "rehype-prism-plus",
-  "version": "1.5.0",
+  "version": "1.5.1",
   "description": "rehype plugin to highlight code blocks in HTML with Prism (via refractor) with line highlighting and line numbers",
   "source": "index.js",
   "files": [
@@ -60,24 +60,24 @@
   "dependencies": {
     "hast-util-to-string": "^2.0.0",
     "parse-numeric-range": "^1.3.0",
-    "refractor": "^4.7.0",
+    "refractor": "^4.8.0",
     "rehype-parse": "^8.0.2",
     "unist-util-filter": "^4.0.0",
     "unist-util-visit": "^4.0.0"
   },
   "devDependencies": {
     "dedent": "^0.7.0",
-    "eslint": "^7.32.0",
+    "eslint": "^8.43.0",
     "eslint-config-prettier": "^8.3.0",
     "eslint-plugin-node": "^11.1.0",
-    "husky": "^4.0.0",
+    "husky": "^8.0.0",
     "lint-staged": "^11.1.2",
-    "microbundle": "^0.14.1",
-    "prettier": "^2.3.2",
+    "microbundle": "^0.15.1",
+    "prettier": "^2.8.8",
     "rehype": "^12.0.0",
     "remark": "^14.0.2",
     "remark-rehype": "^10.1.0",
-    "typescript": "4.4.3",
+    "typescript": "5.1.3",
     "unified": "^10.1.0",
     "uvu": "^0.5.1"
   },
@@ -97,10 +97,5 @@
     "*.+(js|jsx|ts|tsx|json|css|md|mdx)": [
       "prettier --write"
     ]
-  },
-  "husky": {
-    "hooks": {
-      "pre-commit": "lint-staged"
-    }
   }
-}
+}

+ 28 - 11
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'
@@ -14,10 +17,6 @@ import { toString } from 'hast-util-to-string'
 import { filter } from 'unist-util-filter'
 import rangeParser from 'parse-numeric-range'
 
-/**
- * @param {Element} node
- * @return {string|null}
- */
 const getLanguage = (node) => {
   const className = node.properties.className
   //@ts-ignore
@@ -29,6 +28,17 @@ const getLanguage = (node) => {
   return null
 }
 
+/**
+ * @param {import('refractor/lib/core').Refractor} refractor
+ * @param {string} defaultLanguage
+ * @return {void}
+ */
+const checkIfLanguageIsRegistered = (refractor, defaultLanguage) => {
+  if (defaultLanguage && !refractor.registered(defaultLanguage)) {
+    throw new Error(`The default language "${defaultLanguage}" is not registered with refractor.`)
+  }
+}
+
 /**
  * Create a closure that determines if we have to highlight the given index
  *
@@ -162,6 +172,7 @@ const addNodePositionClosure = () => {
  */
 const rehypePrismGenerator = (refractor) => {
   return (options = {}) => {
+    checkIfLanguageIsRegistered(refractor, options.defaultLanguage)
     return (tree) => {
       visit(tree, 'element', visitor)
     }
@@ -188,7 +199,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 +208,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 +284,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')
         }
       }

+ 24 - 0
test.js

@@ -346,6 +346,30 @@ 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('throws error if options.defaultLanguage is not registered with refractor', () => {
+  assert.throws(
+    () =>
+      processHtml(
+        dedent`
+    <pre><code>x = 6</code></pre>
+  `,
+        { defaultLanguage: 'pyzqt' }
+      ),
+    /"pyzqt" is not registered with refractor/
+  )
+})
+
 test('should work with multiline code / comments', () => {
   const result = processHtml(
     dedent`

Unele fișiere nu au fost afișate deoarece prea multe fișiere au fost modificate în acest diff