Просмотр исходного кода

fix: allow range with whitespace in between

Timothy Lin 4 лет назад
Родитель
Сommit
bc09c5bd50
3 измененных файлов с 28 добавлено и 3 удалено
  1. 7 2
      index.js
  2. 1 1
      package.json
  3. 20 0
      test.js

+ 7 - 2
index.js

@@ -32,8 +32,13 @@ const getLanguage = (node) => {
  */
 const calculateLinesToHighlight = (meta) => {
   const RE = /{([\d,-]+)}/
-  if (RE.test(meta)) {
-    const strlineNumbers = RE.exec(meta)[1]
+  // Remove space between {} e.g. {1, 3}
+  const parsedMeta = meta
+    .split(',')
+    .map((str) => str.trim())
+    .join()
+  if (RE.test(parsedMeta)) {
+    const strlineNumbers = RE.exec(parsedMeta)[1]
     const lineNumbers = rangeParser(strlineNumbers)
     return (index) => lineNumbers.includes(index + 1)
   } else {

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "rehype-prism-plus",
-  "version": "0.0.2",
+  "version": "0.0.3",
   "description": "rehype plugin to highlight code blocks in HTML with Prism (via refractor) with line highlighting and line numbers",
   "source": "index.js",
   "files": [

+ 20 - 0
test.js

@@ -147,6 +147,26 @@ test('should highlight comma separated lines', async () => {
   assert.is(codeHighlightCount, 2)
 })
 
+test('should should parse ranges with a space in between', async () => {
+  const meta = '{1, 3}'
+  const result = processHtml(
+    dedent`
+    <div>
+      <pre>
+      <code class="language-py">x = 6
+      y = 7 
+      z = 10
+      </code>
+      </pre>
+    </div>
+    `,
+    {},
+    meta
+  ).trim()
+  const codeHighlightCount = (result.match(/<div class="code-line highlight-line">/g) || []).length
+  assert.is(codeHighlightCount, 2)
+})
+
 test('should highlight range separated lines', async () => {
   const meta = '{1-3}'
   const result = processHtml(