Skip to content

Commit

Permalink
Rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
3846masa committed Aug 8, 2017
0 parents commit 0c33ac0
Show file tree
Hide file tree
Showing 15 changed files with 2,415 additions and 0 deletions.
66 changes: 66 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@

# Created by https://www.gitignore.io/api/node

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env


# End of https://www.gitignore.io/api/node
lib
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright © 2017 3846masa

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
135 changes: 135 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# @paperist/remark-math

[![LICENSE][license-badge]][license]
[![NPM][npm-badge]][npm]
[![standard-readme compliant][standard-readme-badge]][standard-readme]

[npm]: https://www.npmjs.com/package/@paperist/remark-crossref
[license]: https://3846masa.mit-license.org
[standard-readme]: https://github.com/RichardLitt/standard-readme

[npm-badge]: https://img.shields.io/npm/v/@paperist/remark-crossref.svg?style=flat-square&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgBAMAAACBVGfHAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAbUExURcwAAOeIiP////G7u/ri4tIZGdpFReJsbPC3t075sZwAAAAvSURBVCjPY2CgDWAThIMEsACjEhwIUCZg0dGCIqASwMAxMgXAgSzOwMAOC2TqAwBvzR4JxLaP0gAAAABJRU5ErkJggg==
[license-badge]: https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQBAMAAADt3eJSAAAAIGNIUk0AAHomAACAhAAA%2BgAAAIDoAAB1MAAA6mAAADqYAAAXcJy6UTwAAAAVUExURSBTICJcIiNgIiZoJTuhNyt3Kf///%2BCqxSgAAAAGdFJOUwpclbn%2B4Fj6/H8AAAABYktHRAZhZrh9AAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4AkEEjEV7MDQQwAAAGBJREFUCNc1TUEKgDAMi07vE/Q%2BRD8g%2B4BbvAvi/79iMjDQJm1CC6BbDzRsZI3incIpYeYFhCaYnLiyPYnYkwWZFWoFHrSuttCmmbwXh0eJQYVON4JthZTxCzzAmyb8%2BAAKXBRyN6RyZQAAAABJRU5ErkJggg==
[standard-readme-badge]: https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square

> [wooorm/remark] plugin for math likes [KaTeX] / [MathJax]
[wooorm/remark]: https://github.com/wooorm/remark
[KaTeX]: https://khan.github.io/KaTeX/
[MathJax]: https://www.mathjax.org/

## Table of Contents

