remark-abbr
Advanced tools
Comparing version 0.0.6 to 0.0.8
@@ -12,6 +12,6 @@ 'use strict'; | ||
var regex = new RegExp(/[*]\[([^\]]*)\]:\s*(.+)\n*/); | ||
var keep = regex.exec(value); | ||
var keep = regex.exec(value | ||
/* istanbul ignore if - never used (yet) */ | ||
if (silent) return true; | ||
);if (silent) return silent; | ||
if (!keep || keep.index !== 0) return; | ||
@@ -21,7 +21,8 @@ | ||
type: 'abbr', | ||
abbr: keep[1], | ||
children: [{ type: 'text', value: keep[1] }], | ||
data: { | ||
hName: 'abbr', | ||
hProperties: { | ||
word: keep[1], | ||
desc: keep[2] | ||
title: keep[2] | ||
} | ||
@@ -31,15 +32,6 @@ } | ||
} | ||
inlineTokenizer.locator = locator; | ||
var Parser = this.Parser; | ||
// Inject inlineTokenizer | ||
var inlineTokenizers = Parser.prototype.inlineTokenizers; | ||
var inlineMethods = Parser.prototype.inlineMethods; | ||
inlineTokenizers.abbr = inlineTokenizer; | ||
inlineMethods.splice(0, 0, 'abbr'); | ||
function transformer(tree) { | ||
var abbrs = {}; | ||
visit(tree, 'element', find(abbrs)); | ||
visit(tree, 'paragraph', find(abbrs)); | ||
visit(tree, replace(abbrs)); | ||
@@ -49,18 +41,14 @@ } | ||
function find(abbrs) { | ||
function one(node, index, parent) { | ||
if (node.tagName === 'p') { | ||
for (var i = 0; i < node.children.length; ++i) { | ||
var child = node.children[i]; | ||
if (child.tagName === 'abbr') { | ||
// Store abbreviation | ||
abbrs[child.properties.word] = child.properties.desc; | ||
node.children.splice(i, 1); | ||
i -= 1; | ||
} | ||
} | ||
// Remove paragraph if there is no child | ||
if (node.children.length === 0) parent.children.splice(index, 1); | ||
return function one(node, index, parent) { | ||
for (var i = 0; i < node.children.length; i++) { | ||
var child = node.children[i]; | ||
if (child.type !== 'abbr') continue; | ||
// Store abbr node for later use | ||
abbrs[child.abbr] = child; | ||
node.children.splice(i, 1); | ||
i -= 1; | ||
} | ||
} | ||
return one; | ||
// Remove paragraph if there is no child | ||
if (node.children.length === 0) parent.children.splice(index, 1); | ||
}; | ||
} | ||
@@ -70,3 +58,4 @@ | ||
function escapeRegExp(str) { | ||
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&'); // eslint-disable-line no-useless-escape | ||
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&' // eslint-disable-line no-useless-escape | ||
); | ||
} | ||
@@ -79,22 +68,26 @@ | ||
if (Object.keys(abbrs).length === 0) return; | ||
if (node.type !== 'text') return; | ||
if (!node.children) return; | ||
var keep = regex.exec(node.value); | ||
if (keep) { | ||
var newTexts = node.value.split(regex); | ||
parent.children = []; | ||
for (var i = 0; i < newTexts.length; ++i) { | ||
// If a text node is present in child nodes, check if an abbreviation is present | ||
for (var c = 0; c < node.children.length; c++) { | ||
var child = node.children[c]; | ||
if (node.type === 'abbr' || child.type !== 'text') continue; | ||
if (!regex.test(child.value)) continue; | ||
// Transform node | ||
var newTexts = child.value.split(regex | ||
// Remove old text node | ||
);node.children.splice(c, 1 | ||
// Replace abbreviations | ||
);for (var i = 0; i < newTexts.length; i++) { | ||
var content = newTexts[i]; | ||
if (abbrs.hasOwnProperty(content)) { | ||
parent.children[i] = { | ||
type: 'element', | ||
tagName: 'abbr', | ||
properties: { title: abbrs[content] }, | ||
children: [{ type: 'text', value: content }] | ||
}; | ||
node.children.splice(c + i, 0, abbrs[content]); | ||
} else { | ||
parent.children[i] = { | ||
node.children.splice(c + i, 0, { | ||
type: 'text', | ||
value: content | ||
}; | ||
}); | ||
} | ||
@@ -104,6 +97,15 @@ } | ||
} | ||
return one; | ||
} | ||
inlineTokenizer.locator = locator; | ||
var Parser = this.Parser; | ||
// Inject inlineTokenizer | ||
var inlineTokenizers = Parser.prototype.inlineTokenizers; | ||
var inlineMethods = Parser.prototype.inlineMethods; | ||
inlineTokenizers.abbr = inlineTokenizer; | ||
inlineMethods.splice(0, 0, 'abbr'); | ||
return transformer; | ||
@@ -110,0 +112,0 @@ } |
{ | ||
"name": "remark-abbr", | ||
"version": "0.0.6", | ||
"version": "0.0.8", | ||
"repository": { | ||
@@ -16,5 +16,5 @@ "url": "https://github.com/zestedesavoir/zmarkdown/tree/master/packages/remark-abbr", | ||
"pretest": "eslint src", | ||
"prepublish": "del-cli dist && BABEL_ENV=production babel --out-dir dist src", | ||
"test": "npm run prepublish && nyc ava __tests__", | ||
"coverage": "./node_modules/.bin/nyc report --reporter=text-lcov > coverage/coverage.lcov" | ||
"prepare": "del-cli dist && cross-env BABEL_ENV=production babel --out-dir dist src", | ||
"test": "jest", | ||
"coverage": "jest --coverage" | ||
}, | ||
@@ -34,4 +34,16 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"unist-util-visit": "^1.1.1" | ||
"unist-util-visit": "^1.1.3" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.24.1", | ||
"babel-jest": "^20.0.3", | ||
"babel-preset-es2015": "^6.24.1", | ||
"cross-env": "^5.0.1", | ||
"dedent": "^0.7.0", | ||
"del-cli": "^1.0.0", | ||
"eslint": "^4.0.0", | ||
"jest": "^20.0.4", | ||
"remark-parse": "^3.0.1", | ||
"unified": "^6.1.5" | ||
} | ||
} |
@@ -1,15 +0,73 @@ | ||
This plug-in adds custom Markdown syntax to use HTML abbreviations. | ||
# remark-abbr [![Build Status][build-badge]][build-status] [![Coverage Status][coverage-badge]][coverage-status] | ||
This plugin parses custom Markdown syntax to produce (HTML) abbreviations. | ||
## Syntax | ||
An abbreviation works the same as footnotes: | ||
```markdown | ||
This plugin works on MDAST, a Markdown AST | ||
implemented by [remark](https://github.com/wooorm/remark) | ||
*[HAST]: Markdown Abstract Syntax Tree. | ||
*[AST]: Abstract syntax tree | ||
``` | ||
This is an abbreviation: "REF". | ||
ref and REFERENCE should be ignored. | ||
*[REF]: Reference | ||
produces: | ||
```html | ||
<p>This plugin works on <abbr title="Markdown Abstract Syntax Tree.">MDAST</abbr>, a Markdown <abbr title="Abstract syntax tree">AST</abbr> | ||
implemented by <a href="https://github.com/wooorm/remark">remark</a></p> | ||
``` | ||
-> | ||
## Installation | ||
```HTML | ||
<p>This is an abbreviation: "<abbr title="Reference">REF</abbr>". | ||
ref and REFERENCE should be ignored.</p> | ||
[npm][npm]: | ||
```bash | ||
npm install remark-abbr | ||
``` | ||
## Usage | ||
Dependencies: | ||
```javascript | ||
const unified = require('unified') | ||
const remarkParse = require('remark-parse') | ||
const remarkAbbr = require('remark-abbr') | ||
const stringify = require('rehype-stringify') | ||
const remark2rehype = require('remark-rehype') | ||
``` | ||
Usage: | ||
```javascript | ||
unified() | ||
.use(remarkParse) | ||
.use(remarkAbbr) | ||
.use(remark2rehype) | ||
.use(stringify) | ||
``` | ||
## License | ||
[MIT][license] © [Zeste de Savoir][zds] | ||
<!-- Definitions --> | ||
[build-badge]: https://img.shields.io/travis/zestedesavoir/zmarkdown.svg | ||
[build-status]: https://travis-ci.org/zestedesavoir/zmarkdown | ||
[coverage-badge]: https://img.shields.io/coveralls/zestedesavoir/zmarkdown.svg | ||
[coverage-status]: https://coveralls.io/github/zestedesavoir/zmarkdown | ||
[license]: https://github.com/zestedesavoir/zmarkdown/blob/master/packages/remark-abbr/LICENSE-MIT | ||
[zds]: https://zestedesavoir.com | ||
[npm]: https://www.npmjs.com/package/remark-abbr |
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
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
6818
74
10
87
Updatedunist-util-visit@^1.1.3