Skip to content

Commit

Permalink
Add JSDoc based types
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 9, 2021
1 parent 5d86d87 commit edf4ca2
Show file tree
Hide file tree
Showing 42 changed files with 420 additions and 426 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
coverage/
node_modules/
.DS_Store
*.d.ts
*.log
yarn.lock
19 changes: 12 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
],
"type": "module",
"devDependencies": {
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"dtslint": "^4.0.0",
"lerna": "^4.0.0",
"prettier": "^2.0.0",
"rehype-parse": "^8.0.0",
Expand All @@ -29,8 +29,10 @@
"remark-preset-wooorm": "^8.0.0",
"remark-rehype": "^9.0.0",
"remark-stringify": "^10.0.0",
"rimraf": "^3.0.0",
"tape": "^5.0.0",
"to-vfile": "^7.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"unified": "^10.0.0",
"unist-builder": "^3.0.0",
Expand All @@ -39,11 +41,11 @@
},
"scripts": {
"postinstall": "lerna bootstrap --no-ci",
"build": "lerna run build",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "lerna run test-api",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov npm run test-api",
"test-types": "lerna run test-types --concurrency 1 --stream",
"test": "npm run format && npm run test-coverage && npm run test-types"
"test": "npm run build && npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -54,14 +56,17 @@
"trailingComma": "none"
},
"xo": {
"prettier": true,
"rules": {
"@typescript-eslint/ban-types": "off"
}
"prettier": true
},
"remarkConfig": {
"plugins": [
"preset-wooorm"
]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true,
"ignoreCatch": true
}
}
28 changes: 20 additions & 8 deletions packages/rehype-katex/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* @typedef {import('hast').Root} Root
* @typedef {import('katex').KatexOptions} Options
*/

import katex from 'katex'
import {visit} from 'unist-util-visit'
import {removePosition} from 'unist-util-remove-position'
Expand All @@ -11,17 +16,22 @@ const parseHtml = unified().use(rehypeParse, {fragment: true})

const source = 'rehype-katex'

/**
* Plugin to transform `<span class=math-inline>` and `<div class=math-display>`
* with KaTeX.
*
* @type {import('unified').Plugin<[Options?]|void[], Root>}
*/
export default function rehypeKatex(options) {
const settings = options || {}
const throwOnError = settings.throwOnError || false

return transformMath

function transformMath(tree, file) {
visit(tree, 'element', onelement)

function onelement(element) {
const classes = element.properties.className || []
return (tree, file) => {
visit(tree, 'element', (element) => {
const classes =
element.properties && Array.isArray(element.properties.className)
? element.properties.className
: []
const inline = classes.includes('math-inline')
const displayMode = classes.includes('math-display')

Expand All @@ -31,6 +41,7 @@ export default function rehypeKatex(options) {

const value = toText(element)

/** @type {string} */
let result

try {
Expand All @@ -54,7 +65,8 @@ export default function rehypeKatex(options) {
)
}

// @ts-expect-error: assume no `doctypes` in KaTeX result.
element.children = removePosition(parseHtml.parse(result), true).children
}
})
}
}
15 changes: 12 additions & 3 deletions packages/rehype-katex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js"
],
"dependencies": {
"@types/hast": "^2.0.0",
"@types/katex": "^0.11.0",
"hast-util-to-text": "^3.0.0",
"katex": "^0.13.0",
Expand All @@ -45,9 +48,15 @@
"unist-util-visit": "^4.0.0"
},
"scripts": {
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"test-api": "node --conditions development test.js",
"test-types": "dtslint types",
"test": "npm run test-api && npm run test-types"
"test": "npm run build && npm run test-api"
},
"xo": false
"xo": false,
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true,
"ignoreCatch": true
}
}
2 changes: 1 addition & 1 deletion packages/rehype-katex/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ test('rehype-katex', (t) => {

t.deepEqual(
unified()
.use(remarkParse, {position: false})
.use(remarkParse)
.use(remarkMath)
.use(remarkRehype)
.use(rehypeKatex)
Expand Down
4 changes: 4 additions & 0 deletions packages/rehype-katex/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["*.js"]
}
8 changes: 0 additions & 8 deletions packages/rehype-katex/types/index.d.ts

This file was deleted.

9 changes: 0 additions & 9 deletions packages/rehype-katex/types/test.ts

This file was deleted.

9 changes: 0 additions & 9 deletions packages/rehype-katex/types/tsconfig.json

This file was deleted.

7 changes: 0 additions & 7 deletions packages/rehype-katex/types/tslint.json

This file was deleted.

12 changes: 0 additions & 12 deletions packages/rehype-mathjax/browser.d.ts

This file was deleted.

54 changes: 34 additions & 20 deletions packages/rehype-mathjax/browser.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
/**
* @typedef {import('hast').Root} Root
* @typedef {import('hast').Element} Element
* @typedef {import('./lib/create-plugin').MathNotation} MathNotation
* @typedef {import('./lib/create-plugin').BrowserOptions} Options
*/

import {createPlugin} from './lib/create-plugin.js'

const rehypeMathJaxBrowser = createPlugin(
'rehypeMathJaxBrowser',
renderBrowser,
false,
true
)
export default rehypeMathJaxBrowser
const rehypeMathJaxBrowser =
/** @type {import('unified').Plugin<[Options?]|void[], Root>} */ (
createPlugin(
// To do next major: Make `options` match the format of MathJax options
// `{tex: ...}`
(_, options) => {
/** @type {MathNotation} */
let display = ['\\[', '\\]']
/** @type {MathNotation} */
let inline = ['\\(', '\\)']

// To do next major: Make `options` match the format of MathJax options
// `{tex: ...}`
function renderBrowser(options) {
const settings = options || {}
const display = settings.displayMath || ['\\[', '\\]']
const inline = settings.inlineMath || ['\\(', '\\)']
if ('displayMath' in options && options.displayMath) {
display = options.displayMath
}

return {render}
if ('inlineMath' in options && options.inlineMath) {
inline = options.inlineMath
}

function render(node, renderOptions) {
const delimiters = renderOptions.display ? display : inline
node.children.unshift({type: 'text', value: delimiters[0]})
node.children.push({type: 'text', value: delimiters[1]})
}
}
return {
render(node, options) {
const delimiters = options.display ? display : inline
node.children.unshift({type: 'text', value: delimiters[0]})
node.children.push({type: 'text', value: delimiters[1]})
}
}
}
)
)
export default rehypeMathJaxBrowser
43 changes: 0 additions & 43 deletions packages/rehype-mathjax/chtml.d.ts

This file was deleted.

23 changes: 14 additions & 9 deletions packages/rehype-mathjax/chtml.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import {createInput} from './lib/create-input.js'
/**
* @typedef {import('hast').Root} Root
* @typedef {import('./lib/create-plugin').CHtmlOptions} Options
*/

import {createOutputChtml} from './lib/create-output-chtml.js'
import {createRenderer} from './lib/create-renderer.js'
import {createPlugin} from './lib/create-plugin.js'

const rehypeMathJaxCHtml = createPlugin('rehypeMathJaxCHtml', renderCHtml, true)
const rehypeMathJaxCHtml =
/** @type {import('unified').Plugin<[Options?]|void[], Root>} */
(
createPlugin(
(inputOptions, outputOptions) =>
createRenderer(inputOptions, createOutputChtml(outputOptions)),
true
)
)

export default rehypeMathJaxCHtml

function renderCHtml(inputOptions, outputOptions) {
return createRenderer(
createInput(inputOptions),
createOutputChtml(outputOptions)
)
}
Loading

0 comments on commit edf4ca2

Please sign in to comment.