- [Install](#install)
- [Usage](#usage)
- [AST](#ast)
- [Contribute](#contribute)
- [License](#license)

## Install

```
npm i remark @paperist/remark-math
```

## Usage

```js
const unified = require('unified');
const parser = require('remark-parse');
const math = require('@paperist/remark-math');

const markdown = `
In an x-y Cartesian coordinate system,
the circle with centre coordinates \\((a, b)\\) and
radius \\(r\\) is the set of all points \\((x, y)\\) such that
\\[
(x - a)^2 + (y - b)^2 = r^2
\\]
> https://en.wikipedia.org/wiki/Circle
`;

const processor = unified().use(parser).use(math);
const ast = processor.parse(markdown);

processor.run(ast).then(ast => {
console.dir(ast, { depth: null });
});
```

## AST

See also [mdast], [unist].

[mdast]: https://github.com/syntax-tree/mdast
[unist]: https://github.com/syntax-tree/unist

### `Math`

`Math` extends [`Text`][unist-text].

```typescript
interface Math extends Text {
type: 'math';
math: string;
}
```

For example, the following markdown:

```md
\[
x^2 + y^2 = r^2
\]
```

Yields:

```json
{
"type": "math",
"value": "\\[\nx^2 + y^2 = r^2\n\\]",
"math": "\nx^2 + y^2 = r^2\n"
}
```

### `InlineMath`

`InlineMath` extends [`Text`][unist-text].

```typescript
interface InlineMath extends Text {
type: 'inlineMath';
math: string;
}
```

For example, the following markdown:

```md
\(E = mc^2\)
```

Yields:

```json
{
"type": "inlineMath",
"value": "\\(E = mc^2\\)",
"math": "E = mc^2"
}
```

[unist-text]: https://github.com/syntax-tree/unist#text

## Contribute

PRs accepted.

## License

![3846masa] MIT (c) 3846masa

[3846masa]: https://www.gravatar.com/avatar/cfeae69aae4f4fc102960f01d35d2d86?s=50
22 changes: 22 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
machine:
node:
version: 6
post:
- npm install -g npm
timezone: Asia/Tokyo
dependencies:
pre:
- npm config set progress false
- npm config set spin false
- npm install -d
override:
- npm run build
test:
override:
- npm test
deployment:
production:
tag: /v\d+(\.\d+)*/
commands:
- echo -e "$NPM_USER\n$NPM_PASS\n$NPM_EMAIL" | npm login
- npm publish --access=public
69 changes: 69 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"name": "@paperist/remark-math",
"description": "wooorm/remark plugin for math likes LaTeX / MathJax",
"author": "3846masa <[email protected]>",
"license": "MIT",
"homepage": "https://github.com/Paperist/remark-math#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/Paperist/remark-math.git"
},
"bugs": {
"url": "https://github.com/Paperist/remark-math/issues"
},
"version": "0.0.0",
"main": "lib/index.js",
"files": [
"lib"
],
"directories": {
"lib": "lib"
},
"scripts": {
"prebuild": "npm-run-all prebuild:clean prebuild:mkdir",
"build": "npm-run-all -p build:*",
"postbuild": "npm-run-all -p postbuild:*",
"prebuild:clean": "npm run clean",
"postbuild:copy-d.ts": "cpx \"src/**/*.d.ts\" lib",
"prebuild:mkdir": "mkdirp lib/peg",
"build:peg": "npm-run-all -p build:peg:*",
"build:peg:inlineMath": "pegjs -o ./lib/peg/inlineMath.js ./src/peg/inlineMath.pegjs",
"build:peg:math": "pegjs -o ./lib/peg/math.js ./src/peg/math.pegjs",
"build:tsc": "tsc",
"clean": "rimraf lib",
"precommit": "lint-staged"
},
"dependencies": {},
"peerDependencies": {
"remark-parse": "^4.0.0"
},
"devDependencies": {
"@paperist/types-remark": "^0.1.1",
"@types/node": "^8.0.19",
"@types/pegjs": "^0.10.0",
"cpx": "^1.5.0",
"husky": "^0.14.3",
"lint-staged": "^4.0.2",
"mkdirp": "^0.5.1",
"npm-run-all": "^4.0.2",
"pegjs": "^0.10.0",
"prettier-package-json": "^1.4.0",
"remark-parse": "^4.0.0",
"rimraf": "^2.6.1",
"typescript": "^2.4.2",
"unified": "^6.1.5"
},
"keywords": [
"katex",
"markdown",
"mathjax",
"remark"
],
"lint-staged": {
"package.json": [
"prettier-package-json --write",
"git add"
]
},
"types": "lib/index.d.ts"
}
64 changes: 64 additions & 0 deletions src/InlineMathTokenizer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { UNIST } from 'unist';
import { MDAST } from 'mdast';
import * as RemarkParse from 'remark-parse';

import { parse, ParseResult } from './peg/inlineMath';

declare module 'mdast' {
export namespace MDAST {
interface InlineMath extends UNIST.Text {
type: 'inlineMath';
math: string;
}
}
}

const InlineMathLocator: RemarkParse.Locator = (value, fromIndex) => {
let location = value.indexOf('$', fromIndex);
if (location === -1) {
location = value.indexOf('\\(', fromIndex);
}
return location;
};

const InlineMathTokenizerFunction: RemarkParse.TokenizerFunction = (
eat,
value,
silent
) => {
let result: ParseResult;
try {
result = parse(value);
} catch (err) {
return silent ? false : undefined;
}

if (silent) {
return true;
}

const matchStr = value.substring(
result.location.start.offset,
result.location.end.offset
);

const node: MDAST.InlineMath = {
type: 'inlineMath',
value: matchStr,
math: result.math,
};

return eat(matchStr)(node);
};

const InlineMathTokenizer: RemarkParse.Tokenizer = Object.assign(
InlineMathTokenizerFunction,
{
locator: InlineMathLocator,
notInBlock: true,
notInList: true,
notInLink: true,
}
);

export default InlineMathTokenizer;
Loading

0 comments on commit 0c33ac0

Please sign in to comment.