Skip to content

Commit

Permalink
Refactor prose, code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 7, 2019
1 parent 5e71250 commit 9993e7e
Show file tree
Hide file tree
Showing 22 changed files with 985 additions and 772 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage/
49 changes: 0 additions & 49 deletions examples/nodejs/process.js

This file was deleted.

48 changes: 0 additions & 48 deletions examples/webpack/assets/index.html

This file was deleted.

21 changes: 0 additions & 21 deletions examples/webpack/entry.js

This file was deleted.

35 changes: 0 additions & 35 deletions examples/webpack/webpack.config.js

This file was deleted.

File renamed without changes.
54 changes: 31 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
"description": "Math Inline and Block supporting for Remark",
"scripts": {
"postinstall": "lerna bootstrap --no-ci",
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
"test": "jest --coverage",
"test:api": "jest",
"test:watch": "jest --watch",
"pretest": "npm run lint --silent",
"lint": "standard --fix",
"demo:webpack": "webpack-dev-server --config examples/webpack/webpack.config.js",
"demo:nodejs": "node examples/nodejs/process.js",
"pretest": "npm run format",
"release": "lerna publish"
},
"repository": {
Expand All @@ -32,39 +30,24 @@
},
"homepage": "https://github.com/Rokt33r/remark-math#readme",
"devDependencies": {
"babel-eslint": "^10.0.0",
"css-loader": "^3.0.0",
"eslint-plugin-jest": "^22.0.0",
"file-loader": "^4.0.0",
"github-markdown-css": "^3.0.0",
"hastscript": "^5.0.0",
"jest": "^24.0.0",
"katex": "^0.10.0",
"lerna": "^3.0.0",
"prettier": "^1.0.0",
"rehype-parse": "^6.0.0",
"rehype-stringify": "^6.0.0",
"remark-cli": "^7.0.0",
"remark-html": "^10.0.0",
"remark-parse": "^7.0.0",
"remark-preset-wooorm": "^6.0.1",
"remark-rehype": "^5.0.0",
"remark-stringify": "^7.0.0",
"standard": "^13.0.0",
"style-loader": "^1.0.0",
"unified": "^8.0.0",
"unist-builder": "^1.0.0",
"webpack": "^4.0.0",
"webpack-cli": "^3.0.0",
"webpack-dev-server": "^3.0.0"
"xo": "^0.24.0"
},
"dependencies": {},
"standard": {
"parser": "babel-eslint",
"plugins": [
"jest"
],
"envs": [
"jest"
]
},
"jest": {
"collectCoverage": false,
"collectCoverageFrom": [
Expand All @@ -78,5 +61,30 @@
"statements": 100
}
}
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"bracketSpacing": false,
"semi": false,
"trailingComma": "none"
},
"xo": {
"prettier": true,
"esnext": false,
"rules": {
"complexity": "off",
"no-eq-null": "off",
"eqeqeq": [
2,
"allow-null"
]
}
},
"remarkConfig": {
"plugins": [
"preset-wooorm"
]
}
}
42 changes: 26 additions & 16 deletions packages/rehype-katex/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const unified = require('unified')
const parse = require('rehype-parse')
const position = require('unist-util-position')

function parseMathHtml (html) {
module.exports = plugin

function parseMathHtml(html) {
return unified()
.use(parse, {
fragment: true,
Expand All @@ -13,23 +15,32 @@ function parseMathHtml (html) {
.parse(html)
}

function hasClass (element, className) {
return element.properties.className && element.properties.className.includes(className)
function hasClass(element, className) {
return (
element.properties.className &&
element.properties.className.includes(className)
)
}

function isTag (element, tag) {
function isTag(element, tag) {
return element.tagName === tag
}

module.exports = function plugin (opts) {
function plugin(opts) {
if (opts == null) opts = {}
if (opts.throwOnError == null) opts.throwOnError = false
if (opts.errorColor == null) opts.errorColor = '#cc0000'
if (opts.macros == null) opts.macros = {}
return function transform (node, file) {
visit(node, 'element', function (element) {
const isInlineMath = isTag(element, 'span') && hasClass(element, 'inlineMath')
const isMath = (opts.inlineMathDoubleDisplay && hasClass(element, 'inlineMathDouble')) || (isTag(element, 'div') && hasClass(element, 'math'))
return transform

function transform(node, file) {
visit(node, 'element', function(element) {
const isInlineMath =
isTag(element, 'span') && hasClass(element, 'inlineMath')
const isMath =
(opts.inlineMathDoubleDisplay &&
hasClass(element, 'inlineMathDouble')) ||
(isTag(element, 'div') && hasClass(element, 'math'))

if (isInlineMath || isMath) {
let renderedValue
Expand All @@ -39,14 +50,11 @@ module.exports = function plugin (opts) {
macros: opts.macros,
strict: opts.strict
})
} catch (err) {
} catch (error) {
if (opts.throwOnError) {
throw err
throw error
} else {
file.message(
err.message,
position.start(element)
)
file.message(error.message, position.start(element))

renderedValue = katex.renderToString(element.children[0].value, {
displayMode: isMath,
Expand All @@ -60,7 +68,9 @@ module.exports = function plugin (opts) {

const inlineMathAst = parseMathHtml(renderedValue).children[0]

Object.assign(element.properties, { className: element.properties.className })
Object.assign(element.properties, {
className: element.properties.className
})
element.children = [inlineMathAst]
}
})
Expand Down
3 changes: 2 additions & 1 deletion packages/rehype-katex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@
},
"files": [
"index.js"
]
],
"xo": false
}
Loading

0 comments on commit 9993e7e

Please sign in to comment.