@paperist/remark-crossref
Advanced tools
Comparing version 0.0.3 to 1.0.0
@@ -1,5 +0,4 @@ | ||
/** | ||
* Attacher. | ||
*/ | ||
declare function attacher(remark: any): void; | ||
export default attacher; | ||
import * as RemarkParse from 'remark-parse'; | ||
import transformer from './transformer'; | ||
declare function attacher(this: RemarkParse): typeof transformer; | ||
export = attacher; |
"use strict"; | ||
const qs = require('querystring'); | ||
/** | ||
* Tokenise a cross-ref label. | ||
* | ||
* @example | ||
* tokenizeCrossReferenceLabel(eat, '{#fig:image}'); | ||
*/ | ||
function tokenizeCrossReferenceLabel(eat, value, silent = false) { | ||
const matches = new RegExp(/^(\s*{#(.*?)})/).exec(value); | ||
if (!matches) { | ||
return; | ||
} | ||
if (silent) { | ||
return true; | ||
} | ||
const matchStr = matches[1]; | ||
const content = matches[2]; | ||
const [label, optionStr] = content.split(/\s+/); | ||
return eat(matchStr)({ | ||
label, | ||
options: qs.parse(optionStr, ',', '='), | ||
type: 'crossReferenceLabel', | ||
value: matchStr, | ||
}); | ||
} | ||
/** | ||
* Find a possible cross-ref label. | ||
* | ||
* @example | ||
* locateCrossReferenceLabel('![](){#fig:image}'); // 5 | ||
* | ||
*/ | ||
function locateCrossReferenceLabel(value, fromIndex) { | ||
const idx = value.substr(fromIndex).search(/\s*{#/); | ||
if (idx === -1) { | ||
return idx; | ||
} | ||
return idx + fromIndex; | ||
} | ||
/** | ||
* Tokenise a cross-ref. | ||
* | ||
* @example | ||
* tokenizeCrossReference(eat, '[@fig:image]'); | ||
*/ | ||
function tokenizeCrossReference(eat, value, silent = false) { | ||
const matches = new RegExp(/^(\[(@.*?)\])/).exec(value); | ||
if (!matches) { | ||
return; | ||
} | ||
if (silent) { | ||
return true; | ||
} | ||
const matchStr = matches[1]; | ||
const identifier = matches[2]; | ||
return eat(matchStr)({ | ||
identifier, | ||
type: 'crossReference', | ||
value: matchStr, | ||
}); | ||
} | ||
/** | ||
* Find a possible cross-ref. | ||
* | ||
* @example | ||
* locateCrossReference('![]()[@fig:image]'); // 5 | ||
* | ||
*/ | ||
function locateCrossReference(value, fromIndex) { | ||
return value.indexOf('[@', fromIndex); | ||
} | ||
/** | ||
* Attacher. | ||
*/ | ||
function attacher(remark) { | ||
const proto = remark.Parser.prototype; | ||
const inlineMethods = proto.inlineMethods; | ||
/** | ||
* Add a tokenizer to the `Parser`. | ||
*/ | ||
proto.inlineTokenizers.crossReferenceLabel = | ||
Object.assign(tokenizeCrossReferenceLabel, { locator: locateCrossReferenceLabel }); | ||
const CrossReferenceLabelTokenizer_1 = require("./CrossReferenceLabelTokenizer"); | ||
const transformer_1 = require("./transformer"); | ||
function attacher() { | ||
const Parser = this.Parser; | ||
const inlineTokenizers = Parser.prototype.inlineTokenizers; | ||
const inlineMethods = Parser.prototype.inlineMethods; | ||
inlineTokenizers['crossReferenceLabel'] = CrossReferenceLabelTokenizer_1.default; | ||
inlineMethods.splice(inlineMethods.indexOf('text'), 0, 'crossReferenceLabel'); | ||
proto.inlineTokenizers.crossReference = | ||
Object.assign(tokenizeCrossReference, { locator: locateCrossReference }); | ||
inlineMethods.splice(inlineMethods.indexOf('link'), 0, 'crossReference'); | ||
return transformer_1.default; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = attacher; | ||
module.exports = attacher; |
{ | ||
"name": "@paperist/remark-crossref", | ||
"description": "wooorm/remark plugin for corss-references inspired by pandoc-crossref", | ||
"version": "0.0.3", | ||
"dependencies": {}, | ||
"author": "3846masa <3846masahiro+git@gmail.com>", | ||
"license": "MIT", | ||
"version": "1.0.0", | ||
"main": "lib/index.js", | ||
"files": [ | ||
"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:crossReference": "pegjs -o ./lib/peg/crossReference.js ./src/peg/crossReference.pegjs", | ||
"build:peg:crossReferenceLabel": "pegjs -o ./lib/peg/crossReferenceLabel.js ./src/peg/crossReferenceLabel.pegjs", | ||
"build:tsc": "tsc", | ||
"clean": "rimraf lib", | ||
"precommit": "lint-staged" | ||
}, | ||
"dependencies": { | ||
"unist-util-visit": "^1.1.3" | ||
}, | ||
"peerDependencies": {}, | ||
"devDependencies": { | ||
"@types/node": "^6.0.48", | ||
"greenkeeper-postpublish": "^1.0.1", | ||
"tslint": "^4.0.1", | ||
"typescript": "^2.0.10" | ||
"@paperist/types-remark": "^0.1.0", | ||
"@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" | ||
}, | ||
"files": [ | ||
"package.json", | ||
"README", | ||
"LICENSE", | ||
"lib", | ||
"index.js" | ||
], | ||
"keywords": [ | ||
@@ -24,9 +50,9 @@ "crossref", | ||
], | ||
"license": "MIT", | ||
"main": "index.js", | ||
"scripts": { | ||
"build": "tsc", | ||
"test": "exit 0", | ||
"postpublish": "greenkeeper-postpublish" | ||
} | ||
"lint-staged": { | ||
"package.json": [ | ||
"prettier-package-json --write", | ||
"git add" | ||
] | ||
}, | ||
"types": "lib/index.d.ts" | ||
} |
113
README.md
@@ -1,11 +0,9 @@ | ||
# remark-crossref | ||
# @paperist/remark-crossref | ||
[![standard-readme compliant][standard-readme-badge]][standard-readme] | ||
[![LICENSE][license-badge]][license] | ||
[![NPM][npm-badge]][npm] | ||
[![CircleCI][circleci-badge]][circleci] | ||
[![standard-readme compliant][standard-readme-badge]][standard-readme] | ||
[npm]: https://www.npmjs.com/package/@paperist/remark-crossref | ||
[license]: https://3846masa.mit-license.org | ||
[circleci]: https://circleci.com/gh/Paperist/remark-crossref | ||
[standard-readme]: https://github.com/RichardLitt/standard-readme | ||
@@ -15,3 +13,2 @@ | ||
[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== | ||
[circleci-badge]: https://img.shields.io/circleci/project/Paperist/remark-crossref/master.svg?style=flat-square&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQEAYAAABPYyMiAAAAIGNIUk0AAHomAACAhAAA%2BgAAAIDoAAB1MAAA6mAAADqYAAAXcJy6UTwAAAAGYktHRP///////wlY99wAAAAHdElNRQfgCQQSJS8EYt6kAAAAiklEQVRIx2M41nqs9Xi9WDQh%2BjQQnDnz%2BS5x9KS9xJrLgN/CSXtJs5h0BzHQ1mLCDmEgL4jJpyl0AOG4JTYE4Q6gdqIi1UMM2F1OrE9xBy2xDmGgrs8wHU5nB2CGBNEOQPcBoaigThQA08AgS4QDkA3pXRChe4SBskqHiiUhreoEsmtDyhxEfKIFAG4yoGuqR9fTAAAAAElFTkSuQmCC | ||
[standard-readme-badge]: https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square | ||
@@ -28,2 +25,3 @@ | ||
- [Usage](#usage) | ||
- [AST](#ast) | ||
- [Contribute](#contribute) | ||
@@ -41,14 +39,107 @@ - [License](#license) | ||
```js | ||
const remark = require('remark'); | ||
const unified = require('unified'); | ||
const parser = require('remark-parse'); | ||
const crossref = require('@paperist/remark-crossref'); | ||
const markdown = ` | ||
# Heading {#sec:heading} | ||
# Heading {#sec:first} | ||
See sec.[@sec:heading]. | ||
See sec.[@sec:first]. | ||
`; | ||
remark().use(crossref).parse(markdown); | ||
const processor = unified().use(parser).use(crossref); | ||
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 | ||
### `CrossReferenceLabel` | ||
`CrossReferenceLabel` extends [`Text`][unist-text]. | ||
```typescript | ||
interface CrossReferenceLabel extends Text { | ||
type: 'crossReferenceLabel'; | ||
label: string; | ||
options: { [key: string]: any }; | ||
} | ||
``` | ||
For example, the following markdown: | ||
```md | ||
# Heading {#sec:first} | ||
``` | ||
Yields: | ||
```json | ||
{ | ||
"type": "heading", | ||
"depth": 1, | ||
"children": [ | ||
{ | ||
"type": "text", | ||
"value": "Heading ", | ||
}, | ||
{ | ||
"label": "sec:first", | ||
"value": "{#sec:first}", | ||
"options": {}, | ||
"type": "crossReferenceLabel", | ||
} | ||
] | ||
} | ||
``` | ||
### `CrossReference` | ||
`CrossReference` extends [`Text`][unist-text]. | ||
```typescript | ||
interface CrossReference extends Text { | ||
type: 'crossReference'; | ||
identifiers: string[]; | ||
} | ||
``` | ||
For example, the following markdown: | ||
```md | ||
See sec.[@sec:first;@sec:second] | ||
``` | ||
Yields: | ||
```json | ||
{ | ||
"type": "paragraph", | ||
"children": [ | ||
{ | ||
"type": "text", | ||
"value": "See sec." | ||
}, | ||
{ | ||
"type": "crossReference", | ||
"value": "[@sec:first;@sec:second]", | ||
"identifiers": [ | ||
"sec:first", | ||
"sec:second" | ||
] | ||
} | ||
] | ||
} | ||
``` | ||
[unist-text]: https://github.com/syntax-tree/unist#text | ||
## Contribute | ||
@@ -60,2 +151,4 @@ | ||
MIT © 3846masa | ||
![3846masa] MIT (c) 3846masa | ||
[3846masa]: https://www.gravatar.com/avatar/cfeae69aae4f4fc102960f01d35d2d86?s=50 |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
43731
15
1315
2
1
151
1
14
2
+ Addedunist-util-visit@^1.1.3
+ Addedunist-util-is@3.0.0(transitive)
+ Addedunist-util-visit@1.4.1(transitive)
+ Addedunist-util-visit-parents@2.1.2(transitive)