Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

textlint-util-to-string

Package Overview
Dependencies
Maintainers
3
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

textlint-util-to-string - npm Package Compare versions

Comparing version 2.1.1 to 3.0.0

lib/index.d.ts

14

lib/index.js
// LICENSE : MIT
"use strict";
var _StringSource = require("./StringSource");
var _StringSource2 = _interopRequireDefault(_StringSource);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
module.exports = _StringSource2.default;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var StringSource_1 = __importDefault(require("./StringSource"));
exports.StringSource = StringSource_1.default;
//# sourceMappingURL=index.js.map
// LICENSE : MIT
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _objectAssign = require("object-assign");
var _objectAssign2 = _interopRequireDefault(_objectAssign);
var _structuredSource = require("structured-source");
var _structuredSource2 = _interopRequireDefault(_structuredSource);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var StringSource = function () {
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var structured_source_1 = __importDefault(require("structured-source"));
var unified = require("unified");
var parse = require("rehype-parse");
var html2hast = function (html) {
return unified()
.use(parse, { fragment: true })
.parse(html);
};
var StringSource = /** @class */ (function () {
function StringSource(node) {
_classCallCheck(this, StringSource);
this.rootNode = node;

@@ -31,297 +22,258 @@ this.tokenMaps = [];

this._stringify(this.rootNode);
this.originalSource = new _structuredSource2.default(this.rootNode.raw);
this.generatedSource = new _structuredSource2.default(this.generatedString);
/*
[
// e.g.) **Str**
{
// original range
// e.g.) [0, 7] = `**Str**`
original : [start, end]
// intermediate = trim decoration from Original
// e.g.) [2, 5]
intermediate: [start, end]
// generated value = "Str"
// e.g.) [0, 3]
generated : [start, end]
}]
*/
this.originalSource = new structured_source_1.default(this.rootNode.raw);
this.generatedSource = new structured_source_1.default(this.generatedString);
}
StringSource.prototype.toString = function () {
return this.generatedString;
};
/**
* @deprecated use originalIndexFromIndex instead of
* @param targetIndex
*/
StringSource.prototype.originalIndexFor = function (targetIndex) {
return this.originalIndexFromIndex(targetIndex);
};
/**
* @deprecated use originalPositionFromPosition instead of
* @param generatedPosition
* @param {boolean} isEnd - is the position end of the node?
_createClass(StringSource, [{
key: "toString",
value: function toString() {
return this.generatedString;
* @returns {Object}
*/
StringSource.prototype.originalPositionFor = function (generatedPosition, isEnd) {
if (isEnd === void 0) { isEnd = false; }
return this.originalPositionFromPosition(generatedPosition, isEnd);
};
/**
* get original index from generated index value
* @param {number} generatedIndex - position is a index value.
* @param {boolean} isEnd - is the position end of the node?
* @returns {number|undefined} original
*/
StringSource.prototype.originalIndexFromIndex = function (generatedIndex, isEnd) {
var _this = this;
if (isEnd === void 0) { isEnd = false; }
var hitTokenMaps = this.tokenMaps.filter(function (tokenMap, index) {
var generated = tokenMap.generated;
var nextTokenMap = _this.tokenMaps[index + 1];
var nextGenerated = nextTokenMap ? nextTokenMap.generated : null;
if (!generated) {
return false;
}
if (nextGenerated) {
if (generated[0] <= generatedIndex && generatedIndex <= nextGenerated[0]) {
return true;
}
}
else {
if (generated[0] <= generatedIndex && generatedIndex <= generated[1]) {
return true;
}
}
return false;
});
if (hitTokenMaps.length === 0) {
return;
}
/**
* @deprecated use originalIndexFromIndex instead of
* @param targetIndex
* **Str**ABC
* |
* |
* generatedIndex
*
* If isEnd is true, generatedIndex is end of **Str** node.
* If isEnd is false, generatedIndex is index of ABC node.
*/
}, {
key: "originalIndexFor",
value: function originalIndexFor(targetIndex) {
return this.originalIndexFromIndex(targetIndex);
var hitTokenMap = isEnd ? hitTokenMaps[0] : hitTokenMaps[hitTokenMaps.length - 1];
// <----------->[<------------->|text]
// ^ ^
// position-generated intermediate-origin
// <-------------->[<------------->|text]
// | |
// outer adjust _
// inner adjust = 1
if (!hitTokenMap.generated) {
console.warn("hitTokenMap.generated is missing", hitTokenMap);
return;
}
/**
* @deprecated use originalPositionFromPosition instead of
* @param generatedPosition
* @param {boolean} isEnd - is the position end of the node?
* @returns {Object}
*/
}, {
key: "originalPositionFor",
value: function originalPositionFor(generatedPosition, isEnd) {
return this.originalPositionFromPosition(generatedPosition, isEnd);
var outerAdjust = generatedIndex - hitTokenMap.generated[0];
var innerAdjust = hitTokenMap.intermediate[0] - hitTokenMap.original[0];
return outerAdjust + innerAdjust + hitTokenMap.original[0];
};
/**
* get original position from generated position
* @param {object} position
* @param {boolean} isEnd - is the position end of the node?
* @returns {object} original position
*/
StringSource.prototype.originalPositionFromPosition = function (position, isEnd) {
if (isEnd === void 0) { isEnd = false; }
if (typeof position.line === "undefined" || typeof position.column === "undefined") {
throw new Error("position.{line, column} should not undefined: " + JSON.stringify(position));
}
/**
* get original index from generated index value
* @param {number} generatedIndex - position is a index value.
* @param {boolean} isEnd - is the position end of the node?
* @returns {number|undefined} original
*/
}, {
key: "originalIndexFromIndex",
value: function originalIndexFromIndex(generatedIndex) {
var _this = this;
var isEnd = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var hitTokenMaps = this.tokenMaps.filter(function (tokenMap, index) {
var generated = tokenMap.generated;
var nextTokenMap = _this.tokenMaps[index + 1];
var nextGenerated = nextTokenMap ? nextTokenMap.generated : null;
if (nextGenerated) {
if (generated[0] <= generatedIndex && generatedIndex <= nextGenerated[0]) {
return true;
}
} else {
if (generated[0] <= generatedIndex && generatedIndex <= generated[1]) {
return true;
}
}
});
if (hitTokenMaps.length === 0) {
return;
}
/**
* **Str**ABC
* |
* |
* generatedIndex
*
* If isEnd is true, generatedIndex is end of **Str** node.
* If isEnd is false, generatedIndex is index of ABC node.
*/
var hitTokenMap = isEnd ? hitTokenMaps[0] : hitTokenMaps[hitTokenMaps.length - 1];
// <----------->[<------------->|text]
// ^ ^
// position-generated intermediate-origin
// <-------------->[<------------->|text]
// | |
// outer adjust _
// inner adjust = 1
var outerAdjust = generatedIndex - hitTokenMap.generated[0];
var innerAdjust = hitTokenMap.intermediate[0] - hitTokenMap.original[0];
return outerAdjust + innerAdjust + hitTokenMap.original[0];
var generatedIndex = this.generatedSource.positionToIndex(position);
if (isNaN(generatedIndex)) {
// Not Found
return;
}
/**
* get original position from generated position
* @param {object} position
* @param {boolean} isEnd - is the position end of the node?
* @returns {object} original position
*/
}, {
key: "originalPositionFromPosition",
value: function originalPositionFromPosition(position) {
var isEnd = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
if (typeof position.line === "undefined" || typeof position.column === "undefined") {
throw new Error("position.{line, column} should not undefined: " + JSON.stringify(position));
}
var generatedIndex = this.generatedSource.positionToIndex(position);
if (isNaN(generatedIndex)) {
// Not Found
return;
}
var originalIndex = this.originalIndexFromIndex(generatedIndex, isEnd);
return this.originalSource.indexToPosition(originalIndex, isEnd);
var originalIndex = this.originalIndexFromIndex(generatedIndex, isEnd);
if (originalIndex === undefined) {
return;
}
/**
* get original index from generated position
* @param {object} generatedPosition
* @param {boolean} isEnd - is the position end of the node?
* @returns {number} original index
*/
}, {
key: "originalIndexFromPosition",
value: function originalIndexFromPosition(generatedPosition) {
var isEnd = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var originalPosition = this.originalPositionFromPosition(generatedPosition);
return this.originalSource.positionToIndex(originalPosition, isEnd);
return this.originalSource.indexToPosition(originalIndex);
};
/**
* get original index from generated position
* @param {object} generatedPosition
* @param {boolean} isEnd - is the position end of the node?
* @returns {number} original index
*/
StringSource.prototype.originalIndexFromPosition = function (generatedPosition, isEnd) {
if (isEnd === void 0) { isEnd = false; }
var originalPosition = this.originalPositionFromPosition(generatedPosition, isEnd);
if (originalPosition === undefined) {
return;
}
/**
* get original position from generated index
* @param {number} generatedIndex
* @param {boolean} isEnd - is the position end of the node?
* @return {object} original position
*/
}, {
key: "originalPositionFromIndex",
value: function originalPositionFromIndex(generatedIndex) {
var isEnd = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var originalIndex = this.originalIndexFromIndex(generatedIndex);
return this.originalSource.indexToPosition(originalIndex, isEnd);
return this.originalSource.positionToIndex(originalPosition);
};
/**
* get original position from generated index
* @param {number} generatedIndex
* @param {boolean} isEnd - is the position end of the node?
* @return {object} original position
*/
StringSource.prototype.originalPositionFromIndex = function (generatedIndex, isEnd) {
if (isEnd === void 0) { isEnd = false; }
var originalIndex = this.originalIndexFromIndex(generatedIndex, isEnd);
if (originalIndex === undefined) {
return;
}
}, {
key: "isParagraphNode",
value: function isParagraphNode(node) {
return node.type === "Paragraph";
return this.originalSource.indexToPosition(originalIndex);
};
StringSource.prototype.isParagraphNode = function (node) {
return node.type === "Paragraph";
};
StringSource.prototype.isStringNode = function (node) {
return node.type === "Str";
};
/**
*
* @param node
* @returns {string|undefined}
* @private
*/
StringSource.prototype._getValue = function (node) {
if (node.value) {
return node.value;
}
}, {
key: "isStringNode",
value: function isStringNode(node) {
return node.type === "Str";
else if (node.alt) {
return node.alt;
}
/**
*
* @param node
* @returns {string|undefined}
* @private
*/
}, {
key: "_getValue",
value: function _getValue(node) {
if (node.value) {
return node.value;
} else if (node.alt) {
return node.alt;
} else if (node.title) {
// See https://github.com/azu/textlint-rule-sentence-length/issues/6
if (node.type === "Link") {
return;
}
return node.title;
else if (node.title) {
// See https://github.com/azu/textlint-rule-sentence-length/issues/6
if (node.type === "Link") {
return;
}
return node.title;
}
}, {
key: "_nodeRangeAsRelative",
value: function _nodeRangeAsRelative(node) {
// relative from root
return [node.range[0] - this.rootNode.range[0], node.range[1] - this.rootNode.range[0]];
else {
return;
}
}, {
key: "_valueOf",
value: function _valueOf(node, parent) {
if (!node) {
return;
}
// [padding][value][padding]
// =>
// [value][value][value]
var value = this._getValue(node);
if (!value) {
return;
}
if (parent === null || parent === undefined) {
return;
}
// <p><Str /></p>
if (this.isParagraphNode(parent) && this.isStringNode(node)) {
return {
original: this._nodeRangeAsRelative(node),
intermediate: this._nodeRangeAsRelative(node),
value: value
};
}
// <p><code>code</code></p>
// => container is <p>
// <p><strong><Str /></strong></p>
// => container is <strong>
var container = this.isParagraphNode(parent) ? node : parent;
var rawValue = container.raw;
// avoid match ! with ![
// TODO: indexOf(value, 1) 1 is unexpected ...
var paddingLeft = rawValue.indexOf(value, 1) === -1 ? 0 : rawValue.indexOf(value, 1);
var paddingRight = rawValue.length - (paddingLeft + value.length);
// original range should be relative value from rootNode
var originalRange = this._nodeRangeAsRelative(container);
var intermediateRange = [originalRange[0] + paddingLeft, originalRange[1] - paddingRight];
};
StringSource.prototype._nodeRangeAsRelative = function (node) {
// relative from root
return [node.range[0] - this.rootNode.range[0], node.range[1] - this.rootNode.range[0]];
};
StringSource.prototype._valueOf = function (node, parent) {
if (!node) {
return;
}
// [padding][value][padding]
// =>
// [value][value][value]
var value = this._getValue(node);
if (!value) {
return;
}
if (parent === null || parent === undefined) {
return;
}
// <p><Str /></p>
if (this.isParagraphNode(parent) && this.isStringNode(node)) {
return {
original: originalRange,
intermediate: intermediateRange,
value: value
original: this._nodeRangeAsRelative(node),
intermediate: this._nodeRangeAsRelative(node),
generatedValue: value
};
}
}, {
key: "_addTokenMap",
value: function _addTokenMap(tokenMap) {
if (tokenMap == null) {
// <p><code>code</code></p>
// => container is <p>
// <p><strong><Str /></strong></p>
// => container is <strong>
var container = this.isParagraphNode(parent) ? node : parent;
var rawValue = container.raw;
// avoid match ! with ![
// TODO: indexOf(value, 1) 1 is unexpected ...
var paddingLeft = rawValue.indexOf(value, 1) === -1 ? 0 : rawValue.indexOf(value, 1);
var paddingRight = rawValue.length - (paddingLeft + value.length);
// original range should be relative value from rootNode
var originalRange = this._nodeRangeAsRelative(container);
var intermediateRange = [originalRange[0] + paddingLeft, originalRange[1] - paddingRight];
return {
original: originalRange,
intermediate: intermediateRange,
generatedValue: value
};
};
StringSource.prototype._addTokenMap = function (tokenMap) {
if (tokenMap == null) {
return;
}
var addedTokenMap = Object.assign({}, tokenMap);
if (this.tokenMaps.length === 0) {
var textLength = addedTokenMap.intermediate[1] - addedTokenMap.intermediate[0];
addedTokenMap["generated"] = [0, textLength];
}
else {
var textLength = addedTokenMap.intermediate[1] - addedTokenMap.intermediate[0];
addedTokenMap["generated"] = [this.generatedString.length, this.generatedString.length + textLength];
}
this.generatedString += tokenMap.generatedValue;
this.tokenMaps.push(addedTokenMap);
};
/**
* Compute text content of a node. If the node itself
* does not expose plain-text fields, `toString` will
* recursively mapping
*
* @param {Node} node - Node to transform to a string.
* @param {Node} [parent] - Parent Node of the `node`.
*/
StringSource.prototype._stringify = function (node, parent) {
var _this = this;
var isHTML = node.type === "Html";
var currentNode = isHTML ? html2hast(node.value) : node;
var value = this._valueOf(currentNode, parent);
if (value) {
return value;
}
if (!isParentNode(node)) {
return;
}
currentNode.children.forEach(function (childNode) {
if (!isParentNode(node)) {
return;
}
var addedTokenMap = (0, _objectAssign2.default)({}, tokenMap);
if (this.tokenMaps.length === 0) {
var textLength = addedTokenMap.intermediate[1] - addedTokenMap.intermediate[0];
addedTokenMap["generated"] = [0, textLength];
} else {
var _textLength = addedTokenMap.intermediate[1] - addedTokenMap.intermediate[0];
addedTokenMap["generated"] = [this.generatedString.length, this.generatedString.length + _textLength];
var tokenMap = _this._stringify(childNode, node);
if (tokenMap) {
_this._addTokenMap(tokenMap);
}
this.generatedString += tokenMap.value;
this.tokenMaps.push(addedTokenMap);
}
/**
* Compute text content of a node. If the node itself
* does not expose plain-text fields, `toString` will
* recursivly try its children.
*
* @param {Node} node - Node to transform to a string.
* @param {Node} [parent] - Parent Node of the `node`.
*/
}, {
key: "_stringify",
value: function _stringify(node, parent) {
var _this2 = this;
var value = this._valueOf(node, parent);
if (value) {
return value;
}
if (!node.children) {
return;
}
node.children.forEach(function (childNode) {
var tokenMap = _this2._stringify(childNode, node);
if (tokenMap) {
_this2._addTokenMap(tokenMap);
}
});
}
}]);
});
};
return StringSource;
}();
}());
exports.default = StringSource;
var isParentNode = function (node) {
return "children" in node;
};
//# sourceMappingURL=StringSource.js.map
{
"name": "textlint-util-to-string",
"version": "3.0.0",
"description": "textlint util convert Paragraph Node to text with SourceMap.",
"homepage": "https://github.com/textlint/textlint-util-to-string",
"bugs": {
"url": "https://github.com/textlint/textlint-util-to-string/issues"
},
"repository": {

@@ -7,12 +13,4 @@ "type": "git",

},
"license": "MIT",
"author": "azu",
"email": "azuciao@gmail.com",
"homepage": "https://github.com/textlint/textlint-util-to-string",
"license": "MIT",
"bugs": {
"url": "https://github.com/textlint/textlint-util-to-string/issues"
},
"version": "2.1.1",
"description": "textlint util convert Paragraph Node to text with SourceMap.",
"main": "lib/index.js",
"files": [

@@ -22,2 +20,4 @@ "lib",

],
"main": "lib/index.js",
"types": "lib/index.d.ts",
"directories": {

@@ -27,21 +27,46 @@ "test": "test"

"scripts": {
"build": "NODE_ENV=production babel src --out-dir lib --source-maps",
"watch": "babel src --out-dir lib --watch --source-maps",
"build": "cross-env NODE_ENV=production tsc -p .",
"prepublish": "npm run --if-present build",
"test": "mocha test"
"test": "mocha \"test/**/*.{js,ts}\"",
"watch": "tsc -p . --watch",
"prettier": "prettier --write \"**/*.{js,jsx,ts,tsx,css}\""
},
"dependencies": {
"@textlint/ast-node-types": "^4.2.4",
"@types/structured-source": "^3.0.0",
"rehype-parse": "^6.0.1",
"structured-source": "^3.0.2",
"unified": "^8.4.0"
},
"devDependencies": {
"babel-cli": "^6.2.0",
"babel-preset-es2015": "^6.1.18",
"babel-preset-power-assert": "^1.0.0",
"babel-register": "^6.24.1",
"@types/mocha": "^5.2.7",
"@types/node": "^12.7.5",
"cross-env": "^5.2.1",
"husky": "^3.0.5",
"lint-staged": "^9.2.5",
"markdown-to-ast": "^4.0.0",
"mocha": "^3.0.2",
"power-assert": "^1.4.2",
"sentence-splitter": "^2.0.0"
"mocha": "^6.2.0",
"prettier": "^1.18.2",
"sentence-splitter": "^2.0.0",
"ts-node": "^8.4.1",
"ts-node-test-register": "^8.0.1",
"typescript": "^3.6.3"
},
"dependencies": {
"object-assign": "^4.0.1",
"structured-source": "^3.0.2"
"email": "azuciao@gmail.com",
"prettier": {
"singleQuote": false,
"printWidth": 120,
"tabWidth": 4
},
"husky": {
"hooks": {
"precommit": "lint-staged"
}
},
"lint-staged": {
"*.{js,jsx,ts,tsx,css}": [
"prettier --write",
"git add"
]
}
}

@@ -25,15 +25,15 @@ # textlint-util-to-string

## `originalIndexFromIndex(generatedIndex): number`
## `originalIndexFromIndex(generatedIndex): number | undefined`
Get original index from generated index value
## `originalPositionFromPosition(position): Position`
## `originalPositionFromPosition(position): Position | undefined`
Get original position from generated position
## `originalIndexFromPosition(generatedPosition): number`
## `originalIndexFromPosition(generatedPosition): number | undefined`
Get original index from generated position
## `originalPositionFromIndex(generatedIndex): Position`
## `originalPositionFromIndex(generatedIndex): Position | undefined`

@@ -43,5 +43,5 @@ Get original position from generated index

```js
import assert from "power-assert"
import assert from "assert"
import {parse} from "markdown-to-ast";
import StringSource from "textlint-util-to-string";
import {StringSource} from "textlint-util-to-string";

@@ -90,3 +90,3 @@ let originalText = "This is [Example!?](http://example.com/)";

```js
let AST = ....
let AST = {...}
let rootNode = AST.children[10];

@@ -93,0 +93,0 @@ let source = new StringSource(rootNode);

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc