textlint-util-to-string
Advanced tools
Comparing version 3.3.0 to 3.3.2
@@ -1,2 +0,1 @@ | ||
import StringSource from "./StringSource"; | ||
export { StringSource }; | ||
export { StringSource } from "./StringSource"; |
@@ -8,3 +8,3 @@ import type { TxtNode, TxtParentNode } from "@textlint/ast-node-types"; | ||
node: TxtNode | UnistNode; | ||
parent?: TxtParentNode; | ||
parent?: StringSourceTxtParentNodeLikeNode; | ||
maskValue: typeof maskValue; | ||
@@ -14,3 +14,6 @@ emptyValue: typeof emptyValue; | ||
}; | ||
export default class StringSource { | ||
export type StringSourceTxtParentNodeLikeNode = TxtParentNode | (Omit<TxtParentNode, "type"> & { | ||
type: string; | ||
}); | ||
export declare class StringSource { | ||
private rootNode; | ||
@@ -21,3 +24,3 @@ private generatedString; | ||
private tokenMaps; | ||
constructor(node: TxtParentNode, options?: StringSourceOptions); | ||
constructor(node: StringSourceTxtParentNodeLikeNode, options?: StringSourceOptions); | ||
toString(): string; | ||
@@ -65,3 +68,3 @@ /** | ||
originalPositionFromIndex(generatedIndex: number, isEnd?: boolean): SourcePosition | undefined; | ||
isParagraphNode(node: TxtNode): boolean; | ||
isParagraphNode(node: TxtNode | StringSourceTxtParentNodeLikeNode): boolean; | ||
isStringNode(node: TxtNode | UnistNode): boolean; | ||
@@ -68,0 +71,0 @@ /** |
{ | ||
"name": "textlint-util-to-string", | ||
"version": "3.3.0", | ||
"version": "3.3.2", | ||
"description": "textlint utility that convert Paragraph Node to text with SourceMap.", | ||
@@ -43,2 +43,3 @@ "homepage": "https://github.com/textlint/textlint-util-to-string", | ||
"devDependencies": { | ||
"@textlint/markdown-to-ast": "^13.2.0", | ||
"@types/mocha": "^10.0.1", | ||
@@ -48,7 +49,6 @@ "@types/node": "^18.11.18", | ||
"lint-staged": "^13.1.0", | ||
"@textlint/markdown-to-ast": "^13.2.0", | ||
"microbundle": "^0.15.1", | ||
"mocha": "^10.2.0", | ||
"prettier": "^2.8.3", | ||
"sentence-splitter": "^3.2.2", | ||
"sentence-splitter": "^4.1.0", | ||
"ts-node": "^10.9.1", | ||
@@ -55,0 +55,0 @@ "ts-node-test-register": "^10.0.0", |
@@ -123,4 +123,37 @@ # textlint-util-to-string [![Actions Status: test](https://github.com/textlint/textlint-util-to-string/workflows/test/badge.svg)](https://github.com/textlint/textlint-util-to-string/actions?query=workflow%3A"test") | ||
## Examples | ||
### Integration with sentence-splitter | ||
[sentence-splitter](https://github.com/textlint-rule/sentence-splitter) splits a paragraph into sentences. | ||
You can pass the Sentence node to `StringSource` to get the plain text of the sentence. | ||
```ts | ||
import assert from "assert"; | ||
import { splitAST, SentenceSplitterSyntax } from "sentence-splitter"; | ||
import { StringSource } from "textlint-util-to-string"; | ||
import type { TextlintRuleModule } from "@textlint/types"; | ||
const report: TextlintRuleModule<Options> = function (context) { | ||
const { Syntax, report, RuleError } = context; | ||
return { | ||
// "First sentence. Second sentence." | ||
[Syntax.Paragraph](node) { | ||
// { children: [Sentence, WhiteSpace, Sentence] } | ||
const sentenceRoot = splitAST(node); | ||
// ["First sentence." node, "Second sentence." node] | ||
const sentences = sentenceRoot.children.filter((node) => node.type === SentenceSplitterSyntax.Sentence); | ||
for (const sentence of sentences) { | ||
const sentenceSource = new StringSource(sentence); | ||
const sentenceText = sentenceSource.toString(); | ||
console.log(sentenceText); | ||
const sentenceIndex = sentenceText.indexOf("sentence"); | ||
const originalSentenceIndex = sentenceSource.originalIndexFromIndex(sentenceIndex); | ||
console.log({ sentenceIndex, originalSentenceIndex }); | ||
} | ||
} | ||
} | ||
}; | ||
export default report; | ||
``` | ||
## Rules that use this library | ||
- [azu/textlint-rule-first-sentence-length: textlint rule that limit maximum length of First sentence of the section.](https://github.com/azu/textlint-rule-first-sentence-length) | ||
@@ -127,0 +160,0 @@ - [azu/textlint-rule-en-max-word-count: textlint rule that specify the maximum word count of a sentence.](https://github.com/azu/textlint-rule-en-max-word-count) |
@@ -1,3 +0,1 @@ | ||
import StringSource from "./StringSource"; | ||
export { StringSource }; | ||
export { StringSource } from "./StringSource"; |
import type { TxtNode, TxtParentNode } from "@textlint/ast-node-types"; | ||
import { StructuredSource, SourcePosition } from "structured-source"; | ||
import { SourcePosition, StructuredSource } from "structured-source"; | ||
import type { Node as UnistNode } from "unist"; | ||
@@ -18,3 +18,3 @@ import unified from "unified"; | ||
const isParentNode = (node: TxtNode | TxtParentNode): node is TxtParentNode => { | ||
const isParentNode = (node: TxtNode | StringSourceTxtParentNodeLikeNode): node is StringSourceTxtParentNodeLikeNode => { | ||
return "children" in node; | ||
@@ -52,3 +52,3 @@ }; | ||
node: TxtNode | UnistNode; | ||
parent?: TxtParentNode; | ||
parent?: StringSourceTxtParentNodeLikeNode; | ||
maskValue: typeof maskValue; | ||
@@ -58,4 +58,6 @@ emptyValue: typeof emptyValue; | ||
}; | ||
export default class StringSource { | ||
private rootNode: TxtParentNode; | ||
export type StringSourceTxtParentNodeLikeNode = TxtParentNode | (Omit<TxtParentNode, "type"> & { type: string }); | ||
export class StringSource { | ||
private rootNode: StringSourceTxtParentNodeLikeNode; | ||
private generatedString: string; | ||
@@ -66,3 +68,3 @@ private originalSource: StructuredSource; | ||
constructor(node: TxtParentNode, options: StringSourceOptions = {}) { | ||
constructor(node: StringSourceTxtParentNodeLikeNode, options: StringSourceOptions = {}) { | ||
this.rootNode = node; | ||
@@ -208,3 +210,3 @@ this.tokenMaps = []; | ||
isParagraphNode(node: TxtNode): boolean { | ||
isParagraphNode(node: TxtNode | StringSourceTxtParentNodeLikeNode): boolean { | ||
return node.type === "Paragraph"; | ||
@@ -257,3 +259,3 @@ } | ||
node: TxtNode | UnistNode; | ||
parent?: TxtParentNode; | ||
parent?: StringSourceTxtParentNodeLikeNode; | ||
options: StringSourceOptions; | ||
@@ -337,4 +339,4 @@ }): StringSourceIR | undefined { | ||
}: { | ||
node: TxtNode | TxtParentNode; | ||
parent?: TxtParentNode; | ||
node: TxtNode | StringSourceTxtParentNodeLikeNode; | ||
parent?: StringSourceTxtParentNodeLikeNode; | ||
options: StringSourceOptions; | ||
@@ -341,0 +343,0 @@ }): void | StringSourceIR { |
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
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
4994797
22
11872
193