Browse Source

feat: add types

Timothy Lin 4 năm trước cách đây
mục cha
commit
80f606a7f6
5 tập tin đã thay đổi với 71 bổ sung4 xóa
  1. 41 1
      index.js
  2. 7 1
      package-lock.json
  3. 5 2
      package.json
  4. 16 0
      tsconfig.json
  5. 2 0
      vite.config.cjs

+ 41 - 1
index.js

@@ -1,8 +1,18 @@
+/**
+ * @typedef {import('unist-util-visit').Node & {properties: Object<any, any>}} Node
+ * @typedef {import('unist-util-visit').Parent & {properties: Object<any, any>}} Parent
+ * @typedef {import('unist-util-visit').Visitor<Node>} Visitor
+ */
+
 import { visit } from 'unist-util-visit'
 import toString from 'hast-util-to-string'
 import { refractor } from 'refractor'
 import rangeParser from 'parse-numeric-range'
 
+/**
+ * @param {Node} node
+ * @return {string|null}
+ */
 const getLanguage = (node) => {
   const className = node.properties.className || []
 
@@ -14,7 +24,12 @@ const getLanguage = (node) => {
   return null
 }
 
-// Create a closure that determines if we have to highlight the given index
+/**
+ * Create a closure that determines if we have to highlight the given index
+ *
+ * @param {string} meta
+ * @return { (index:number) => boolean }
+ */
 const calculateLinesToHighlight = (meta) => {
   const RE = /{([\d,-]+)}/
   if (RE.test(meta)) {
@@ -26,6 +41,12 @@ const calculateLinesToHighlight = (meta) => {
   }
 }
 
+/**
+ * Split line to div node with className `code-line`
+ *
+ * @param {string} text
+ * @return {Node[]}
+ */
 const splitLine = (text) => {
   // Xdm Markdown parser every code line with \n
   const textArray = text.split(/\n/)
@@ -45,6 +66,17 @@ const splitLine = (text) => {
   })
 }
 
+/**
+ * Rehype plugin that highlights code blocks with refractor (prismjs)
+ *
+ * Set `showLineNumbers` to `true` to always display line number
+ *
+ * Set `ignoreMissing` to `true` to ignore unsupported languages and line highlighting when no language is specified
+ *
+ * @typedef {{ showLineNumbers?: boolean, ignoreMissing?: boolean }} RehypePrismOptions
+ * @param {RehypePrismOptions} options
+ * @return {Visitor}
+ */
 const rehypePrism = (options) => {
   options = options || {}
 
@@ -52,12 +84,20 @@ const rehypePrism = (options) => {
     visit(tree, 'element', visitor)
   }
 
+  /**
+   * @param {Node} node
+   * @param {number} index
+   * @param {Parent} parent
+   */
   function visitor(node, index, parent) {
     if (!parent || parent.tagName !== 'pre' || node.tagName !== 'code') {
       return
     }
 
     const lang = getLanguage(node)
+
+    /** @type {string} */
+    // @ts-ignore
     let meta = node.data && node.data.meta ? node.data.meta : ''
 
     if (lang) {

+ 7 - 1
package-lock.json

@@ -1,6 +1,6 @@
 {
   "name": "rehype-prism-plus",
-  "version": "0.0.1-beta.11",
+  "version": "0.0.1-beta.14",
   "lockfileVersion": 1,
   "requires": true,
   "dependencies": {
@@ -2284,6 +2284,12 @@
       "integrity": "sha512-t0rzBq87m3fVcduHDUFhKmyyX+9eo6WQjZvf51Ea/M0Q7+T374Jp1aUiyUl0GKxp8M/OETVHSDvmkyPgvX+X2w==",
       "dev": true
     },
+    "typescript": {
+      "version": "4.3.4",
+      "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.3.4.tgz",
+      "integrity": "sha512-uauPG7XZn9F/mo+7MrsRjyvbxFpzemRjKEZXS4AK83oP2KKOJPvb+9cO/gmnv8arWZvhnjVOXz7B49m1l0e9Ew==",
+      "dev": true
+    },
     "unified": {
       "version": "9.2.1",
       "resolved": "https://registry.npmjs.org/unified/-/unified-9.2.1.tgz",

+ 5 - 2
package.json

@@ -1,6 +1,6 @@
 {
   "name": "rehype-prism-plus",
-  "version": "0.0.1-beta.11",
+  "version": "0.0.1",
   "description": "rehype plugin to highlight code blocks in HTML with Prism (via refractor) with line highlighting and line numbers",
   "source": "index.js",
   "files": [
@@ -9,6 +9,7 @@
   "main": "./dist/rehype-prism-plus.umd.js",
   "module": "./dist/rehype-prism-plus.es.js",
   "type": "module",
+  "types": "./dist/index.d.ts",
   "exports": {
     ".": {
       "import": "./dist/rehype-prism-plus.es.js",
@@ -16,7 +17,8 @@
     }
   },
   "scripts": {
-    "build": "vite build --config vite.config.cjs",
+    "build": "tsc -b && vite build --config vite.config.cjs",
+    "tsc": "tsc --watch --noEmit",
     "lint": "eslint .",
     "prettier": "prettier --write '*.js'",
     "test": "uvu"
@@ -54,6 +56,7 @@
     "lint-staged": "^11.0.0",
     "prettier": "^2.3.2",
     "rehype": "^11.0.0",
+    "typescript": "^4.3.4",
     "uvu": "^0.5.1",
     "vite": "^2.3.8"
   },

+ 16 - 0
tsconfig.json

@@ -0,0 +1,16 @@
+{
+  "include": ["index.js"],
+  "compilerOptions": {
+    "target": "ES2020",
+    "lib": ["ES2020"],
+    "module": "ES2020",
+    "moduleResolution": "node",
+    "outDir": "dist",
+    "allowJs": true,
+    "checkJs": true,
+    "declaration": true,
+    "emitDeclarationOnly": true,
+    "allowSyntheticDefaultImports": true,
+    "skipLibCheck": true
+  }
+}

+ 2 - 0
vite.config.cjs

@@ -7,5 +7,7 @@ module.exports = {
       entry: path.resolve(__dirname, 'index.js'),
       name: 'rehype-prism-plus'
     },
+    // do not empty output directory as it contains typescript declarations.
+    emptyOutDir: false,
   }
 }