textlint-plugin-html
Advanced tools
Comparing version
@@ -5,25 +5,18 @@ // LICENSE : MIT | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | ||
exports.parse = parse; | ||
var _hast = require("hast"); | ||
var _hast = _interopRequireDefault(require("hast")); | ||
var _hast2 = _interopRequireDefault(_hast); | ||
var _traverse = _interopRequireDefault(require("traverse")); | ||
var _traverse = require("traverse"); | ||
var _structuredSource = _interopRequireDefault(require("structured-source")); | ||
var _traverse2 = _interopRequireDefault(_traverse); | ||
var _mapping = require("./mapping"); | ||
var _structuredSource = require("structured-source"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
var _structuredSource2 = _interopRequireDefault(_structuredSource); | ||
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } | ||
var _mapping = require("./mapping"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
@@ -34,80 +27,103 @@ * Remove undocumented properties on TxtNode from node | ||
function removeUnusedProperties(node) { | ||
if ((typeof node === "undefined" ? "undefined" : _typeof(node)) !== "object") { | ||
return; | ||
if (_typeof(node) !== "object") { | ||
return; | ||
} | ||
["position"].forEach(function (key) { | ||
if (node.hasOwnProperty(key)) { | ||
delete node[key]; | ||
} | ||
["position"].forEach(function (key) { | ||
if (node.hasOwnProperty(key)) { | ||
delete node[key]; | ||
} | ||
}); | ||
}); | ||
} | ||
function mapNodeType(node, parent) { | ||
if (parent) { | ||
var parentNode = parent.parent.node; | ||
if (parentNode.tagName === "script" || parentNode.tagName === "style") { | ||
return "CodeBlock"; | ||
} | ||
if (parent) { | ||
var parentNode = parent.parent.node; | ||
if (parentNode.tagName === "script" || parentNode.tagName === "style") { | ||
return "CodeBlock"; | ||
} | ||
if (node.tagName && node.type === "element") { | ||
var mappedType = _mapping.tagNameToType[node.tagName]; | ||
if (mappedType) { | ||
// p => Paragraph... | ||
return mappedType; | ||
} else { | ||
// other element is "Html" | ||
return "Html"; | ||
} | ||
} | ||
if (node.tagName && node.type === "element") { | ||
var mappedType = _mapping.tagNameToType[node.tagName]; | ||
if (mappedType) { | ||
// p => Paragraph... | ||
return mappedType; | ||
} else { | ||
// text => Str | ||
return _mapping.nodeTypes[node.type]; | ||
// other element is "Html" | ||
return "Html"; | ||
} | ||
} else { | ||
// text => Str | ||
return _mapping.nodeTypes[node.type]; | ||
} | ||
} | ||
function parse(html) { | ||
var ast = _hast2.default.parse(html); | ||
var src = new _structuredSource2.default(html); | ||
var tr = (0, _traverse2.default)(ast); | ||
tr.forEach(function (node) { | ||
if (this.notLeaf) { | ||
// avoid conflict <input type="text" /> | ||
// AST node has type and position | ||
if (node.type && node.position) { | ||
// case: element => Html or ... | ||
node.type = mapNodeType(node, this.parent); | ||
} else if (node.type === "root") { | ||
// FIXME: workaround, should fix hast | ||
node.type = _mapping.nodeTypes[node.type]; | ||
var position = src.rangeToLocation([0, html.length]); | ||
// reverse adjust | ||
node.position = { | ||
start: { line: position.start.line, column: position.start.column + 1 }, | ||
end: { line: position.end.line, column: position.end.column + 1 } | ||
}; | ||
} | ||
// Unknown type | ||
if (typeof node.type === "undefined") { | ||
node.type = "UNKNOWN"; | ||
} | ||
// map `range`, `loc` and `raw` to node | ||
if (_typeof(node.position) === "object") { | ||
var _position = node.position; | ||
// TxtNode's line start with 1 | ||
// TxtNode's column start with 0 | ||
var positionCompensated = { | ||
start: { line: _position.start.line, column: _position.start.column - 1 }, | ||
end: { line: _position.end.line, column: _position.end.column - 1 } | ||
}; | ||
var range = src.locationToRange(positionCompensated); | ||
node.loc = positionCompensated; | ||
node.range = range; | ||
node.raw = html.slice(range[0], range[1]); | ||
} | ||
// map `url` to Link node | ||
if (node.type === "Link" && typeof node.properties.href !== "undefined") { | ||
node.url = node.properties.href; | ||
} | ||
} | ||
removeUnusedProperties(node); | ||
}); | ||
return ast; | ||
var ast = _hast["default"].parse(html); | ||
var src = new _structuredSource["default"](html); | ||
var tr = (0, _traverse["default"])(ast); | ||
tr.forEach(function (node) { | ||
if (this.notLeaf) { | ||
// avoid conflict <input type="text" /> | ||
// AST node has type and position | ||
if (node.type && node.position) { | ||
// case: element => Html or ... | ||
node.type = mapNodeType(node, this.parent); | ||
} else if (node.type === "root") { | ||
// FIXME: workaround, should fix hast | ||
node.type = _mapping.nodeTypes[node.type]; | ||
var position = src.rangeToLocation([0, html.length]); // reverse adjust | ||
node.position = { | ||
start: { | ||
line: position.start.line, | ||
column: position.start.column + 1 | ||
}, | ||
end: { | ||
line: position.end.line, | ||
column: position.end.column + 1 | ||
} | ||
}; | ||
} // Unknown type | ||
if (typeof node.type === "undefined") { | ||
node.type = "UNKNOWN"; | ||
} // map `range`, `loc` and `raw` to node | ||
if (_typeof(node.position) === "object") { | ||
var _position = node.position; // TxtNode's line start with 1 | ||
// TxtNode's column start with 0 | ||
var positionCompensated = { | ||
start: { | ||
line: _position.start.line, | ||
column: _position.start.column - 1 | ||
}, | ||
end: { | ||
line: _position.end.line, | ||
column: _position.end.column - 1 | ||
} | ||
}; | ||
var range = src.locationToRange(positionCompensated); | ||
node.loc = positionCompensated; | ||
node.range = range; | ||
node.raw = html.slice(range[0], range[1]); | ||
} // map `url` to Link node | ||
if (node.type === "Link" && typeof node.properties.href !== "undefined") { | ||
node.url = node.properties.href; | ||
} | ||
} | ||
removeUnusedProperties(node); | ||
}); | ||
return ast; | ||
} | ||
//# sourceMappingURL=html-to-ast.js.map |
@@ -5,7 +5,6 @@ // LICENSE : MIT | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
exports["default"] = void 0; | ||
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 _htmlToAst = require("./html-to-ast"); | ||
@@ -15,36 +14,40 @@ | ||
var HTMLProcessor = function () { | ||
function HTMLProcessor(config) { | ||
_classCallCheck(this, HTMLProcessor); | ||
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); } } | ||
this.config = config; | ||
this.extensions = this.config.extensions ? this.config.extensions : []; | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } | ||
var HTMLProcessor = /*#__PURE__*/function () { | ||
function HTMLProcessor(config) { | ||
_classCallCheck(this, HTMLProcessor); | ||
this.config = config; | ||
this.extensions = this.config.extensions ? this.config.extensions : []; | ||
} | ||
_createClass(HTMLProcessor, [{ | ||
key: "availableExtensions", | ||
value: function availableExtensions() { | ||
return [".htm", ".html"].concat(this.extensions); | ||
} | ||
_createClass(HTMLProcessor, [{ | ||
key: "availableExtensions", | ||
value: function availableExtensions() { | ||
return [".htm", ".html"].concat(this.extensions); | ||
}, { | ||
key: "processor", | ||
value: function processor(ext) { | ||
return { | ||
preProcess: function preProcess(text, filePath) { | ||
return (0, _htmlToAst.parse)(text); | ||
}, | ||
postProcess: function postProcess(messages, filePath) { | ||
return { | ||
messages: messages, | ||
filePath: filePath ? filePath : "<html>" | ||
}; | ||
} | ||
}, { | ||
key: "processor", | ||
value: function processor(ext) { | ||
return { | ||
preProcess: function preProcess(text, filePath) { | ||
return (0, _htmlToAst.parse)(text); | ||
}, | ||
postProcess: function postProcess(messages, filePath) { | ||
return { | ||
messages: messages, | ||
filePath: filePath ? filePath : "<html>" | ||
}; | ||
} | ||
}; | ||
} | ||
}]); | ||
}; | ||
} | ||
}]); | ||
return HTMLProcessor; | ||
return HTMLProcessor; | ||
}(); | ||
exports.default = HTMLProcessor; | ||
exports["default"] = HTMLProcessor; | ||
//# sourceMappingURL=HTMLProcessor.js.map |
// LICENSE : MIT | ||
"use strict"; | ||
var _HTMLProcessor = require("./HTMLProcessor"); | ||
var _HTMLProcessor = _interopRequireDefault(require("./HTMLProcessor")); | ||
var _HTMLProcessor2 = _interopRequireDefault(_HTMLProcessor); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
module.exports = { | ||
Processor: _HTMLProcessor2.default | ||
Processor: _HTMLProcessor["default"] | ||
}; | ||
//# sourceMappingURL=index.js.map |
@@ -5,43 +5,47 @@ // LICENSE : MIT | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
var tagNameToType = exports.tagNameToType = { | ||
"p": "Paragraph", | ||
"ui": "ListItem", | ||
"li": "List", | ||
"q": "BlockQuote", | ||
"blockquote": "BlockQuote", | ||
"code": "CodeBlock", | ||
"hr": "horizontalRule", | ||
"br": "break", | ||
"em": "Emphasis", | ||
"strong": "Strong", | ||
"a": "Link", | ||
"img": "Image" | ||
exports.tagNameToType = exports.nodeTypes = void 0; | ||
var tagNameToType = { | ||
"p": "Paragraph", | ||
"ul": "List", | ||
"ol": "List", | ||
"li": "ListItem", | ||
"q": "BlockQuote", | ||
"blockquote": "BlockQuote", | ||
"code": "CodeBlock", | ||
"hr": "horizontalRule", | ||
"br": "break", | ||
"em": "Emphasis", | ||
"strong": "Strong", | ||
"a": "Link", | ||
"img": "Image" | ||
}; | ||
var nodeTypes = exports.nodeTypes = { | ||
"root": "Document", | ||
"paragraph": "Paragraph", | ||
"blockquote": "BlockQuote", | ||
"listItem": "ListItem", | ||
"list": "List", | ||
"Bullet": "Bullet", // no need? | ||
"heading": "Header", | ||
"code": "CodeBlock", | ||
"comment": "Comment", | ||
"HtmlBlock": "Html", | ||
"ReferenceDef": "ReferenceDef", | ||
"horizontalRule": "HorizontalRule", | ||
// inline block | ||
'text': 'Str', | ||
'break': 'Break', | ||
'emphasis': 'Emphasis', | ||
'strong': 'Strong', | ||
'html': 'Html', | ||
'link': 'Link', | ||
'image': 'Image', | ||
'inlineCode': 'Code', | ||
'yaml': 'Yaml' | ||
exports.tagNameToType = tagNameToType; | ||
var nodeTypes = { | ||
"root": "Document", | ||
"paragraph": "Paragraph", | ||
"blockquote": "BlockQuote", | ||
"listItem": "ListItem", | ||
"list": "List", | ||
"Bullet": "Bullet", | ||
// no need? | ||
"heading": "Header", | ||
"code": "CodeBlock", | ||
"comment": "Comment", | ||
"HtmlBlock": "Html", | ||
"ReferenceDef": "ReferenceDef", | ||
"horizontalRule": "HorizontalRule", | ||
// inline block | ||
'text': 'Str', | ||
'break': 'Break', | ||
'emphasis': 'Emphasis', | ||
'strong': 'Strong', | ||
'html': 'Html', | ||
'link': 'Link', | ||
'image': 'Image', | ||
'inlineCode': 'Code', | ||
'yaml': 'Yaml' | ||
}; | ||
exports.nodeTypes = nodeTypes; | ||
//# sourceMappingURL=mapping.js.map |
@@ -14,3 +14,3 @@ { | ||
}, | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "textlint HTML processsor plugin.", | ||
@@ -38,12 +38,11 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"babel-cli": "^6.8.0", | ||
"babel-preset-es2015": "^6.6.0", | ||
"babel-preset-jsdoc-to-assert": "^4.0.0", | ||
"babel-preset-power-assert": "^1.0.0", | ||
"babel-register": "^6.8.0", | ||
"@babel/cli": "^7.0.0", | ||
"@babel/core": "^7.0.0", | ||
"@babel/preset-env": "^7.0.0", | ||
"@babel/register": "^7.0.0", | ||
"@textlint/ast-tester": "^12.1.0", | ||
"@textlint/module-interop": "^12.1.0", | ||
"glob": "^7.1.1", | ||
"mocha": "^3.2.0", | ||
"power-assert": "^1.4.0", | ||
"textlint": "^11.0.0", | ||
"textlint-ast-test": "^1.1.3", | ||
"mocha": "^9.1.3", | ||
"textlint": "^12.1.0", | ||
"textlint-rule-no-todo": "^2.0.0" | ||
@@ -50,0 +49,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
# textlint-plugin-html [](https://travis-ci.org/textlint/textlint-plugin-html) | ||
# textlint-plugin-html [](https://github.com/textlint/textlint-plugin-html/actions?query=workflow%3A"test") | ||
@@ -3,0 +3,0 @@ Add HTML support for [textlint](https://github.com/textlint/textlint "textlint"). |
@@ -5,4 +5,5 @@ // LICENSE : MIT | ||
"p": "Paragraph", | ||
"ui": "ListItem", | ||
"li": "List", | ||
"ul": "List", | ||
"ol": "List", | ||
"li": "ListItem", | ||
"q": "BlockQuote", | ||
@@ -9,0 +10,0 @@ "blockquote": "BlockQuote", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
25872
4.17%10
-9.09%363
3.71%