textlint-rule-en-capitalization
Advanced tools
Comparing version 1.1.0 to 2.0.0
@@ -11,8 +11,11 @@ // MIT © 2017 azu | ||
var StringSource = require("textlint-util-to-string"); | ||
var _require2 = require("sentence-splitter"), | ||
split = _require2.split, | ||
splitAST = _require2.splitAST, | ||
SentenceSyntax = _require2.Syntax; | ||
var REPORT_TYPE = { | ||
Heading: "Heading", | ||
Paragraph: "Paragraph", | ||
List: "List" | ||
}; | ||
var shouldNotCapitalized = function shouldNotCapitalized(string, allowWords) { | ||
@@ -39,21 +42,3 @@ // allow words | ||
/** | ||
* get node at index | ||
* @param node | ||
* @param index | ||
* @returns {undefined|*} | ||
*/ | ||
var getNodeAtIndex = function getNodeAtIndex(node, index) { | ||
if (!node || !node.children) { | ||
return undefined; | ||
} | ||
for (var i = 0; i < node.children.length; i++) { | ||
var childNode = node.children[i]; | ||
if (childNode.range[0] <= index && index <= childNode.range[1]) { | ||
return childNode; | ||
} | ||
} | ||
return undefined; | ||
}; | ||
/** | ||
* @param node | ||
* @param Syntax | ||
@@ -66,2 +51,3 @@ * @param {function} getSource | ||
* @param {string[]} allowWords allow lower-case words | ||
* @param {string} reportType REPORT_TYPE | ||
*/ | ||
@@ -76,39 +62,32 @@ var checkNode = function checkNode(_ref) { | ||
allowFigures = _ref.allowFigures, | ||
allowWords = _ref.allowWords; | ||
allowWords = _ref.allowWords, | ||
reportType = _ref.reportType; | ||
var source = new StringSource(node); | ||
var sourceText = source.toString(); | ||
var sentences = split(sourceText); | ||
sentences.filter(function (sentence) { | ||
var DocumentURL = "https://owl.english.purdue.edu/owl/resource/592/01/"; | ||
var paragraphNode = splitAST(node); | ||
paragraphNode.children.filter(function (sentence) { | ||
return sentence.type === SentenceSyntax.Sentence; | ||
}).forEach(function (sentence) { | ||
var originalIndex = source.originalIndexFromIndex(sentence.range[0]); | ||
var targetNode = void 0; | ||
if (node.type === Syntax.ListItem) { | ||
targetNode = getNodeAtIndex(node.children[0], node.range[0] + originalIndex); | ||
} else { | ||
targetNode = getNodeAtIndex(node, originalIndex); | ||
} | ||
if (!targetNode) { | ||
var sentenceFirstNode = sentence.children[0]; | ||
if (!sentenceFirstNode) { | ||
return; | ||
} | ||
var DocumentURL = "https://owl.english.purdue.edu/owl/resource/592/01/"; | ||
// check | ||
if (targetNode.type === Syntax.Str) { | ||
var text = sentence.value; | ||
var firstWord = text.split(" ")[0]; | ||
// check first word is String | ||
if (sentenceFirstNode.type === Syntax.Str) { | ||
var text = sentenceFirstNode.value; | ||
var firstWord = text.split(/\s/)[0]; | ||
if ((0, _captalize.isCapitalized)(firstWord) || shouldNotCapitalized(firstWord, allowWords)) { | ||
return; | ||
} | ||
var index = originalIndex; | ||
return report(node, new RuleError("Heading: Follow the standard capitalization rules for American English.\nSee " + DocumentURL, { | ||
var index = 0; | ||
return report(sentenceFirstNode, new RuleError(reportType + ": Follow the standard capitalization rules for American English.\nSee " + DocumentURL, { | ||
index: index, | ||
fix: fixer.replaceTextRange([index, index + firstWord.length], (0, _captalize.upperFirstCharacter)(firstWord)) | ||
})); | ||
} else if (allowFigures && targetNode.type === Syntax.Image && typeof targetNode.alt === "string") { | ||
var _text = targetNode.alt; | ||
} else if (allowFigures && sentenceFirstNode.type === Syntax.Image && typeof sentenceFirstNode.alt === "string") { | ||
var _text = sentenceFirstNode.alt; | ||
if ((0, _captalize.isCapitalized)(_text) || shouldNotCapitalized(_text, allowWords)) { | ||
return; | ||
} | ||
return report(targetNode, new RuleError("Image alt: Follow the standard capitalization rules for American English\nSee " + DocumentURL)); | ||
return report(sentenceFirstNode, new RuleError("Image alt: Follow the standard capitalization rules for American English\nSee " + DocumentURL)); | ||
} | ||
@@ -148,3 +127,13 @@ }); | ||
} | ||
checkNode({ node: node, Syntax: Syntax, getSource: getSource, report: report, RuleError: RuleError, fixer: fixer, allowFigures: allowFigures, allowWords: allowWords }); | ||
checkNode({ | ||
node: node, | ||
Syntax: Syntax, | ||
getSource: getSource, | ||
report: report, | ||
RuleError: RuleError, | ||
fixer: fixer, | ||
allowFigures: allowFigures, | ||
allowWords: allowWords, | ||
reportType: REPORT_TYPE.Heading | ||
}); | ||
}), _defineProperty(_ref2, Syntax.Paragraph, function (node) { | ||
@@ -157,3 +146,13 @@ if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis])) { | ||
} | ||
checkNode({ node: node, Syntax: Syntax, getSource: getSource, report: report, RuleError: RuleError, fixer: fixer, allowFigures: allowFigures, allowWords: allowWords }); | ||
checkNode({ | ||
node: node, | ||
Syntax: Syntax, | ||
getSource: getSource, | ||
report: report, | ||
RuleError: RuleError, | ||
fixer: fixer, | ||
allowFigures: allowFigures, | ||
allowWords: allowWords, | ||
reportType: REPORT_TYPE.Paragraph | ||
}); | ||
}), _defineProperty(_ref2, Syntax.ListItem, function (node) { | ||
@@ -163,3 +162,15 @@ if (!allowLists) { | ||
} | ||
checkNode({ node: node, Syntax: Syntax, getSource: getSource, report: report, RuleError: RuleError, fixer: fixer, allowFigures: allowFigures, allowWords: allowWords }); | ||
node.children.forEach(function (paragraph) { | ||
checkNode({ | ||
node: paragraph, | ||
Syntax: Syntax, | ||
getSource: getSource, | ||
report: report, | ||
RuleError: RuleError, | ||
fixer: fixer, | ||
allowFigures: allowFigures, | ||
allowWords: allowWords, | ||
reportType: REPORT_TYPE.List | ||
}); | ||
}); | ||
}), _ref2; | ||
@@ -166,0 +177,0 @@ }; |
@@ -14,3 +14,3 @@ { | ||
"name": "textlint-rule-en-capitalization", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"description": "textlint rule that check capitalization in english text.", | ||
@@ -41,5 +41,5 @@ "main": "lib/textlint-rule-en-capitalization.js", | ||
"husky": "^0.14.3", | ||
"lint-staged": "^4.3.0", | ||
"prettier": "^1.7.4", | ||
"textlint-scripts": "^1.3.0" | ||
"lint-staged": "^6.0.0", | ||
"prettier": "^1.9.2", | ||
"textlint-scripts": "^1.4.0" | ||
}, | ||
@@ -58,6 +58,5 @@ "prettier": { | ||
"en-pos": "^1.0.16", | ||
"sentence-splitter": "^2.2.0", | ||
"textlint-rule-helper": "^2.0.0", | ||
"textlint-util-to-string": "^2.1.1" | ||
"sentence-splitter": "^3.0.0", | ||
"textlint-rule-helper": "^2.0.0" | ||
} | ||
} |
// MIT © 2017 azu | ||
"use strict"; | ||
const { RuleHelper } = require("textlint-rule-helper"); | ||
const StringSource = require("textlint-util-to-string"); | ||
const { split, Syntax: SentenceSyntax } = require("sentence-splitter"); | ||
const { splitAST, Syntax: SentenceSyntax } = require("sentence-splitter"); | ||
import { getPosFromSingleWord, isCapitalized, upperFirstCharacter } from "./captalize"; | ||
const REPORT_TYPE = { | ||
Heading: "Heading", | ||
Paragraph: "Paragraph", | ||
List: "List" | ||
}; | ||
const shouldNotCapitalized = (string, allowWords) => { | ||
@@ -29,21 +33,3 @@ // allow words | ||
/** | ||
* get node at index | ||
* @param node | ||
* @param index | ||
* @returns {undefined|*} | ||
*/ | ||
const getNodeAtIndex = (node, index) => { | ||
if (!node || !node.children) { | ||
return undefined; | ||
} | ||
for (let i = 0; i < node.children.length; i++) { | ||
const childNode = node.children[i]; | ||
if (childNode.range[0] <= index && index <= childNode.range[1]) { | ||
return childNode; | ||
} | ||
} | ||
return undefined; | ||
}; | ||
/** | ||
* @param node | ||
* @param Syntax | ||
@@ -56,31 +42,24 @@ * @param {function} getSource | ||
* @param {string[]} allowWords allow lower-case words | ||
* @param {string} reportType REPORT_TYPE | ||
*/ | ||
const checkNode = ({ node, Syntax, getSource, report, RuleError, fixer, allowFigures, allowWords }) => { | ||
const source = new StringSource(node); | ||
const sourceText = source.toString(); | ||
const sentences = split(sourceText); | ||
sentences.filter(sentence => sentence.type === SentenceSyntax.Sentence).forEach(sentence => { | ||
const originalIndex = source.originalIndexFromIndex(sentence.range[0]); | ||
let targetNode; | ||
if (node.type === Syntax.ListItem) { | ||
targetNode = getNodeAtIndex(node.children[0], node.range[0] + originalIndex); | ||
} else { | ||
targetNode = getNodeAtIndex(node, originalIndex); | ||
} | ||
if (!targetNode) { | ||
const checkNode = ({ node, Syntax, getSource, report, RuleError, fixer, allowFigures, allowWords, reportType }) => { | ||
const DocumentURL = "https://owl.english.purdue.edu/owl/resource/592/01/"; | ||
const paragraphNode = splitAST(node); | ||
paragraphNode.children.filter(sentence => sentence.type === SentenceSyntax.Sentence).forEach(sentence => { | ||
const sentenceFirstNode = sentence.children[0]; | ||
if (!sentenceFirstNode) { | ||
return; | ||
} | ||
const DocumentURL = "https://owl.english.purdue.edu/owl/resource/592/01/"; | ||
// check | ||
if (targetNode.type === Syntax.Str) { | ||
const text = sentence.value; | ||
const firstWord = text.split(" ")[0]; | ||
// check first word is String | ||
if (sentenceFirstNode.type === Syntax.Str) { | ||
const text = sentenceFirstNode.value; | ||
const firstWord = text.split(/\s/)[0]; | ||
if (isCapitalized(firstWord) || shouldNotCapitalized(firstWord, allowWords)) { | ||
return; | ||
} | ||
const index = originalIndex; | ||
const index = 0; | ||
return report( | ||
node, | ||
sentenceFirstNode, | ||
new RuleError( | ||
`Heading: Follow the standard capitalization rules for American English. | ||
`${reportType}: Follow the standard capitalization rules for American English. | ||
See ${DocumentURL}`, | ||
@@ -93,4 +72,8 @@ { | ||
); | ||
} else if (allowFigures && targetNode.type === Syntax.Image && typeof targetNode.alt === "string") { | ||
const text = targetNode.alt; | ||
} else if ( | ||
allowFigures && | ||
sentenceFirstNode.type === Syntax.Image && | ||
typeof sentenceFirstNode.alt === "string" | ||
) { | ||
const text = sentenceFirstNode.alt; | ||
if (isCapitalized(text) || shouldNotCapitalized(text, allowWords)) { | ||
@@ -100,3 +83,3 @@ return; | ||
return report( | ||
targetNode, | ||
sentenceFirstNode, | ||
new RuleError( | ||
@@ -134,3 +117,13 @@ `Image alt: Follow the standard capitalization rules for American English | ||
} | ||
checkNode({ node, Syntax, getSource, report, RuleError, fixer, allowFigures, allowWords }); | ||
checkNode({ | ||
node, | ||
Syntax, | ||
getSource, | ||
report, | ||
RuleError, | ||
fixer, | ||
allowFigures, | ||
allowWords, | ||
reportType: REPORT_TYPE.Heading | ||
}); | ||
}, | ||
@@ -144,3 +137,13 @@ [Syntax.Paragraph](node) { | ||
} | ||
checkNode({ node, Syntax, getSource, report, RuleError, fixer, allowFigures, allowWords }); | ||
checkNode({ | ||
node, | ||
Syntax, | ||
getSource, | ||
report, | ||
RuleError, | ||
fixer, | ||
allowFigures, | ||
allowWords, | ||
reportType: REPORT_TYPE.Paragraph | ||
}); | ||
}, | ||
@@ -151,3 +154,15 @@ [Syntax.ListItem](node) { | ||
} | ||
checkNode({ node, Syntax, getSource, report, RuleError, fixer, allowFigures, allowWords }); | ||
node.children.forEach(paragraph => { | ||
checkNode({ | ||
node: paragraph, | ||
Syntax, | ||
getSource, | ||
report, | ||
RuleError, | ||
fixer, | ||
allowFigures, | ||
allowWords, | ||
reportType: REPORT_TYPE.List | ||
}); | ||
}); | ||
} | ||
@@ -154,0 +169,0 @@ }; |
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
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
3
386
30926
+ Added@textlint/ast-node-types@4.4.3(transitive)
+ Addedconcat-stream@2.0.0(transitive)
+ Addedobject_values@0.1.2(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsentence-splitter@3.2.3(transitive)
+ Addedstring_decoder@1.3.0(transitive)
- Removedtextlint-util-to-string@^2.1.1
- Removedconcat-stream@1.6.2(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removedisarray@1.0.0(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedreadable-stream@2.3.8(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedsentence-splitter@2.3.2(transitive)
- Removedstring_decoder@1.1.1(transitive)
- Removedtextlint-util-to-string@2.1.1(transitive)
Updatedsentence-splitter@^3.0.0